我正在开发一个应该从Web服务获取JSON响应并编写listview(图像和文本)中的每个元素的应用程序,我确实使用AsyncTask来获取HTTP响应以从Web服务检索数据并显示它们在TextViews和ImageViews中。但是当我尝试显示listview时,它不会显示任何内容(文本和图像)。
logcat的:
03-18 09:13:36.329: I/Process(1478): Sending signal. PID: 1478 SIG: 9
03-18 09:15:16.279: I/dalvikvm-heap(1547): Grow heap (frag case) to 4.198MB for 1127536-byte allocation
03-18 09:15:17.179: I/Choreographer(1547): Skipped 46 frames! The application may be doing too much work on its main thread.
03-18 09:15:17.619: I/Choreographer(1547): Skipped 30 frames! The application may be doing too much work on its main thread.
03-18 09:15:19.039: I/Choreographer(1547): Skipped 131 frames! The application may be doing too much work on its main thread.
03-18 09:15:25.309: I/Choreographer(1547): Skipped 40 frames! The application may be doing too much work on its main thread.
03-18 09:15:27.749: I/Choreographer(1547): Skipped 79 frames! The application may be doing too much work on its main thread.
03-18 09:15:32.999: I/Choreographer(1547): Skipped 34 frames! The application may be doing too much work on its main thread.
03-18 09:15:47.619: I/dalvikvm-heap(1547): Grow heap (frag case) to 5.085MB for 498256-byte allocation
03-18 09:15:48.299: I/Choreographer(1547): Skipped 30 frames! The application may be doing too much work on its main thread.
03-18 09:17:02.299: I/Choreographer(1547): Skipped 52 frames! The application may be doing too much work on its main thread.
03-18 09:18:28.929: I/Choreographer(1547): Skipped 32 frames! The application may be doing too much work on its main thread.
MainActivity :
public class MainActivity extends Activity {
private ProgressDialog pDialog;
// URL to get contacts JSON
private static String url = "MY_URL";
// JSON Node names
public static final String TAG_CONTACTS = "contacts";
public static final String TAG_NAME = "name";
public static final String TAG_PRODUCT_IMG = "product_img";
ArrayList<HashMap<String, String>> arraylist;
JSONObject JOBJ;
JSONArray contacts = null;
ListViewAdapter adapter;
ListView lv;
public JSONArray jsonStr;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
lv = (ListView) findViewById(R.id.list);
new GetContacts().execute();
}
private class GetContacts extends AsyncTask<Void, Void, Void> {
@Override
protected void onPreExecute() {
super.onPreExecute();
pDialog = new ProgressDialog(MainActivity.this);
pDialog.setMessage("Please wait...");
pDialog.setCancelable(false);
pDialog.show();
}
@Override
protected Void doInBackground(Void... arg0) {
arraylist = new ArrayList<HashMap<String, String>>();
JOBJ = JSONfunctions
.getJSONfromURL("http://www.nopaltechnologies.com/restaurant/products.php/");
try {
jsonStr = JOBJ.getJSONArray(TAG_CONTACTS);
// Getting JSON Array node
// looping through All Contacts
for (int i = 0; i < jsonStr.length(); i++) {
// tmp hashmap for single contact
HashMap<String, String> contact = new HashMap<String, String>();
JOBJ = jsonStr.getJSONObject(i);
// adding each child node to HashMap key => value
contact.put(TAG_PRODUCT_IMG, JOBJ.getString("product_img"));
contact.put(TAG_NAME, JOBJ.getString("name"));
// adding contact to contact list
arraylist.add(contact);
}
} catch (JSONException e) {
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(Void result) {
super.onPostExecute(result);
adapter = new ListViewAdapter(MainActivity.this, arraylist);
lv.setAdapter(adapter);
pDialog.dismiss();
}
}
}