【问题标题】:How to update the image of each ImageView of a ListView in Android with new image for each one如何在Android中使用新图像更新Android中ListView的每个ImageView的图像
【发布时间】:2016-05-02 22:28:00
【问题描述】:

我首先获取文本并将它们添加到 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?
        }
    }

【问题讨论】:

  • 真的,老实说不要使用AsyncTask 使用Callback PatternRunnable 并且因为你想更新UI Thread 使用Holder 类将图像添加到‍‍UI
  • 您绝对应该考虑使用图像加载库,例如 Picasso。它确实简化了您丢失图像的工作并处理您的代码将出现的看不见的错误。关于在适配器中显示图像,这取决于您使用什么视图来显示它们,但请按照本教程寻求建议:developer.android.com/training/material/lists-cards.html
  • 我到处都读到,只需将新数据添加到适配器并通知它,但我无法实现它。你能帮帮我吗?
  • @chRyNaN 我可以轻松获取所有图像,但无法在 ListView 中更新它们。假设有 3 个项目,那么我将获得 3 个图像,我想分别更新它们。

标签: java android listview android-arrayadapter android-imageview


【解决方案1】:

在您的第二个 AsyncTask 中,在循环中执行以下操作:

  1. 在 image1 中获取图像后对其进行解析。
  2. 从适配器获取 i 处的项目。
  3. 从该项目中找到具有其 ID 的视图。
  4. 在上面设置图片。

完成。试试吧,希望对你有用。

【讨论】:

    【解决方案2】:

    为此,您需要实现一个自定义 ArrayAdapter。在您的第一个 asyncTask 中,您使用您拥有的文本数据设置适配器,但您使用的适配器的格式为

    ArrayAdapter(Context context, int resource, T[] objects)
    

    在您的情况下, T 是字符串(据我所知)。但是您希望这个 T 是一个自定义对象,其中还包含一个图像。您可以通过构建自己的自定义适配器来做到这一点。看看this的答案。

    或者另一种方式是你的 T 对象应该是这样的。

    CustomObject customObject = new CustomObject(text, image). 
    

    尝试创建这些对象的列表,然后将其传递给适配器,而不是仅传递文本数组。但为此,您必须等到所有图像都加载完毕。

    一个更好的主意是使用Picasso。并实现一个自定义适配器。你目前的方法有很大缺陷。

    【讨论】:

    • 我的图像在数据库而不是网络服务器中,这就是为什么我不能使用 Picasso。你能写第二个异步任务的左边部分吗,因为我真的不能这样做。
    猜你喜欢
    • 1970-01-01
    • 2019-04-24
    • 2014-11-27
    • 1970-01-01
    • 2023-01-18
    • 1970-01-01
    • 2011-08-12
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多