列表视图图像在滚动时会发生变化

时间:2013-05-14 10:18:03

标签: android json listview scroll baseadapter

解析列表视图(imageview和textview)中的json 数据。对于listview的第一个四个项目图像和textview正确添加,但是当我滚动列表视图时,imageview中的图像会发生变化。 这是我的代码..

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 个答案:

答案 0 :(得分:2)

首先,您已将ViewView引用设置为ViewHolder中的静态引用。你不应该这样做,因为每个getView都会覆盖它,你总是让每个视图的ViewHolder只引用一个ImageView。 Secod,你应该始终牢记ListView是回收视图,所以如果你开始为一个视图加载图像然后滚动它,到加载图像时,相同的视图可能已经代表另一个列表视图项。 总体而言,在ListView中有效显示位图是一项相当复杂的任务,涉及很多变量。我建议你阅读this并下载他们提供的代码示例。这是一个很好的起点,在简单的情况下可能足以复制粘贴。