ArrayAdapter中各个视图的OnClickListener混淆了

时间:2012-04-30 18:45:58

标签: android android-listview android-arrayadapter onclicklistener

每当我点击ImageView的{​​{1}}时,我会得到该对象的OnClickListener,该对象适用于屏幕底部的下一个URL,还没有出现在屏幕上。

View

以下是 public View getView(int position, View convertView, ViewGroup parent){ View rowView = convertView; if(rowView == null){ rowView = inflater.inflate(R.layout.listitem, null); //Creëer een Viewholder om alles statisch op te slaan in het geheugen holder = new ViewHolder(); holder.title = (TextView) rowView.findViewById(R.id.title); holder.subreddit = (TextView)rowView.findViewById(R.id.subreddit); holder.source = (TextView)rowView.findViewById(R.id.source); holder.score = (TextView)rowView.findViewById(R.id.score); holder.image = (ImageView)rowView.findViewById(R.id.imageView1); holder.image.setTag(position); rowView.setTag(holder); } else{ holder = (ViewHolder) rowView.getTag(); } holder.image.setOnClickListener(new OnClickListener(){ @Override public void onClick(View v) { Intent i = new Intent(); i.setAction("com.tolgap.json.TEST"); i.putExtra("page", holder.thing.url); context.startActivity(i); } }); holder.thing = gegevens.get(position).data; holder.title.setText(holder.thing.title); holder.subreddit.setText(holder.thing.subreddit); holder.source.setText(holder.thing.domain); holder.score.setText(holder.thing.score); holder.image.setImageBitmap(plaatjes.get(position)); return rowView; } 的代码:

ViewHolder

我已经阅读了一些关于使用static class ViewHolder{ Result thing; TextView title; TextView subreddit; TextView source; TextView score; ImageView image; } 方法的内容,并且我已经尝试了这一点,但我并没有得到我必须作为标记的内容。

2 个答案:

答案 0 :(得分:1)

Result数据对象存储在ViewHolder

中是没有意义的
static class ViewHolder{
    TextView title;
    TextView subreddit;
    TextView source;
    TextView score;
    ImageView image;
}

getView()方法:

//...
else{
    holder = (ViewHolder) rowView.getTag();
}
Result obj = gegevens.get(position).data;
holder.image.setTag(obj.url); // It's a String I guess?!?
holder.image.setOnClickListener(new OnClickListener(){

    @Override
    public void onClick(View v) {
        String toPass = (String) v.getTag();
        Intent i = new Intent();
        i.setAction("com.tolgap.json.TEST");
        i.putExtra("page", toPass);
        context.startActivity(i);
    }

});
holder.title.setText(obj.title);
holder.subreddit.setText(obj.subreddit);
holder.source.setText(obj.domain);
holder.score.setText(obj.score);
holder.image.setImageBitmap(plaatjes.get(position)); 
return rowView;
}

答案 1 :(得分:0)

你在哪里宣布持有人变数?将它设为局部最终变量,看看是否能解决您的问题。