如何删除ArrayList的项目,然后更新列表视图 [英] How to remove arraylist item and then update listview
问题描述
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);
}
}
我想从数组列表中删除项。正如你可以看到我使用复选框。我第一次点击复选框,正确的项目被删除。如果我点击复选框第二次,应用程序崩溃是由于索引越界。我怎样才能把所谓的任务数组列表中的项目,并更新列表视图?
I want to remove item from the array list. As you can see I am using checkbox. The first time I click the checkbox, correct item is removed. The second time if I click the checkbox, the application crashes due to index out of bounds. How can I remove an item from the array list called tasks and update the listview?
推荐答案
在您从任务删除项目
阵列,所有以后的项目中取得了新的索引因此,所有的的位置,你已经保存在持有人,并传递给
值 OnCheckedChangeListener
是错误的。我fyou想这样做这样一来,你不能依赖于使用数组作为查找条目的方式中的位置。你需要使用对象本身和搜索数组找到匹配的对象。使用类似
Once you remove an item from the tasks
array, all of the items after that get a new index so all of the position
values that you've saved in the holders and passed to the OnCheckedChangeListener
are wrong. I fyou want to do this this way, you can't rely on using the position in the array as a way of finding an entry. You'll need to use the object itself and search the array to find the matching object. Use something like
int arrayIndexOfThisTask = tasks.indexOf(objectThatIsInTheArray);
编辑:添加code例如:
试试这个:
public View getView(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();
v.setTag(holder); // Attach the holder to the view so we can find it again
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);
} else {
// Get the ViewHolder from the recycled view
holder = (ViewHolder)v.getTag();
}
// Get the task at this position in the array
final Task task = tasks.get(position);
holder.taskTitleTextView.setText(task.getTasksTitleString());
holder.taskDescriptionTextView.setText(task.getTasksDescriptionString());
holder.taskDueTimeTextView.setText(task.getTasksDueTimeString());
holder.checkBox.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if(holder.checkBox.isChecked())
{
// Try to find the position in the array for this object (it may already have been removed)
int position = tasks.indexOf(task);
System.out.println("postion: " + position);
// If this object is no longer in the array, just ignore this action
if (position >= 0) {
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();
}
}
}
});
return v;
}
我可能没有一切完全正确的,但你应该明白了吧。
I may not have everything exactly right, but you should get the idea.
有实际上许多事情不对您的code:
There were actually many things wrong with your code:
- 您没有正确使用
ViewHolder
模式。你永远不使用setTag()附
,你永远不检索ViewHolder
到查看
本身ViewHolder
从查看
给出convertView
。 - 如果
convertView
是非空你刚回到它没有做任何事的。这是错误的,因为适配器回收认为它想要的任何方式,它可以通过你的查看
6的位置,并要求查看
的位置1。在这种情况下,您刚刚返回的查看
为第6位(这是不正确的)。 - 我已经通过实际的
工作
对象到onCheckChanged()
方法,而不是传递的位置和我的以前用过的indexOf()
来获取数组该对象的当前位置。
- You weren't using the
ViewHolder
pattern correctly. You never attached theViewHolder
to theView
itself by usingsetTag()
and you never retrieved theViewHolder
from theView
when given a recycled view inconvertView
. - If
convertView
was non-null you just returned it without doing anything with it. This is wrong because the adapter recycles views any way it wants to and it may pass you theView
from position 6 and ask for aView
for position 1. In this case you would have just returned theView
for position 6 (which isn't correct). - I've passed the actual
Task
object into theonCheckChanged()
method instead of passing the position and I've usedindexOf()
to get the current position in the array for that object.
我希望这是很有帮助的。
I hope this is helpful.
这篇关于如何删除ArrayList的项目,然后更新列表视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!