ListView listView;
Activity activity;
public ArrayList<Tasks> tasks;
View v;
public TaskAdapter(Activity activity, ArrayList<Tasks> tasks)
{
super(activity, R.layout.presenterlayout, tasks);
this.activity = activity;
this.tasks = tasks;
}
static class ViewHolder {
public TextView taskTitleTextView;
public TextView taskDescriptionTextView;
public TextView taskDueTimeTextView;
public CheckBox checkBox;
}
public View getView(final int position, View convertView, ViewGroup parent) {
final ViewHolder holder;
v = convertView;
if (v == null) {
LayoutInflater inflator = activity.getLayoutInflater();
v = inflator.inflate(R.layout.presenterlayout, null, false);
listView = (ListView) v.findViewById(R.id.listView);
holder = new ViewHolder();
holder.taskTitleTextView = (TextView) v.findViewById(R.id.taskTitleTextView);
holder.taskDescriptionTextView = (TextView) v.findViewById(R.id.taskDescriptionTextView);
holder.taskDueTimeTextView = (TextView) v.findViewById(R.id.taskDueTimeTextView);
holder.checkBox = (CheckBox) v.findViewById(R.id.checkBox);
holder.taskTitleTextView.setText(tasks.get(position).getTasksTitleString());
holder.taskDescriptionTextView.setText(tasks.get(position).getTasksDescriptionString());
holder.taskDueTimeTextView.setText(tasks.get(position).getTasksDueTimeString());
holder.checkBox.setId(position);
holder.checkBox.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if(holder.checkBox.isChecked())
{
System.out.println("postion: " + position);
if(tasks.get(position).isTasksCompleted().equals("true"))
{
tasks.get(position).setTasksCompleted("false");
}
else if(tasks.get(position).isTasksCompleted().equals("false"))
{
tasks.get(position).setTasksCompleted("true");
}
updateThisTask(tasks.get(position));
tasks.remove(position);
notifyDataSetChanged();
}
}
});
}
else {
v = convertView;
}
return v;
}
private void updateThisTask(Tasks tasks) {
DBAdapter dbAdapter = new DBAdapter(getContext());
int id = dbAdapter.getID(tasks);
dbAdapter.updateTask(tasks, id);
}
}
我想从数组列表中删除项目。如您所见,我正在使用复选框。第一次单击复选框时,会删除正确的项目。如果我第二次单击该复选框,应用程序由于索引超出范围而崩溃。如何从名为 tasks 的数组列表中删除一个项目并更新列表视图?