【问题标题】:Item in getView() method returns always the last onegetView() 方法中的项目总是返回最后一个
【发布时间】:2014-11-27 14:07:16
【问题描述】:

我用一个简单的适配器创建了一个 json 解析来显示结果。一切正常,但我需要检索要在位图中转换的 url。这是一个列表视图,所以 url 不是一个,而是一个、两个、三个或更多。对于每个 url,我需要创建一个位图,这样我就可以为列表的每个元素显示一个图像。实际上,在 getView() 方法部分之前,我可以在日志中正确显示 url。一旦我进入 getView(),我唯一可以显示的 url 就是我从 json 解析的最后一个。这是我用来执行此操作的onPostExecute() 部分:

@Override
          protected void onPostExecute(JSONObject json) {
              pDialog.dismiss();
          try {
             // Getting JSON Array from URL
              rating = json.getJSONArray(TAG_ITEM);

             for(int i = 0; i < rating.length(); i++)
             {
                 JSONObject c = rating.getJSONObject(i);
                 // Storing  JSON item in a Variable
                 String name = c.getString(TAG_NOME);
                 String commento = c.getString(TAG_COMMENTO);
                 String valore = c.getString(TAG_VALORE);
                 urlImage = c.getString(TAG_URLIMAGE);
                 String timestamp = c.getString(TAG_TIMESTAMP);
                 Log.i("Url immagine", urlImage);// here it shows 2 url and it's ok

                 // Adding value HashMap key => value
                 HashMap<String, String> map = new HashMap<String, String>();
                 map.put(TAG_NOME, name);
                 map.put(TAG_COMMENTO, commento);
                 map.put(TAG_VALORE, "Voted: "+valore);
                 map.put(TAG_TIMESTAMP, timestamp);
                 oslist.add(map);

                 ListAdapter adapter = new SimpleAdapter(RatingDetailsActivty.this, oslist, R.layout.list_rating_item,
                     new String[] 
                             { TAG_NOME,
                               TAG_COMMENTO,
                               TAG_VALORE,
                               TAG_URLIMAGE,
                               TAG_TIMESTAMP
                              }, new int[] 
                                      {
                                        R.id.nome,
                                        R.id.commento, 
                                        R.id.valore, 
                                        R.id.timestamp
                                    })

                 {
                        @Override
                        public View getView(int position, View convertView, ViewGroup parent) {
                            View v = super.getView(position, convertView, parent);

                            ImageView profileImage = (ImageView) v.findViewById(R.id.profile_img);
                            try {
                                URL url = new URL(urlImage);
                                imageProfilo = BitmapFactory.decodeStream(url.openConnection().getInputStream());
                                Log.i("Url immagine", urlImage); // here there is only last one

                            } catch (IOException e) {
                                // TODO Auto-generated catch block
                                e.printStackTrace();
                            }
                            if (imageProfilo != null) {
                                profileImage.setImageBitmap(imageProfilo);
                            } else {
                                profileImage.setImageDrawable(v.getResources() .getDrawable(R.drawable.welcome));
                            }
                            return v;
                        }

                 };
                 list.setAdapter(adapter);
            }
         } catch (JSONException e) {
           e.printStackTrace();
         }
        }

因此,以这种方式,imageview 为列表的每个项目只设置了一个图像(仅使用一个 url)。我在一个循环中..为什么不采用所有网址?

【问题讨论】:

    标签: java android json android-listview getview


    【解决方案1】:

    试试这个: 在 onPostExecute 创建地图列表

    @Override
    protected void onPostExecute(JSONObject json) {
    
        try {
            // Getting JSON Array from URL
            rating = json.getJSONArray(TAG_ITEM);
    
            for(int i = 0; i < rating.length(); i++)
            {
                JSONObject c = rating.getJSONObject(i);
                // Storing  JSON item in a Variable
                String name = c.getString(TAG_NOME);
                String commento = c.getString(TAG_COMMENTO);
                String valore = c.getString(TAG_VALORE);
                String imageUrl = c.getString(TAG_URLIMAGE);
                String timestamp = c.getString(TAG_TIMESTAMP);
                Log.i("Url immagine", imageUrl);// here it shows 2 url and it's ok
    
                // Adding value HashMap key => value
                HashMap<String, String> map = new HashMap<String, String>();
                map.put(TAG_NOME, name);
                map.put(TAG_COMMENTO, commento);
                map.put(TAG_IMAGEURL, imageUrl);
                map.put(TAG_VALORE, "Voted: "+valore);
                map.put(TAG_TIMESTAMP, timestamp);
                oslist.add(map);
    
                ListAdapter adapter = new SimpleAdapter(RatingDetailsActivty.this, oslist, R.layout.list_rating_item,
                        new String[]
                                { TAG_NOME,
                                        TAG_COMMENTO,
                                        TAG_VALORE,
                                        TAG_IMAGEURL,
                                        TAG_TIMESTAMP
                                }, new int[]
                        {
                                R.id.nome,
                                R.id.commento,
                                R.id.valore,
                                R.id.timestamp
                        });
            }
            list.setAdapter(adapter);
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }
    

    在 getView 中,使用 getItem(position) 检索您的列表项,从中检索您的 url

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
    View v = super.getView(position, convertView, parent);
    
    ImageView profileImage = (ImageView) v.findViewById(R.id.profile_img);
    try {
        HashMap<String, String> map = (HashMap<String, String>)getItem(position);
        URL url = new URL(map.get(TAG_IMAGEURL));
        imageProfilo = BitmapFactory.decodeStream(url.openConnection().getInputStream());
        Log.i("Url immagine", urlImage); // here there is only last one
    
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    if (imageProfilo != null) {
        profileImage.setImageBitmap(imageProfilo);
    } else {
        profileImage.setImageDrawable(v.getResources() .getDrawable(R.drawable.welcome));
    }
    return v;
    

    }

    【讨论】:

      【解决方案2】:

      在您的代码中,您使用局部变量 (urlImage),您需要将您的 URL 放入您的数据中并从 getItem(int position) (http://developer.android.com/reference/android/widget/SimpleAdapter.html#getItem(int)) 获取 URL >

      网址 url = 新网址(urlImage);

      【讨论】:

      • 对我来说,你需要清理你的代码: - 创建你自己的适配器来扩展 ArrayAdapter 例如(像这样你可以有你自己的 POJO 模型) - 为你的 POJO 创建一个模型(例如 post.java)所有需要提交的文件(URL、名称、评论)。 - 然后在您的活动/片段中,您获取数据(解析,用您的对象填充 arrayList)并将您的适配器链接到列表。更好的可读性是必不可少的 ;)
      • 您认为有必要将 simpleAdapter 更改为 ArrayAdapter 吗?如果可能的话,我会使用这种方式,因为我会改变很多东西..
      • SimpleAdapter 是为静态数据制作的,在您的情况下,您每次获得新数据时都重新创建一个适配器?如果您使用 ArrayAdapter,您可以添加、删除、替换任何数据并在数据发生更改时通知您的适配器 (yourAdapter.notifyDataSetChanged()),这种方式可以提高性能。
      【解决方案3】:

      您似乎在 getView() 之外缓存您的视图,而不是使用传递给您的 convertView。

      Romain Guy 在这里解释它https://www.youtube.com/watch?v=wDBM6wVEO70

      【讨论】:

      • ehm,好的,这太棒了,我当然会检查视频谢谢..但是你知道我现在如何修复我的代码吗?谢谢
      猜你喜欢
      • 1970-01-01
      • 2015-07-12
      • 2011-04-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-06-29
      • 1970-01-01
      • 2012-07-21
      相关资源
      最近更新 更多