I have a list that pulls image urls from a database. Once it gets the urls, I use the following code to store them on drawables:
private Drawable LoadImageFromWebOperations(String url){
try{
InputStream is = (InputStream) new URL(url).getContent();
Drawable d = Drawable.createFromStream(is, "src name");
return d;
}catch (Exception e) {
System.out.println("Exc="+e);
return null;
}
}
then I save the drawable into a linked list and continue on pulling the other image urls, once this is done the pictures get displayed. The problem that I am having is that it pulls the urls fine but when it gets displayed on the app the first two pictures are good but when i scroll to see the third one it displays the picture of the first one, any thoughts on this??
Here is the code for the getview and the custom list:
private class RowData {
protected int mId;
protected String mTitle;
RowData(int id,String title){
mId=id;
mTitle = title;
}
@Override
public String toString() {
return mId+" "+mTitle+" ";
}
}
private class CustomAdapter extends ArrayAdapter<RowData> {
public CustomAdapter(Context context, int resource,
int textViewResourceId, List<RowData> objects) {
super(context, resource, textViewResourceId, objects);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder = null;
TextView title = null;
TextView date = null;
TextView detail = null;
ImageView i11=null;
String postDate;
RowData rowData= getItem(position);
if(null == convertView){
convertView = mInflater.inflate(R.layout.newspg, null);
holder = new ViewHolder(convertView);
convertView.setTag(holder);
}
holder = (ViewHolder) convertView.getTag();
title = holder.gettitle();
title.setText(rowData.mTitle);
postDate = getpostDate(news, rowData.mTitle );
Log.i("Date",postDate);
date = holder.getDate();
date.setText(postDate);
i11=holder.getImage(position);
Log.i("pos",Integer.toString(position));
//i11.setImageResource(imgid[0]);
return convertView;
}
private class ViewHolder {
private View mRow;
private TextView title = null;
private TextView date = null;
private TextView detail = null;
private ImageView i11=null;
public ViewHolder(View row) {
mRow = row;
}
public TextView gettitle() {
if(null == title){
title = (TextView) mRow.findViewById(R.id.title);
}
return title;
}
public TextView getDate() {
if(null == date){
date = (TextView) mRow.findViewById(R.id.date);
}
return date;
}
public ImageView getImage(int position) {
if(null == i11){
i11 = (ImageView) mRow.findViewById(R.id.img);
i11.setImageDrawable(newArtists.get(position).getNewsPic());
}
return i11;
}
}
}
This is happening because the listview reuse the previous rows (with the views including your pictures) to build the new ones. So you have to take care about that. For example, you can hide the ImageView until you get the good picture to put it in the ImageView and then make it visible.