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?
Try this : In the onPostExecute create your list of maps
In the getView, retreive your list Item using getItem(position), retreive your url from it
}