删除listview项只删除第一项

时间:2017-10-12 06:49:38

标签: android listview

我有一个listview包含很多项目。我在listview项目上执行多次删除。但是当我从listview中仅选择第二项或第三项时,它只删除列表中的第一项而不是所选项。 请帮我解决这个问题。

这是我的代码:

/////multiple delete on clicking listview///////

    listview.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE_MODAL);

    listview.setMultiChoiceModeListener(new AbsListView.MultiChoiceModeListener() {

        @Override
        public void onItemCheckedStateChanged(ActionMode mode,
                                              int position, long id, boolean checked) {
            // Capture total checked items
            final int checkedCount = listview.getCheckedItemCount();

            // Set the CAB title according to total checked items
            mode.setTitle(checkedCount + " Selected");
            // Calls toggleSelection method from ListViewAdapter Class
            adapter.toggleSelection(position);

        }

        @Override
        public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
            switch (item.getItemId()) {
                case R.id.delete:
                    // Calls getSelectedIds method from ListViewAdapter Class
                    SparseBooleanArray selected = adapter.getSelectedIds();
                    // Captures all selected ids with a loop
                    for (int i = 0; i < (selected.size()); i++) {
                        if (selected.valueAt(i)) {

                            all_memories_getter_setter selecteditem = (all_memories_getter_setter) adapter.getItem(selected.keyAt(i));


                            getContentResolver().delete(DataProvider.WRITE_URI, DataProvider.ID + '=' + list.get(i).getId(), null);

                            System.out.println("##############values of ss" + list.get(i).getId());

                            // Remove selected items following the ids
                            adapter.remove(selecteditem);

                        }

                        populateList();

                    }
                    adapter = new ListViewAdapter(All_memories.this, list);
                    listview.setAdapter(adapter);


                    mode.finish();
                    return true;

       default:
                    return false;
            }
        }
   @Override
        public boolean onCreateActionMode(ActionMode mode, Menu menu) {
            mode.getMenuInflater().inflate(R.menu.main, menu);
            return true;
        }

        @Override
        public void onDestroyActionMode(ActionMode mode) {
            // TODO Auto-generated method stub
            adapter.removeSelection();
        }

        @Override
        public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
            // TODO Auto-generated method stub
            return false;
        }
    });

这是我的适配器代码: 这里我添加了多个删除列表视图项的代码。

public class ListViewAdapter extends BaseAdapter {

public ArrayList<all_memories_getter_setter>list;
Activity activity;

/////**///// multiple delete
private SparseBooleanArray mSelectedItemsIds;

public  ListViewAdapter(Activity activity, ArrayList<all_memories_getter_setter>list)
{
    super();
    mSelectedItemsIds= new SparseBooleanArray();
    this.activity=activity;
    this.list=list;
}
@Override
public int getCount() {
    return list.size();
}

@Override
public Object getItem(int position) {
    return list.get(position);
}

@Override
public long getItemId(int position) {
    return 0;
}


/////**/////// for multiple delete
public void remove(all_memories_getter_setter object){
    list.remove(object);
    notifyDataSetChanged();
}

public List<all_memories_getter_setter> getall_memories_getter_setter(){
    return list;
}

public void toggleSelection(int position){
    selectView(position, !mSelectedItemsIds.get(position));

}
public void removeSelection(){
    mSelectedItemsIds = new SparseBooleanArray();
    notifyDataSetChanged();
}

public void selectView(int position , boolean value){
    if(value){
        mSelectedItemsIds.put(position, value);
    }else{
        mSelectedItemsIds.delete(position);
        notifyDataSetChanged();
    }

}
public int getSelectedCount(){
    return mSelectedItemsIds.size();
}
public SparseBooleanArray getSelectedIds(){
    return mSelectedItemsIds;
}



/////**/////// for multiple delete

public  class Viewholder
{
    TextView txtFirst;
    TextView txtSecond;
    TextView txtThird;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {


    Viewholder holder;
    LayoutInflater inflater = activity.getLayoutInflater();

    if (convertView == null) {
        convertView = inflater.inflate(R.layout.activity_all_memories_listview, null);
        holder = new Viewholder();

        holder.txtFirst = (TextView) convertView.findViewById(R.id.list_date);
        holder.txtSecond = (TextView) convertView.findViewById(R.id.list_title);
        holder.txtThird = (TextView) convertView.findViewById(R.id.list_content);
        convertView.setTag(holder);

    } else {
        holder = (Viewholder) convertView.getTag();
    }

    holder.txtFirst.setText("" + list.get(position).getDate());
    holder.txtSecond.setText("" + list.get(position).getTitle());
    holder.txtThird.setText("" + list.get(position).getContent());
    return  convertView;
}
}

2 个答案:

答案 0 :(得分:4)

getItemId

  

获取与列表中指定位置关联的行ID。

您应该传递 position ,而不是 0

<强>唐&#39;吨

@Override
public long getItemId(int position) {
    return 0; // WRONG
}

<强>不要

 @Override
public long getItemId(int position) {
    return position;
}

答案 1 :(得分:0)

将删除方法更改为特定位置而不是对象。

public void remove(int position){
    list.remove(position);
    notifyDataSetChanged();
} 

并且在调用方法时传递ID而不是对象

adapter.remove(i);

修改 您正在使用稀疏布尔数组,单击某个项时不会更新该数组。

 SparseBooleanArray selected = adapter.getSelectedIds();

此值始终返回零,并且所选项始终为零。第一项始终被删除。