Cannot edit more than 1 list item in my To Do app

53 views Asked by At

I've built a to do app that allows me to add to do items into a listview, remove those items on long click and edit them. However, I can't edit more than 1 item. My first edit works and the item is updated but after that, any other item I try to edit remains the same.

Here is my class that's responsible for editing:

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

The startActivityForResult and its corresponding method are called in my MainActivity:

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

There are 1 answers

0
Levent Ozbek On BEST ANSWER

Basically what I did was take Oğuzhan Döngül's advice and add onto it. So in my onActivityResult method, this is what I did:

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