list view image is getting changed when scrolling

2019-07-20 10:09发布

问题:

I am parsing json data in a listview(imageview and textview).For first four items of listview the image and textview are correctly added but when i scroll the listview then the images in imageview is getting changed. here is my code..

adapter.java

 public class Adapter extends BaseAdapter {

private static final String TAG = "Adapter";
private Activity mActivity;
public ArrayList<Data> mObjects;

Context context;

static class ViewHolder {
    static ImageView icon;
    TextView title;
    TextView name;
    TextView review;
    DownloadImageTask mTask;
    String ab[];
    // DownloadImageTask1 mTask1;
    // ImageView photo;
}

public Adapter(Activity activity, Context context, ArrayList<Data> mObjects) {

    this.mActivity = (Activity) activity;
    this.context = context;
    this.mObjects = mObjects;

}

public void setObjects(ArrayList<Data> mObjects) {
    this.mObjects = mObjects;
}

@Override
public View getView(final int position, View convertView, ViewGroup parent) {

    final Data item = mObjects.get(position);
    View rowView = convertView;
    ViewHolder viewHolder;
    if (rowView == null) {
        LayoutInflater inflater = mActivity.getLayoutInflater();
        rowView = inflater.inflate(R.layout.item, parent, false);
        viewHolder = new ViewHolder();

        viewHolder.icon = (ImageView) rowView.findViewById(R.id.image);
        // viewHolder.photo = (ImageView) rowView.findViewById(R.id.photo);
        viewHolder.title = (TextView) rowView.findViewById(R.id.title);
        viewHolder.name = (TextView) rowView.findViewById(R.id.name);

        viewHolder.review = (TextView) rowView.findViewById(R.id.status);
        rowView.setTag(viewHolder);
    } 

    ViewHolder holder = (ViewHolder) rowView.getTag();

    holder.icon.setTag(position);

    holder.title.setText(item.getmTitle());
    // holder.name.setTag(position);
    holder.name.setText(item.getmConcatinate());

    holder.review.setText(item.getmreview());
    holder.icon.setBackgroundResource(R.drawable.ic_ab);
    holder.mTask = new DownloadImageTask(item.getmImageUrl(),

    holder.icon);
    if (!holder.mTask.isCancelled()) {
        holder.mTask.execute();
    }

    ViewHolder.icon.setTag(position);
    // }// update position because list view recycle

    ViewHolder.icon.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            Log.d("Parsing JSON Data", "Before user try122222"); // perform
            // Object tag= v.getTag();
            Log.d("Parsing JSON Data", "abc");

            int clickedPosition = (Integer) v.getTag();

            Data clickedItem = mObjects.get(clickedPosition);
            // Object vi=row.getTag();

            String title = clickedItem.getmConcatinate();
            String picture = clickedItem.getmImageUrl();

            Intent intent = new Intent(v.getContext(), LargeView.class);
            Log.d("Parsing JSON Data", "Before user try444444");
            intent.putExtra("data", title);

            intent.putExtra("image3", picture);

            v.getContext().startActivity(intent);
            Log.d("Parsing JSON Data", "Before user try7777");
        }
    });

    return rowView;
}

@Override
public int getCount() {

    return (this.mObjects.size());
}

@Override
public Object getItem(int position) {

    return (this.mObjects.get(position));
}

@Override
public long getItemId(int position) {

    return (position);
}

public AbsListView.RecyclerListener mRecyclerListener = new RecyclerListener() {

    public void onMovedToScrapHeap(View view) {
        ViewHolder viewHolder = (ViewHolder) view.getTag();
        DownloadImageTask imagetask = viewHolder.mTask;

        if (imagetask != null) {
            imagetask.cancel(true);
        }

    }

};

}

main.java

     @Override
protected void onCreate(Bundle savedInstanceState) {
    StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder()
            .detectAll().penaltyLog().build();
    StrictMode.setThreadPolicy(policy);

    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_profile3);

    name = (TextView) findViewById(R.id.textView1);
    bmImage2 = (ImageView) findViewById(R.id.imageView1);
    address = (TextView) findViewById(R.id.textView2);
    gender = (TextView) findViewById(R.id.textView3);
    loyalitypoints = (TextView) findViewById(R.id.textView7);
    followers = (TextView) findViewById(R.id.textView8);
    following = (TextView) findViewById(R.id.textView9);
    // list13 = new ArrayList<HashMap<String, Object>>();
    mListView = (ListView) findViewById(android.R.id.list);
    mListView.setClickable(true);






    // mListView=(ListView)findViewById(R.id.list);
    mAdapter = new Adapter(this,c,mSource );
    mListView.setAdapter(mAdapter);
    Log.w("Parsing JSON Data", "Before Item click");


    mListView.setRecyclerListener(mAdapter.mRecyclerListener);

}

回答1:

First of all you have made your imageView reference to be static in ViewHolder. You should not do that, because every getView will simply overwrite it and you will always have each view's ViewHolder referencing only one ImageView. Secod, you should always bear in mind that ListView is recycling views, so if you started loading an image for one view and then scrolled it, by the moment image was loaded the same view may already represent another list view item. Overall displaying bitmaps in ListView efficiently is a fairly complex task that involves quite a lot of variables. I would suggest you reading this and downloading a code example they provide. This is an excellent starting point and in simple cases may be enough to be copy-pasted.