本文介绍了当滚动列表视图图像得到改变的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的解析JSON 的在列表视图(ImageView的和TextView的)数据。对于第一次的四个项目的ListView 的在形象TextView的是正确补充,但是当我滚动列表视图然后ImageView的图像是得到改变。
这里是我的code ..

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

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

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

}

推荐答案

您作出的ImageView参考在ViewHolder静态的。首先。你不应该这样做,因为每个getView会简单地覆盖它,你将永远有每个视图的ViewHolder引用只有一个ImageView的。
Secod,你应该始终牢记,ListView的是回收的意见,所以如果你开始加载图像一个视图,然后滚动它,因一时图像装载了同样的看法可能已经重新present另一个列表视图项。
总体显示ListView中有效的位图是一个相当复杂的任务,涉及到相当多的变数。我建议你​​阅读并下载code例如,它们提供。这是一个很好的起点,在简单情况下可能足以被复制粘贴。

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.

这篇关于当滚动列表视图图像得到改变的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-19 21:43