我需要使用“选项”菜单中的按钮从ListView
中删除由复选框选择的项目。选择项目后,我试图单击“选项”菜单上的“删除”按钮,然后我的应用程序崩溃。当我使用相同的代码,并且按钮位于listview正确运行的同一xml上时,仅当我尝试使用“选项菜单项”执行相同的操作时,问题才会发生。
编辑:我正在粘贴整个代码 这是我的代码,来自main_activity.java
public class MainActivity extends AppCompatActivity {
private ArrayList<String> items;
private ArrayAdapter<String> itemsAdapter;
private ListView lvItems;
private static final String TAG = "MainActivity";
Button delete;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(layout.activity_main);
lvItems = (ListView) findViewById(id.lvItems);
lvItems.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE_MODAL);
items = new ArrayList<String>();
itemsAdapter = new ArrayAdapter<String>(this, layout.item_to_do, id.task_title, items);
lvItems.setAdapter(itemsAdapter);
items.add("Pierwsza rzecz");
items.add("Druga rzecz");
delete = (Button) findViewById(id.action_delete_task);
setupListViewListener();
}
public void onCheckboxClicked(View view) {
// Is the view now checked?
boolean checked = ((CheckBox) view).isChecked();
// Check which checkbox was clicked
switch (view.getId()) {
case id.task_delete:
if (checked)
break;
}
}
private void setupListViewListener() {
lvItems.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
@Override
public boolean onItemLongClick(AdapterView<?> adapter, View view, int position, long id) {
items.remove(position);
itemsAdapter.notifyDataSetChanged();
return true;
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(final MenuItem item) {
switch (item.getItemId()) {
case id.action_add_task:
final EditText taskEditText = new EditText(this);
taskEditText.setAllCaps(true);
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Add a new task");
builder.setMessage("What do you want to do next?");
builder.setView(taskEditText);
builder.setPositiveButton("Add", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
String task = String.valueOf(taskEditText.getText());
Log.d(TAG, "Task to add: " + task);
itemsAdapter.add(task);
Snackbar.make(findViewById(id.lvItems), string.add,
Snackbar.LENGTH_SHORT)
.show();
}
});
builder.setNegativeButton("Cancel", null);
AlertDialog dialog = builder.create();
dialog.show();
return true;
case id.action_delete_task:
delete.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
SparseBooleanArray checkedItemPositions = lvItems.getCheckedItemPositions();
int itemCount = lvItems.getCount();
for (int i = itemCount - 1; i >= 0; i--) {
if (checkedItemPositions.get(i)) {
itemsAdapter.remove(items.get(i));
}
}
checkedItemPositions.clear();
itemsAdapter.notifyDataSetChanged();
}
});
default:
return super.onOptionsItemSelected(item);
}
}
}
答案 0 :(得分:0)
删除是菜单项,而不是布局中的视图,因此您将不会在其上设置点击侦听器。
就像下面一样
case id.action_delete_task:
SparseBooleanArray checkedItemPositions = lvItems.getCheckedItemPositions();
int itemCount = lvItems.getCount();
for (int i = itemCount - 1; i >= 0; i--) {
if (checkedItemPositions.get(i)) {
itemsAdapter.remove(items.get(i));
}
}
checkedItemPositions.clear();
itemsAdapter.notifyDataSetChanged();
我在您的代码中也看不到onCreateOption菜单。这用于增加活动中的菜单。
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.your_menu, menu);
return true;
}