如何使用每个新图像更新 Android 中 ListView 的每个 ImageView 的图像
How to update the image of each ImageView of a ListView in Android with new image for each one
我首先获取文本并将它们添加到 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?
}
}
您需要为此实现自定义 ArrayAdapter。在您的第一个 asyncTask 中,您使用您拥有的文本数据设置适配器,但您正在使用的适配器的形式为
ArrayAdapter(Context context, int resource, T[] objects)
在你的例子中,T 是字符串(据我所知)。但是您希望此 T 成为还包含图像的自定义对象。您可以通过构建自己的自定义适配器来实现这一点。查看 this 答案。
或者另一种方式是你的 T 对象应该是这样的。
CustomObject customObject = new CustomObject(text, image).
尝试创建这些对象的列表,然后将其传递给适配器,而不是仅传递文本数组。但是为此你必须等到所有图像都加载完毕。
更好的办法是使用 Picasso。并实现自定义适配器。你目前的做法是非常有缺陷的。
在您的第二个 AsyncTask 中,在循环中执行以下操作:
- 在 image1 中获取图像后对其进行解析。
- 从适配器获取 i 处的项目。
- 从该项目中找到具有其 ID 的视图。
- 在上面设置图像。
完成。试试吧,希望对你有用。
我首先获取文本并将它们添加到 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?
}
}
您需要为此实现自定义 ArrayAdapter。在您的第一个 asyncTask 中,您使用您拥有的文本数据设置适配器,但您正在使用的适配器的形式为
ArrayAdapter(Context context, int resource, T[] objects)
在你的例子中,T 是字符串(据我所知)。但是您希望此 T 成为还包含图像的自定义对象。您可以通过构建自己的自定义适配器来实现这一点。查看 this 答案。
或者另一种方式是你的 T 对象应该是这样的。
CustomObject customObject = new CustomObject(text, image).
尝试创建这些对象的列表,然后将其传递给适配器,而不是仅传递文本数组。但是为此你必须等到所有图像都加载完毕。
更好的办法是使用 Picasso。并实现自定义适配器。你目前的做法是非常有缺陷的。
在您的第二个 AsyncTask 中,在循环中执行以下操作:
- 在 image1 中获取图像后对其进行解析。
- 从适配器获取 i 处的项目。
- 从该项目中找到具有其 ID 的视图。
- 在上面设置图像。
完成。试试吧,希望对你有用。