【发布时间】:2014-03-03 09:12:13
【问题描述】:
我正在尝试将 JSON 放入 ListView。我正在从 http://api.androidhive.info/contacts/ 获取数据(仅使用名称字段)我能够将它们放入 array,但我无法将它们放入 list,
[注意]
但是ListView 为 13 个条目(名称的数量)精确地制作了 13 行,但这些行是空白的。
private class GetJidla extends AsyncTask<Void, Void, Void> {
@Override
protected void onPreExecute() {
super.onPreExecute();
// Showing progress dialog
pDialog = new ProgressDialog(TableMenuActivity.this);
pDialog.setMessage("Please wait...");
pDialog.setCancelable(false);
pDialog.show();
}
@Override
protected Void doInBackground(Void... arg0) {
// Creating service handler class instance
ServiceHandler sh = new ServiceHandler();
// Making a request to url and getting response
String jsonStr = sh.makeServiceCall(url, ServiceHandler.GET);
Log.d("Response: ", "> " + jsonStr);
if (jsonStr != null) {
try {
JSONObject jsonObj = new JSONObject(jsonStr);
// Getting JSON Array node
jidla = jsonObj.getJSONArray(TAG_CONTACTS);
// looping through All Contacts
for (int i = 0; i < jidla.length(); i++) {
JSONObject c = jidla.getJSONObject(i);
// String id = c.getString(TAG_ID);
String name = c.getString(TAG_NAME);
// tmp hashmap for single contact
HashMap<String, String> contact = new HashMap<String, String>();
// adding each child node to HashMap key => value
// contact.put(TAG_ID, id);
contact.put(TAG_NAME, name);
// adding contact to contact list
jidlaList.add(contact);
}
} catch (JSONException e) {
e.printStackTrace();
}
} else {
Log.e("ServiceHandler", "Couldn't get any data from the url");
}
return null;
}
@Override
protected void onPostExecute(Void result) {
super.onPostExecute(result);
// Dismiss the progress dialog
if (pDialog.isShowing())
pDialog.dismiss();
/**
* Updating parsed JSON data into ListView
* */
ListAdapter adapter = new SimpleAdapter(
TableMenuActivity.this, jidlaList,
android.R.layout.simple_list_item_1, new String[] {TAG_NAME}, new int[] {android.R.id.list,
});
menu = (ListView)findViewById(android.R.id.list);
menu.setAdapter(adapter);
}
列表在这里
<ListView
android:id="@android:id/list"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="5dip" >
</ListView>
i need it to be on one screen, in one activity here is the image, the brown lsit is where i need it
【问题讨论】:
-
你是否设置了一个日志来检查数据是否到来?如果数据即将到来,那么您可以将列表视图的背景更改为黑色,因为有时文本视图是白色的并且文本似乎不可见
-
是的,我什至可以将数据放入数组中,logcat: 02-06 07:37:05.895: D/jidla:(1487): > [{name=Ravi Tamada}, {name =约翰尼·德普},{name=莱昂纳多·迪卡普里奥},{name=约翰·韦恩},{name=安吉丽娜·朱莉},{name=Dido},{name=阿黛尔},{name=休·杰克曼},{name=威尔·史密斯}, {name=Clint Eastwood}, {name=Barack Obama}, {name=Kate Winslet}, {name=Eminem}] 这是数组的日志,改变颜色没有帮助
-
那您可以尝试创建自己的自定义适配器吗?