使用ArrayAdapters时,单击按钮将ListView选项模式从singleChoice更改为MultipleEchoiceMode
事实上,我想实现一些类似于我们手机中常见的聊天/MSG删除功能。单击菜单上的删除按钮时,行选择复选框必须出现,顶部的标签显示所选行数。选择行数后点击右上角的“完成/删除”按钮时,必须出现删除确认对话框,并且必须删除所选行。我在下图中突出显示了所需的功能。[图像1-菜单中的按钮][1][图像2-单击删除按钮时的多模式外观][2][图像3-选择和标签][3][图像4-确认对话框][4]。这是我到目前为止编写的代码,但我的代码不起作用,需要帮助。这是我的密码:
//for the start I want the list be in single mode
ListView listView = (ListView) findViewById(R.id.listView);
ArrayAdapter arrList = new ArrayAdapter<String>(this,
安卓.R.layout.simple_list_item_single_choice, namesList);
listView.setAdapter(arrList);
// for the list to be in multimode on clicking the button in the menu
public boolean onCreateOptionsMenu(Menu menu)
{
getMenuInflater().inflate(R.menu.mymenu, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.deletebutton) {
if (namesList.size() != 0) {
if (mListView.getChoiceMode() == ListView.CHOICE_MODE_SINGLE) {
mListView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
directoryList = new ArrayAdapter<String>(this,
安卓.R.layout.simple_list_item_multiple_choice,
namesList);
delete();
}
} else {
Toast.makeText(getApplicationContext(), "names not found",
Toast.LENGTH_SHORT).show();
}
}
return super.onOptionsItemSelected(item);
}
// delete method implementation
AlertDialog.Builder a_builder = new AlertDialog.Builder(this);
a_builder.setTitle("Confirm Delete?");
a_builder.setMessage("DELETE")
.setCancelable(true)
.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
int i;
SparseBooleanArray checkedItemPositions = mListView.getCheckedItemPositions();
int itemCount = listView.getCount();
for (i = itemCount - 1; i >= 0; i--) {
if (checkedItemPositions.get(i)) {
arrList.remove(namesList.get(i));
}
}
checkedItemPositions.clear();
arrList.notifyDataSetChanged();
Toast.makeText(getApplicationContext(), "Deleted", Toast.LENGTH_SHORT).show();
.setNegativeButton("No", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
});
[1]: https://i.stack.imgur.com/ZR1Wv.png
[2]: https://i.stack.imgur.com/4gJGd.png
[3]: https://i.stack.imgur.com/uvn9p.png
[4]: https://i.stack.imgur.com/3gsML.png
共 (0) 个答案