我首先获取文本并将它们添加到ListView(这部分正在运行)。 然后,对于ListView中的每个项目,我将获取图像,并需要将此图像添加到已在布局中定义的每个项目的ImageView中。 我能够获取所有图像,但不知道如何更新ListView。
//AsyncTask For texts.
private class pgData extends AsyncTask<String, String, JSONArray> {
protected JSONArray doInBackground(String... params) {
publishProgress("Starting");
String result=null;
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://xxx.xxx/xxx.php");
HttpResponse response = null;
HttpEntity httpentity =null;
JSONArray t=null;
try{
response= httpclient.execute(httppost);
httpentity=response.getEntity();
result= EntityUtils.toString(httpentity);
t=new JSONArray(result);
}catch (Exception e){
publishProgress(e.toString());
this.cancel(true);
}
return t;
}
protected void onProgressUpdate(String... i) {
Toast.makeText(getBaseContext(), i[0], Toast.LENGTH_SHORT).show();
}
protected void onPostExecute(JSONArray result) {
String x[];
l = result.length();
pg_data = new pg[l];
JSONObject jsonObject;
for (int i = 0; i < l; i++) {
try {
jsonObject = result.getJSONObject(i);
x[0] = jsonObject.getString("xxx");
x[1]=jsonObject.getString("xxx");
x[2] = jsonObject.getString("xxxx");
x[3] = jsonObject.getString("rentMonthly");
pg_data[i] = new pg(
x[0],
x[1],
x[2],
x[3]
);
jdata.add(xxx_data[i]);
} catch (Exception e) {
Toast.makeText(getBaseContext(), e.toString(), Toast.LENGTH_LONG).show();
}
}
adapter = new pgAdapter(second.this,
R.layout.pg,
xxx_data);
lv = (ListView) findViewById(R.id.listView);
lv.setAdapter(adapter);
new xxImage().execute(); //exexuting async task for fetching images.
}
}
//Async Task for fetching images
private class xxImage extends AsyncTask<String, String, JSONArray> {
protected JSONArray doInBackground(String... params) {
publishProgress("Getting Images");
String result=null;
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://xxx.xx/xx.php");
HttpResponse response = null;
HttpEntity httpentity =null;
JSONArray t=null;
try{
response= httpclient.execute(httppost);
httpentity=response.getEntity();
result= EntityUtils.toString(httpentity);
t=new JSONArray(result);
}catch (Exception e){
publishProgress(e.toString());
this.cancel(true);
}
return t;
}
protected void onProgressUpdate(String... i) {
Toast.makeText(getBaseContext(), i[0], Toast.LENGTH_SHORT).show();
}
protected void onPostExecute(JSONArray result) {
String image1;
l = result.length();
xx_image = new xxImage[l];
JSONObject jsonObject;
for (int i = 0; i < l; i++) {
try {
jsonObject = result.getJSONObject(i);
image1 = "\n" + jsonObject.getString("image1") + "\n";
xx_image[i] = new xxImage(
image1
);
jimage.add(xx_image[i]);
} catch (Exception e) {
Toast.makeText(getBaseContext(), e.toString(), Toast.LENGTH_LONG).show();
}
}
//I got all the Images in 'jimage'. Now how to update the ListView from here?
}
}
答案 0 :(得分:0)
您需要实现自定义ArrayAdapter才能执行此操作。首先在asyncTask中使用您拥有的文本数据设置适配器,但您使用的适配器的格式为
ArrayAdapter(Context context, int resource, T[] objects)
在你的情况下T是String(据我所见)。但是你希望这个T是一个包含图像的自定义对象。您可以通过构建自己的自定义适配器来完成此操作看看this回答。
或者另一种方式是你的T对象应该是这样的。
CustomObject customObject = new CustomObject(text, image).
尝试创建这些对象的列表,然后将这些对象传递给适配器,而不是只传递文本数组。但为此你必须等到所有图像都被加载。
更好的想法是使用Picasso。并实现自定义适配器。你目前的方法存在很大缺陷。
答案 1 :(得分:0)
在你的第二个AsyncTask中,在循环中执行以下操作:
完成。试试,希望它适合你。