如何使用位置或索引替换listView中的单击项

时间:2013-11-11 16:13:45

标签: android listview android-listview

我正在开发todolist app,我无法处理listview中的编辑项目。 “编辑”按钮的工作方式与添加按钮类似。旧项目不会被删除。当ı使用(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);
}

1 个答案:

答案 0 :(得分:0)

您可以尝试从onItemClick

中获取位置
todoListView.setOnItemClickListener(new OnItemClickListener()

到全局int。

然后执行:

todoItems.set(globalInt, entry);