0
我有一个列表视图中有两个图像按钮在行中。点击图像按钮更改背景颜色。当我点击第一个列表项时,图像按钮背景变化和视图被保存,但滚动列表视图的底部,另一列表项的图像按钮的背景颜色也会改变。下面是自定义适配器的getView。我怎样才能避免这个问题?与getview混淆android imagebuttons
public View getView(final int i, View view, ViewGroup viewGroup) {
ViewHolder holder = new ViewHolder();
holder = null;
//view=null;
if (inflater == null) {
inflater = (LayoutInflater) activity
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
if (view == null) {
view = inflater.inflate(R.layout.student_list, null);
holder = new ViewHolder();
holder.presentButton = (ImageButton) view.findViewById(R.id.imageView);
holder.absentButton = (ImageButton) view.findViewById(R.id.imageView2);
holder.presentButton.setBackgroundColor(0);
holder.absentButton.setBackgroundColor(0);
view.setTag(holder);
}
else {
holder = (ViewHolder) view.getTag();
}
final SQLiteStudents db1 = new SQLiteStudents(activity.getApplicationContext());
final TextView tvName = (TextView) view.findViewById(R.id.tv_name);
final TextView tvRoll = (TextView) view.findViewById(R.id.tv_roll);
final studentInfo s = students.get(i);
tvRoll.setText(s.getRoll() + ".");
tvName.setText(s.getName());
final Integer roll = Integer.parseInt(s.getRoll());
//ivpresent.setBackgroundColor(0);
final ViewHolder finalHolder1 = holder;
//final ViewHolder finalHolder = holder;
holder.presentButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
db1.updateUser(roll,"present");
finalHolder1.presentButton.setBackgroundColor(GREEN);
finalHolder1.absentButton.setBackgroundColor(0);
//v1.setTag(v.getTag());
//Log.d("present","Roll No: "+String.valueOf(roll));
finalHolder1.presentButton.setTag(Integer.toString(i));
notifyDataSetChanged();
}
});
holder.absentButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
db1.updateUser(roll,"absent");
finalHolder1.presentButton.setBackgroundColor(0);
finalHolder1.absentButton.setBackgroundColor(RED);
//view=null;
//Log.d("absent","Roll No: "+String.valueOf(roll));
finalHolder1.presentButton.setTag(Integer.toString(i));
notifyDataSetChanged();
}
});
return view;
}
public static class ViewHolder {
public ImageButton presentButton;
public ImageButton absentButton;
}
中的其他部分添加代码崩溃我的应用程序。对于两个图像按钮,默认颜色都是空的。我想仅突出显示按下的按钮而不是其他按钮。我的列表视图行由textview和2 imagebuttons组成..完整的customadapter代码位于:[link](https://pastebin.com/SbTKWKLB) –
我从你的代码中得到了这个想法,但仍然没有解决问题..上面的代码返回错误和期望:'最终布尔[] presentButton =新布尔[1];' 当我运行声明imagebutton布尔到最终数组,没有按钮突出显示后点击它.. :( –
谢谢你支持我,但是,我仍然遇到你提供的指南问题,所有列表项目中的一个imageButton被选中onScroll。我在[code](https://pastebin.com/Uupxwt0q)上更改了vew行,但它给了我相同的错误以前。 –