getView()方法中的项始终返回最后一项

时间:2014-11-27 14:07:16

标签: java android json android-listview getview

我用一个简单的适配器创建了一个json解析来显示结果。一切正常但我需要检索一个URL来转换位图。它是一个列表视图,因此网址不是一个,但可以是一个,两个,三个或更多。对于每个网址,我需要创建一个位图,这样我就可以显示列表中每个元素的图像。实际上,在getView()方法部分之前,我可以正确地在日志中显示网址。一旦我进入getView(),我可以显示的唯一网址是我从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)。我在一个循环中..为什么不采取所有网址?

3 个答案:

答案 0 :(得分: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)检索列表Item,从中检索你的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;

}

答案 1 :(得分:0)

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

  

网址url =新网址(urlImage);

答案 2 :(得分:0)

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

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