如何从 php 中获取数据并显示在 FRAGMENT 的 android 列表视图中?
how to fetch data from php and display in android listview in FRAGMENT?
错误投入 onPostExecute()
:
The constructor SimpleAdapter(Commodities,
ArrayList>, int, String[], int[]) is
undefined
问题出在我的 AsyncTask
的 onPostExecute()
中:
@Override
protected void onPostExecute(Void result) {
super.onPostExecute(result);
ListAdapter adapter = new SimpleAdapter(Commodities.this,
matchFixtureList, R.layout.list_item, new String[] {
TAG_SCRIPT, TAG_BUYSELL, TAG_TRADESELL, TAG_TARGET,
TAG_STOPLOSS, TAG_STATUS }, new int[] {
R.id.Script, R.id.BuySell, R.id.TradeSell,
R.id.Target, R.id.StopLoss, R.id.Status });
setListAdapter(adapter);
}
如何解决这个错误?这是整个 class 代码:
public class Commodities extends ListFragment {
private String URL_ITEMS = "http://ourvadodara.ddns.net/NeptuneFinsol/commodities_fetch.php";
private static final String TAG_COMMODITIES = "commodities";
private static final String TAG_DATE = "date";
private static final String TAG_SCRIPT = "script";
private static final String TAG_BUYSELL = "buysell";
private static final String TAG_TRADESELL = "tradesell";
private static final String TAG_TARGET = "target";
private static final String TAG_STOPLOSS = "stoploss";
private static final String TAG_STATUS = "status";
JSONArray matchFixture = null;
ArrayList<HashMap<String, String>> matchFixtureList = new ArrayList<HashMap<String, String>>();
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.activity_bank__details,
container, false);
new GetFixture().execute();
return rootView;
}
private class GetFixture extends AsyncTask<Void, Void, Void> {
@Override
protected void onPreExecute() {
super.onPreExecute();
}
@Override
protected Void doInBackground(Void... arg) {
ServiceHandler serviceClient = new ServiceHandler();
Log.d("url: ", "> " + URL_ITEMS);
String json = serviceClient.makeServiceCall(URL_ITEMS,
ServiceHandler.GET);
// print the json response in the log
Log.d("Get match fixture response: ", "> " + json);
if (json != null) {
try {
Log.d("try", "in the try");
JSONObject jsonObj = new JSONObject(json);
Log.d("jsonObject", "new json Object");
// Getting JSON Array node
matchFixture = jsonObj.getJSONArray(TAG_COMMODITIES);
Log.d("json aray", "user point array");
int len = matchFixture.length();
Log.d("len", "get array length");
for (int i = 0; i < matchFixture.length(); i++) {
JSONObject c = matchFixture.getJSONObject(i);
String Script = c.getString(TAG_SCRIPT);
Log.d("Script", Script);
String BuySell = c.getString(TAG_BUYSELL);
Log.d("BuySell", BuySell);
String TradeSell = c.getString(TAG_TRADESELL);
Log.d("TradeSell", TradeSell);
String Target = c.getString(TAG_TARGET);
Log.d("Target", Target);
String StopLoss = c.getString(TAG_STOPLOSS);
Log.d("StopLoss", StopLoss);
String Status = c.getString(TAG_STATUS);
Log.d("Status", Status);
// hashmap for single match
HashMap<String, String> matchFixture = new HashMap<String, String>();
// adding each child node to HashMap key => value
matchFixture.put(TAG_SCRIPT, Script);
matchFixture.put(TAG_BUYSELL, BuySell);
matchFixture.put(TAG_TRADESELL, TradeSell);
matchFixture.put(TAG_TARGET, Target);
matchFixture.put(TAG_STOPLOSS, StopLoss);
matchFixture.put(TAG_STATUS, Status);
matchFixtureList.add(matchFixture);
}
} catch (JSONException e) {
Log.d("catch", "in the catch");
e.printStackTrace();
}
} else {
Log.e("JSON Data", "Didn't receive any data from server!");
}
return null;
}
@Override
protected void onPostExecute(Void result) {
super.onPostExecute(result);
ListAdapter adapter = new SimpleAdapter(Commodities.this,
matchFixtureList, R.layout.list_item, new String[] {
TAG_SCRIPT, TAG_BUYSELL, TAG_TRADESELL, TAG_TARGET,
TAG_STOPLOSS, TAG_STATUS }, new int[] {
R.id.Script, R.id.BuySell, R.id.TradeSell,
R.id.Target, R.id.StopLoss, R.id.Status });
setListAdapter(adapter);
}
}
}
由于 Commodities
是 ListFragment
,您不能将 this
用作 Context
。
尝试使用 Commodities.this.getActivity()
而不是 Commodities.this
ListAdapter adapter = new SimpleAdapter(Commodities.this.getActivity(),
matchFixtureList, R.layout.list_item, new String[] {
TAG_SCRIPT, TAG_BUYSELL, TAG_TRADESELL, TAG_TARGET,
TAG_STOPLOSS, TAG_STATUS }, new int[] {
R.id.Script, R.id.BuySell, R.id.TradeSell,
R.id.Target, R.id.StopLoss, R.id.Status });
这里你需要的Context
就是包含这个ListFragment
的Activity
。
从the documentation开始,构造函数的第一个参数为:
context The context where the View associated with this SimpleAdapter
is running
另外需要注意的是调用setListAdapter()
:
时需要使用Commodities.this
//setListAdapter(adapter); //won't work
Commodities.this.setListAdapter(adapter); //use this instead
这是因为setListAdapter()
这里是ListFragment
的一个方法,see documentation.
错误投入 onPostExecute()
:
The constructor SimpleAdapter(Commodities, ArrayList>, int, String[], int[]) is undefined
问题出在我的 AsyncTask
的 onPostExecute()
中:
@Override
protected void onPostExecute(Void result) {
super.onPostExecute(result);
ListAdapter adapter = new SimpleAdapter(Commodities.this,
matchFixtureList, R.layout.list_item, new String[] {
TAG_SCRIPT, TAG_BUYSELL, TAG_TRADESELL, TAG_TARGET,
TAG_STOPLOSS, TAG_STATUS }, new int[] {
R.id.Script, R.id.BuySell, R.id.TradeSell,
R.id.Target, R.id.StopLoss, R.id.Status });
setListAdapter(adapter);
}
如何解决这个错误?这是整个 class 代码:
public class Commodities extends ListFragment {
private String URL_ITEMS = "http://ourvadodara.ddns.net/NeptuneFinsol/commodities_fetch.php";
private static final String TAG_COMMODITIES = "commodities";
private static final String TAG_DATE = "date";
private static final String TAG_SCRIPT = "script";
private static final String TAG_BUYSELL = "buysell";
private static final String TAG_TRADESELL = "tradesell";
private static final String TAG_TARGET = "target";
private static final String TAG_STOPLOSS = "stoploss";
private static final String TAG_STATUS = "status";
JSONArray matchFixture = null;
ArrayList<HashMap<String, String>> matchFixtureList = new ArrayList<HashMap<String, String>>();
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.activity_bank__details,
container, false);
new GetFixture().execute();
return rootView;
}
private class GetFixture extends AsyncTask<Void, Void, Void> {
@Override
protected void onPreExecute() {
super.onPreExecute();
}
@Override
protected Void doInBackground(Void... arg) {
ServiceHandler serviceClient = new ServiceHandler();
Log.d("url: ", "> " + URL_ITEMS);
String json = serviceClient.makeServiceCall(URL_ITEMS,
ServiceHandler.GET);
// print the json response in the log
Log.d("Get match fixture response: ", "> " + json);
if (json != null) {
try {
Log.d("try", "in the try");
JSONObject jsonObj = new JSONObject(json);
Log.d("jsonObject", "new json Object");
// Getting JSON Array node
matchFixture = jsonObj.getJSONArray(TAG_COMMODITIES);
Log.d("json aray", "user point array");
int len = matchFixture.length();
Log.d("len", "get array length");
for (int i = 0; i < matchFixture.length(); i++) {
JSONObject c = matchFixture.getJSONObject(i);
String Script = c.getString(TAG_SCRIPT);
Log.d("Script", Script);
String BuySell = c.getString(TAG_BUYSELL);
Log.d("BuySell", BuySell);
String TradeSell = c.getString(TAG_TRADESELL);
Log.d("TradeSell", TradeSell);
String Target = c.getString(TAG_TARGET);
Log.d("Target", Target);
String StopLoss = c.getString(TAG_STOPLOSS);
Log.d("StopLoss", StopLoss);
String Status = c.getString(TAG_STATUS);
Log.d("Status", Status);
// hashmap for single match
HashMap<String, String> matchFixture = new HashMap<String, String>();
// adding each child node to HashMap key => value
matchFixture.put(TAG_SCRIPT, Script);
matchFixture.put(TAG_BUYSELL, BuySell);
matchFixture.put(TAG_TRADESELL, TradeSell);
matchFixture.put(TAG_TARGET, Target);
matchFixture.put(TAG_STOPLOSS, StopLoss);
matchFixture.put(TAG_STATUS, Status);
matchFixtureList.add(matchFixture);
}
} catch (JSONException e) {
Log.d("catch", "in the catch");
e.printStackTrace();
}
} else {
Log.e("JSON Data", "Didn't receive any data from server!");
}
return null;
}
@Override
protected void onPostExecute(Void result) {
super.onPostExecute(result);
ListAdapter adapter = new SimpleAdapter(Commodities.this,
matchFixtureList, R.layout.list_item, new String[] {
TAG_SCRIPT, TAG_BUYSELL, TAG_TRADESELL, TAG_TARGET,
TAG_STOPLOSS, TAG_STATUS }, new int[] {
R.id.Script, R.id.BuySell, R.id.TradeSell,
R.id.Target, R.id.StopLoss, R.id.Status });
setListAdapter(adapter);
}
}
}
由于 Commodities
是 ListFragment
,您不能将 this
用作 Context
。
尝试使用 Commodities.this.getActivity()
而不是 Commodities.this
ListAdapter adapter = new SimpleAdapter(Commodities.this.getActivity(),
matchFixtureList, R.layout.list_item, new String[] {
TAG_SCRIPT, TAG_BUYSELL, TAG_TRADESELL, TAG_TARGET,
TAG_STOPLOSS, TAG_STATUS }, new int[] {
R.id.Script, R.id.BuySell, R.id.TradeSell,
R.id.Target, R.id.StopLoss, R.id.Status });
这里你需要的Context
就是包含这个ListFragment
的Activity
。
从the documentation开始,构造函数的第一个参数为:
context The context where the View associated with this SimpleAdapter is running
另外需要注意的是调用setListAdapter()
:
Commodities.this
//setListAdapter(adapter); //won't work
Commodities.this.setListAdapter(adapter); //use this instead
这是因为setListAdapter()
这里是ListFragment
的一个方法,see documentation.