Item in getView() method returns always the last one

400 Views Asked by At

I created a json parse with a simple adapter to shows the result. Everything works fine but i need to retrieve an url to convert in bitmap. It's a listview, so the url isn't one but could be one,two,three or more. For each url i need create a bitmap so i can show an image for each element of the list.. Actually, before the getView() method part i can display in a log the urls correctly. As soon as i enter in the getView() the only url i can show is the last one i parse from json. This is the onPostExecute() part i use to do this:

@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();
         }
        }

So in this way the imageview set only one image for each item of the list (using only one url). I'm inside a loop..why doesn't take all urls?

3

There are 3 best solutions below

0
On BEST ANSWER

Try this : In the onPostExecute create your list of maps

@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();
    }
}

In the getView, retreive your list Item using getItem(position), retreive your url from it

@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;

}

4
On

In your code, you use the local variable (urlImage), you need to put your URL in your data and get the URL from getItem(int position) (http://developer.android.com/reference/android/widget/SimpleAdapter.html#getItem(int))

URL url = new URL(urlImage);

1
On

You seem to be caching your views outside of getView() instead of using the convertView passed to you.

Romain Guy explains it here https://www.youtube.com/watch?v=wDBM6wVEO70