Я пишу действительно простое приложение для проверки списка.Пользователь может добавлять задачи, а затем отмечать их.Но по какой-то причине, когда пользователь проверяет элемент - позиция в списке меняется.Вот пара скриншотов, чтобы показать, что я имею в виду.Код моего ListAdapter выглядит следующим образом:
(Число в конце элемента списка - это его позиция. Например, У стирки [False] 0 - элемент в позиции 0.)
КОД
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;
}
}