0

我有 todolist 应用程序,其中包括任务、优先级、日期、状态和我可以将新项目添加到列表中。当单击项目时另一个活动运行。这是问题,我无法获取新活动的项目值,当我单击编辑按钮时,程序添加新项目而不删除旧项目。

这里 setOnItemClickListener

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);

        }
    });

和 OnActivityResult

protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (resultCode == Activity.RESULT_OK) {

        switch (requestCode) {
        case ADD_NOTE:
            String extraName = AddItem.code;
            ArrayList<String> list = data
                    .getStringArrayListExtra(extraName);
            todoItems.addAll(list);
            todoArrayAdapter.notifyDataSetChanged();
            break;
        case EDIT_NOTE:
            String extraName2 = EditItem.code;
            ArrayList<String> list2 = data
                    .getStringArrayListExtra(extraName2);
            todoItems.addAll(list2);
            todoArrayAdapter.notifyDataSetChanged();
            break;
        default:
            break;
        }

    }
    super.onActivityResult(requestCode, resultCode, data);
}

和 EditItem.java

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.edit_item);

    editText1 = (EditText) findViewById(R.id.editText1);
    spinner = (Spinner) findViewById(R.id.prioritySpinner);
    datePicker = (DatePicker) findViewById(R.id.datePicker);
    toggleButton = (ToggleButton) findViewById(R.id.statusbutton);
    editButton = (Button) findViewById(R.id.editButton);
    cancelButton = (Button) findViewById(R.id.cancelButton);

    editButton.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View arg0) {
            itemList = new ArrayList<String>();
            item = 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.add(new Entry(item, priorityLevel, date, status)
                    .toString());

            Intent okIntent = new Intent();
            okIntent.putExtra(code, itemList);
            setResult(Activity.RESULT_OK, okIntent);
            finish();
        }
    });
4

0 回答 0