我想把JSON放到ListView
。我從http://api.androidhive.info/contacts/獲取數據(僅使用名字字段),我能夠讓他們到陣列,但即時通訊無法把它們放進列表,
JSON在android中的列表視圖
[注] 但是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
您是否設置了日誌來檢查數據是否到來?如果數據來了,那麼你可以改變列表的背景爲黑色,因爲有時textview是白色的,文字看起來是不可見的 –
是的,我可以得到數據,甚至把它們放入數組中,logcat:02-06 07:37 :05.895:D/jidla:(1487):[{name = Ravi Tamada},{name = Johnny Depp},{name = Leonardo Dicaprio},{name = John Wayne},{name = Angelina Jolie},{name =名字=阿德勒} {姓名=休傑克曼},{名字=威爾史密斯},{名字=克林特伊斯特伍德},{名字= Barack奧巴馬},{名字=凱特Winslet},{名字=阿姆} ] 這是日誌陣列,改變顏色沒有幫助 – user3185930
然後你可以嘗試通過創建自己的自定義適配器? –