我正在开发 todolist 应用程序,但无法处理列表视图中的编辑项目。编辑按钮就像添加按钮一样工作。旧项目不会被删除。当我使用 (ListView.getSelectedItemPosition(),myobject) 程序给出 arrayindexoutofboundsexception 时。
这里 EditItem.java
editButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
// ArrayList<String> b = new ArrayList<String>();
task = editText1.getText().toString();
priorityLevel = spinner.getSelectedItem().toString();
status = toggleButton.getText().toString();
int day = datePicker.getDayOfMonth();
int month = datePicker.getMonth() + 1;
int year = datePicker.getYear();
date = day + "/" + month + "/" + year;
itemList.setName(task);
itemList.setPriorityLevel(priorityLevel);
itemList.setStatus(status);
itemList.setDueDate(date);
Intent okIntent = new Intent();
okIntent.putExtra("editItem", new DataWrapper(itemList));
setResult(Activity.RESULT_OK, okIntent);
finish();
}
});
和 MainActivity.java
todoListView.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
Intent editIntent = new Intent(MainActivity.this,
EditItem.class);
startActivityForResult(editIntent, EDIT_NOTE);
}
});
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == Activity.RESULT_OK) {
switch (requestCode) {
case EDIT_NOTE:
DataWrapper dw2 = (DataWrapper) data
.getSerializableExtra("editItem");
entry = dw2.getEntry();
todoItems.set(todoListView.getSelectedItemPosition(), entry);
todoArrayAdapter.notifyDataSetChanged();
break;
default:
break;
}
}
super.onActivityResult(requestCode, resultCode, data);
}