我的To Do应用程序中无法编辑多个列表项

时间:2017-08-29 13:44:13

标签: android listview startactivityforresult

我已经构建了一个待办事项应用程序,允许我将项目添加到列表视图中,在长按下删除这些项目并进行编辑。但是,我无法编辑超过1个项目。我的第一个编辑工作,项目更新,但在那之后,我尝试编辑的任何其他项目保持不变。

这是我的班级,负责编辑:

public class EditItemActivity extends AppCompatActivity {
    EditText editToDo;
    String itemPos;
    Button saveChanges;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_edit_item);

        editToDo = (EditText) findViewById(R.id.edit_todo_item);
        saveChanges = (Button)findViewById(R.id.save_changes);

        String item = getIntent().getStringExtra("item");
        editToDo.setText(item);
        itemPos = getIntent().getStringExtra("itemPos");

        saveChanges.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                onSubmit();
            }
        });
    }

    public void onSubmit() {
        Intent item = new Intent();
        item.putExtra("item", editToDo.getText().toString());
        item.putExtra("itemPos", itemPos);
        setResult(RESULT_OK, item);
        finish();
    }
}

在我的MainActivity中调用startActivityForResult及其相应的方法:

public class MainActivity extends AppCompatActivity {
    private final int REQUEST_CODE = 10;
    ArrayList <String> toDoItems = new ArrayList<>();
    ArrayAdapter<String> adapter;
    ListView listItems;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        listItems = (ListView) findViewById(R.id.listViewItems);
        adapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, toDoItems);
        listItems.setAdapter(adapter);

        listItems.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
            @Override
            public boolean onItemLongClick(AdapterView<?> adapter, View item, int position, long id) {
                toDoItems.remove(position);
                MainActivity.this.adapter.notifyDataSetChanged();
                return true;
            }
        });

        listItems.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapter, View item, int pos, long id) {
                Intent intent = new Intent(MainActivity.this, EditItemActivity.class);
                intent.putExtra("item", toDoItems.get(pos));
                intent.putExtra("itemPos", String.valueOf(pos));
                startActivityForResult(intent, REQUEST_CODE);
            }
        });
    }

    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (resultCode == RESULT_OK && requestCode == REQUEST_CODE) {
            String item = data.getStringExtra("item");
            int itemPosition = Integer.parseInt(data.getStringExtra("itemPos"));
            toDoItems.set(itemPosition, item);
            adapter.notifyDataSetChanged();
        }
    }

    public void addItem(View v){
        EditText newItem = (EditText) findViewById(R.id.itemInputEditText);
        String item = newItem.getText().toString();
        adapter.add(item);
        newItem.setText("");
    }
}

1 个答案:

答案 0 :(得分:0)

基本上我所做的是接受OğuzhanDöngül的建议并加入其中。所以在我的onActivityResult方法中,这就是我所做的:

protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (resultCode == RESULT_OK && requestCode == REQUEST_CODE) {
        String item = data.getStringExtra("item");
        int itemPosition = Integer.parseInt(data.getStringExtra("itemPos"));
        toDoItems.add(itemPosition, item);
        toDoItems.remove(itemPosition + 1);
        adapter.notifyDataSetChanged();
    }
}