Li Kevin
Li Kevin

Reputation: 83

Android Asyn loading ImageView in ListVIew, image changed auto and not correct

I have a ListView to show a photo gallery with short title(ImageView + TexiView). I used AsyncTask to do the download duty. But the problem is sometimes it will show wrong image then changed suddenly(may happen many times). For example: IA B C will show in ListView, image m1 m2 m3 for ABC respectively. when I run the application, the imageView for A may display m1,m2 or m3 and change frequently. Here is my code:

DownClass:

  public class DownloadImageTask extends AsyncTask<String, Void, Bitmap> {
    ImageView bmImage = null;

    public DownloadImageTask(ImageView bmImage) {
        this.bmImage = bmImage;
    }
    protected Bitmap doInBackground(String... urls) {
        String urldisplay = urls[0];
        Bitmap mIcon11 = null;
        try {
            InputStream in = new java.net.URL(urldisplay).openStream();
            mIcon11 = BitmapFactory.decodeStream(in);
        } catch (Exception e) {
            Log.e("Error", e.getMessage());
            e.printStackTrace();
        }
        return mIcon11;
    }
    protected void onPostExecute(Bitmap result) {
        if(result == null){
            bmImage.setImageResource(R.drawable.loading);
          }else{
            bmImage.setImageBitmap(result);
            }
    }
}

Adapter for ListView:

    public View getView(int position, View convertView, ViewGroup parent){
    item item1 = getItem(position);
    if (convertView == null) {
        convertView = LayoutInflater.from(getContext()).inflate(R.layout.item1, parent, false);
    }
    ImageView iv = (ImageView) convertView.findViewById(R.id.img);
    TextView ttv = (TextView) convertView.findViewById(R.id.p_title);
    TextView itv = (TextView) convertView.findViewById(R.id.p_price);
    /**
    *Download Image
    */
    String img_url;
    img_url = item1.getImg_url();
    new DownloadImageTask((ImageView) convertView.findViewById(R.id.img))
            .execute(img_url);

    ttv.setText(item1.getTitle());
    itv.setText("$"+item1.getPrice());
    return convertView;
}

Activity:

 protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.show_promotion2);

    gridView = (GridView) findViewById(R.id.gridview);
    getPromotionByRequest(previousPage, data);//Get Data for items

    itemAdapter = new promotionAdapter(this, items);
    //progress..........
    gridView.setAdapter(itemAdapter);
    setupListViewListener();
}

Upvotes: 0

Views: 336

Answers (4)

Li Kevin
Li Kevin

Reputation: 83

Thank you for your answers, unfortunately problem still happen. Finally, I did solved it by use "Picasso" in each adapter, such powerful

Upvotes: 1

brahmy adigopula
brahmy adigopula

Reputation: 617

create view holder class in getview() method like bellow.

ViewHolder holder = new ViewHolder();

and later add view to holder like bellow code

holder.imageviewname= (ImageView) convertView.findViewById(R.id.listitem_image);
holder.textviewname = (TextView) convertView.findViewById(R.id.listitem_text);

for more info refer doc https://developer.android.com/training/improving-layouts/smooth-scrolling.html

Upvotes: 0

AliveDev
AliveDev

Reputation: 3

public View getView(int position, View convertView, ViewGroup parent){
item item1 = getItem(position);
ViewHolder vh = null;
if (convertView == null) {
    convertView = LayoutInflater.from(getContext()).inflate(R.layout.item1, parent, false);
    vh.iv = (ImageView) convertView.findViewById(R.id.img);
    vh.ttv = (TextView) convertView.findViewById(R.id.p_title);
    vh.itv = (TextView) convertView.findViewById(R.id.p_price);
    convertView.setTag(vh);
}else{
    vh = (ViewHoldler)convertView.getTag();
}

/**
*Download Image
*/
String img_url;
img_url = item1.getImg_url();
new DownloadImageTask((ImageView) convertView.findViewById(R.id.img))
        .execute(img_url);

ttv.setText(item1.getTitle());
itv.setText("$"+item1.getPrice());
return convertView;

}

public class ViewHolder {
  public ImageView iv;
  public TextView ttv;
  public TextView itv;
}

Maybe it is not correct,you can try...I've been a long time without a listview

Upvotes: 0

brahmy adigopula
brahmy adigopula

Reputation: 617

try with viewholder instesd of getview

public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
LayoutInflater inflater = LayoutInflater.from(getActivity());
View view = inflater.inflate(android.R.layout.list_item_recyclerView, parent, false);
return new ViewHolder(view);
}

orelse try with this in getview also create layoutinflator:

LayoutInflater  inflater = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);

and create another view reference:
then inflate the child views:

View v = convertView;
v = mInflater.inflate(R.layout.layout_namehere, parent, false);
TextView name = (TextView) view.findViewById(R.id.name);

Upvotes: 0

Related Questions