我在arrayadapter类中有一个ViewHolder,我想在listview中的特定项目上绑定点击操作,并且需要在favoriteicon点击上更新项目的内容。
我在getView方法中绑定了click动作:
class ViewHolder
{
public ImageView favoriteicon;
public TextView title;
public TextView time;
public TextView type;
public TextView hidden_value_id;
public TextView hidden_image_name;
public boolean imageset=false;
public ImageView imgView;
public ViewHolder(View base)
{
this.favoriteicon=(ImageView) base.findViewById(R.id.favoriteicon);
this.title = (TextView) base.findViewById(R.id.title);
this.time = (TextView) base.findViewById(R.id.time);
this.type = (TextView) base.findViewById(R.id.type);
this.imgView = (ImageView) base.findViewById(R.id.icon);
this.hidden_value_id = (TextView) base.findViewById(R.id.hidden_value_id);
this.hidden_image_name = (TextView) base.findViewById(R.id.hidden_image_name);
}
}
@Override
public View getView(final int position,View view, final ViewGroup parent) {
final ViewHolder holder;
View rowView=view;
if(rowView==null) {
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(context.LAYOUT_INFLATER_SERVICE);
rowView = inflater.inflate(R.layout.new_listview_items, parent, false);
holder = new ViewHolder(rowView);
rowView.setTag(holder);
}
else {
holder=(ViewHolder)rowView.getTag();
}
holder.title.setText(title.get(position));
holder.time.setText(time.get(position));
holder.type.setText(type.get(position));
holder.hidden_value_id.setText(id.get(position));
holder.hidden_image_name.setText(hidden_image_name.get(position));
if(holder.imageset==true)
{
holder.favoriteicon.setImageResource(R.drawable.h);
}
holder.favoriteicon.setOnClickListener(new
View.OnClickListener() {
public void onClick(View b) {
holder.favoriteicon.setImageResource(R.drawable.h);
holder.imageset=true;
notifyDataSetChanged();
}
});
return rowView;
}
如果我有项目的位置,如何更新项目点击列表视图中的特定项目。
答案 0 :(得分:0)
所以目前我想你有一个if else语句,检查当前位置的项目是否有利于你?现在发生的是您在单击图标时设置图标,但是您没有更改对象值。因此,当您向上和向下滚动并且列表项被回收时,它将重置该值。
你需要有这样的东西
holder.favoriteicon.setOnClickListener(new View.OnClickListener() {
public void onClick(View b) {
object.setFavourited(true);
holder.favoriteicon.setImageResource(R.drawable.h);
}
});
现在这只是基于您目前发布的代码的猜测。如果您使用其余的适配器代码更新问题,我可以更新我的答案。