如何进行可编辑的listview项目?

时间:2013-11-10 14:26:10

标签: android listview

我有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();
        }
    });

0 个答案:

没有答案