0
我正在編寫一個非常基本的檢查列表應用程序。用戶可以添加任務,然後檢查它們。但出於某種原因,當用戶檢查一個項目時 - 列表中的位置發生變化。這裏有幾個截圖來展示我的意思。我ListAdapter的代碼如下:檢查一些項目後,我的checklistview中的項目位置發生了什麼變化
(AT的列表項的末尾數是它的位置,比如洗衣服[虛假] 0是項目在位置0)
CODE
class TaskListAdapter extends ArrayAdapter<Task> implements OnCheckedChangeListener
{
Activity context;
View recycledView;
ViewAccessor viewAccessor;
CheckBox checkbox;
int position;
public TaskListAdapter(Activity context, int resource, int textViewResourceId, List<Task> objects) {
super(context, resource, textViewResourceId, objects);
this.context = context;
}
public View getView(int position, View convertView, ViewGroup parent) {
this.recycledView = convertView;
if(recycledView == null)
{
LayoutInflater inflater = context.getLayoutInflater();
recycledView = inflater.inflate(R.layout.checkview, null);
this.viewAccessor = new ViewAccessor(recycledView);
this.recycledView.setTag(this.viewAccessor);
this.checkbox = (CheckBox) recycledView.findViewById(R.id.cbTaskCompleted);
this.checkbox.setOnCheckedChangeListener(this);
this.checkbox.setTag((Integer) position);
}else
{
viewAccessor = (ViewAccessor) this.recycledView.getTag();
this.checkbox = viewAccessor.getCheckBox();
}
this.checkbox.setText(taskList.get(position).toString()+String.valueOf(position));
this.checkbox.setChecked(taskList.get(position).isCompleted);
return recycledView;
}
public void onCheckedChanged(CompoundButton buttonView,boolean isChecked) {
Task taskUnderConsideration = taskList.get((Integer) buttonView.getTag()) ;
taskUnderConsideration.setCompleted(isChecked);
buttonView.setText(taskUnderConsideration.toString()+buttonView.getTag().toString());
}
}
class ViewAccessor
{
View v;
CheckBox cb = null;
ViewAccessor(View v)
{
this.v = v;
}
CheckBox getCheckBox()
{
if(cb==null)
cb = (CheckBox) v.findViewById(R.id.cbTaskCompleted);
return cb;
}
}