我的适配器中有以下代码。每个项目包含两个图像,每个图像下面有一个复选框。我想显示这些项目的列表,但是当我向上和向下滚动时,似乎第一个和最后一个项目是相同的。然后一段时间后其他项目也会出现问题。我已经查看了有关此问题的其他问题,我无法找到为什么在向下滚动后我的ListView项目会改组。
@Override
public View getView(final int position, View convertView, ViewGroup parent){
View view = convertView;
if(view == null){
viewHolder = new ViewHolder();
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = inflater.inflate(R.layout.wheel_list_item, null);
viewHolder.decisionNum = (TextView)view.findViewById(R.id.decisionNumText);
viewHolder.leftWheel = (ImageView)view.findViewById(R.id.leftWheel);
viewHolder.rightWheel = (ImageView)view.findViewById(R.id.rightWheel);
viewHolder.leftCheckBox = (CheckBox)view.findViewById(R.id.leftCheckBox);
viewHolder.rightCheckBox = (CheckBox)view.findViewById(R.id.rightCheckBox);
viewHolder.wheelTuple = list.get(position);
view.setTag(viewHolder);
}
else{
viewHolder = (ViewHolder)view.getTag();
}
viewHolder.decisionNum.setText(Integer.toString(position + 1));
viewHolder.leftCheckBox.setClickable(false);
viewHolder.rightCheckBox.setClickable(false);
viewHolder.leftWheel.setImageBitmap(new WheelView(context, viewHolder.wheelTuple.left).getBitmapReduced());
viewHolder.rightWheel.setImageBitmap(new WheelView(context, viewHolder.wheelTuple.right).getBitmapReduced());
int id = context.getResources().getIdentifier("GREY", "color", context.getPackageName());
int colorId=context.getResources().getColor(id);
if(!viewHolder.wheelTuple.left.isChosen()){
viewHolder.leftWheel.setColorFilter(colorId, PorterDuff.Mode.MULTIPLY);
viewHolder.rightCheckBox.setChecked(true);
}
else{
viewHolder.rightWheel.setColorFilter(colorId, PorterDuff.Mode.MULTIPLY);
viewHolder.leftCheckBox.setChecked(true);
}
return view;
}
static class ViewHolder{
TextView decisionNum;
ImageView leftWheel;
ImageView rightWheel;
CheckBox leftCheckBox;
CheckBox rightCheckBox;
WheelListActivity.WheelTuple wheelTuple;
}
答案 0 :(得分:5)
你必须使用以下两个功能,
@Override
public int getViewTypeCount() {
if(searchList.size()==0){
return 1;
}
return searchList.size();
}
@Override
public int getItemViewType(int position) {
return position;
}
如果你的dataArrayList的大小为0,那么记得从getViewTypeCount()返回1,否则你将得到ArrayIndexOutOfBoundsException。请参阅上面的代码示例。
答案 1 :(得分:-1)
尝试使用代码:
在布尔数组中添加getview()
if (mCheckedState[position])
viewHolder.leftCheckBox.setChecked(true);
else
viewHolder.leftCheckBox.setChecked(false);
if (mCheckedState[position])
viewHolder.rightCheckBox.setChecked(true);
else
viewHolder.rightCheckBox.setChecked(false);
声明变量并设置长度:
boolean [] mCheckedState;
public class Adapter_Sandesh extends BaseAdapter {
Context context;
boolean[] mCheckedState;
private LayoutInflater mInflater;
public Adapter_Sandesh(FragmentActivity activity, String[] list_margdarshan) {
context=activity;
mCheckedState = new boolean[<your length>];
}
...
...
...
@Override
public View getView(final int position, View convertView, ViewGroup parent){
View view = convertView;
if(view == null){
viewHolder = new ViewHolder();
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = inflater.inflate(R.layout.wheel_list_item, null);
viewHolder.decisionNum = (TextView)view.findViewById(R.id.decisionNumText);
viewHolder.leftWheel = (ImageView)view.findViewById(R.id.leftWheel);
viewHolder.rightWheel = (ImageView)view.findViewById(R.id.rightWheel);
viewHolder.leftCheckBox = (CheckBox)view.findViewById(R.id.leftCheckBox);
viewHolder.rightCheckBox = (CheckBox)view.findViewById(R.id.rightCheckBox);
viewHolder.wheelTuple = list.get(position);
view.setTag(viewHolder);
}
else{
viewHolder = (ViewHolder)view.getTag();
}
viewHolder.decisionNum.setText(Integer.toString(position + 1));
viewHolder.leftCheckBox.setClickable(false);
viewHolder.rightCheckBox.setClickable(false);
if (mCheckedState[position])
viewHolder.leftCheckBox.setChecked(true);
else
viewHolder.leftCheckBox.setChecked(false);
if (mCheckedState[position])
viewHolder.rightCheckBox.setChecked(true);
else
viewHolder.rightCheckBox.setChecked(false);
viewHolder.leftWheel.setImageBitmap(new WheelView(context, viewHolder.wheelTuple.left).getBitmapReduced());
viewHolder.rightWheel.setImageBitmap(new WheelView(context, viewHolder.wheelTuple.right).getBitmapReduced());
int id = context.getResources().getIdentifier("GREY", "color", context.getPackageName());
int colorId=context.getResources().getColor(id);
if(!viewHolder.wheelTuple.left.isChosen()){
viewHolder.leftWheel.setColorFilter(colorId, PorterDuff.Mode.MULTIPLY);
viewHolder.rightCheckBox.setChecked(true);
}
else{
viewHolder.rightWheel.setColorFilter(colorId, PorterDuff.Mode.MULTIPLY);
viewHolder.leftCheckBox.setChecked(true);
}
return view;
}
static class ViewHolder{
TextView decisionNum;
ImageView leftWheel;
ImageView rightWheel;
CheckBox leftCheckBox;
CheckBox rightCheckBox;
WheelListActivity.WheelTuple wheelTuple;
}
}