获取所选项目在操作模式下的位置

时间:2016-06-08 08:21:24

标签: android listview position multichoiceitems

当我长按列表视图中的任何项目时,会显示上下文操作栏。现在我想要的是当我在选择项目后按下删除按钮然后所选项目被删除但我无法获得所选项目的位置。 我的多选模式代码是:

listViewMessages.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE_MODAL);
listViewMessages.setMultiChoiceModeListener(new MultiChoiceModeListener() {

    @Override
    public void onItemCheckedStateChanged(ActionMode mode, int position,
                                      long id, boolean checked) {
       mode.setTitle(listViewMessages.getCheckedItemCount()+ " Selected");

    }

    @Override
    public boolean onActionItemClicked(final ActionMode mode, MenuItem item) {
        switch (item.getItemId()) {
            case R.id.menu:
                deleteScreenedMessageFromInbox();                                       
                  mode.finish();
                return true;
            default:
                return false;
        }
    }

    @Override
    public boolean onCreateActionMode(ActionMode mode, Menu menu) {
        // Inflate the menu for the CAB
        MenuInflater inflater = mode.getMenuInflater();
        inflater.inflate(R.menu.contextual, menu);
        return true;
    }


    @Override
    public void onDestroyActionMode(ActionMode mode) {

    }

    @Override
    public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
        return false;
    }
});

当我点击删除按钮时,此方法正在调用:

deleteScreenedMessageFromInbox();

我必须在这个方法中传递一个参数,这是所选项目的位置。此方法的代码是:

public void deleteScreenedMessageFromInbox(Integer position) {

Integer id = smsIDs.get(position);

String deleteScreenedMessage = "delete from " + "sms" + " where " + "id" + " = " + id;

DBsms smsDb = new DBsms(this);
SQLiteDatabase dbw = smsDb.getWritableDatabase();
dbw.execSQL(deleteScreenedMessage);
dbw.close();

任何解决方案?

2 个答案:

答案 0 :(得分:1)

以下是我的建议:

List<Integer> positionsList = new ArrayList<>();//declare this as a member variable--outside of any method
listViewMessages.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE_MODAL);
listViewMessages.setMultiChoiceModeListener(new MultiChoiceModeListener() {

@Override
public void onItemCheckedStateChanged(ActionMode mode, int position,
                                  long id, boolean checked) {
   mode.setTitle(listViewMessages.getCheckedItemCount()+ " Selected");
   if(checked)
       positionsList.add(position)
   else
       positionsList.remove(position)
}

@Override
public boolean onActionItemClicked(final ActionMode mode, MenuItem item) {
    switch (item.getItemId()) {
        case R.id.menu:
            deleteScreenedMessageFromInbox();                                       
              mode.finish();
            return true;
        default:
            return false;
    }
}

@Override
public boolean onCreateActionMode(ActionMode mode, Menu menu) {
    // Inflate the menu for the CAB
    MenuInflater inflater = mode.getMenuInflater();
    inflater.inflate(R.menu.contextual, menu);
    return true;
}


@Override
public void onDestroyActionMode(ActionMode mode) {

}

@Override
public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
    return false;
}
});

然后在deleteScreenedMessageFromInbox()访问positionsList上,如下:

public void deleteScreenedMessageFromInbox() {

    DBsms smsDb = new DBsms(this);
    SQLiteDatabase dbw = smsDb.getWritableDatabase();

    for(int position : positionsList) {
        Integer id = smsIDs.get(position);

        String deleteScreenedMessage = "delete from " + "sms" + " where " + "id" + " = " + id;
        dbw.execSQL(deleteScreenedMessage);
    }
    dbw.close();
}

答案 1 :(得分:0)

实现onitemclickListener并获取项ID并将其保存在任何变量中,并在用户单击delete并下次调用adapter.notifyDataSetChanged();

时删除该项

例如

listViewMessages.setOnItemClickListener(new AdapterView.OnItemClickListener() {
    @Override
    public void onItemClick(AdapterView<?> adapter, View v, int position,
                            long arg3) {
        //you can pass position from here to your method like this
         deleteScreenedMessageFromInbox(position)


        String value = (String) adapter.getItemAtPosition(position);
        adapter.remove(value);
        adapter.notifyDataSetChanged();               
    }
});