3

I have a ListView, which displays a list of notes taken from a table in SQLiteDatabase with ArrayAdapter.

public class NotepadActivity extends ListActivity {

protected static final int ADD_NEW_NOTE = 0;
protected static final int EDIT_NOTE = 1;
ArrayAdapter<Note> adapter;
NotesManager manager;
private Note nNoteToDelete;
ArrayList<Note> lstAllNotes;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    findViewById(R.id.btnAddNewNote).setOnClickListener(addNewNote);
    manager = new NotesManager(this);
    lstAllNotes = manager.getAllNotes();
    adapter = new ArrayAdapter<Note>(this, R.layout.note, lstAllNotes);
    setListAdapter(adapter);
    getListView().setOnItemClickListener(editNote);
    registerForContextMenu(getListView());
}

When I click on an Item in this ListView, it takes this Note object to the EditNote activity:

private OnItemClickListener editNote = new OnItemClickListener() {

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position,
            long id) {
        Note currNote = (Note)parent.getItemAtPosition(position);

        int curr_note_id = currNote.getId();
        String curr_note_title = currNote.getTitle().toString();
        String curr_note_details = currNote.getDetails().toString();

        Intent editNote = new Intent(NotepadActivity.this, EditNote.class);
        editNote.putExtra("CURR_NOTE_ID", curr_note_id);
        editNote.putExtra("CURR_NOTE_TITLE", curr_note_title);
        editNote.putExtra("CURR_NOTE_DETAILS", curr_note_details);

        startActivityForResult(editNote, EDIT_NOTE);

    }
};

I can edit the title of the note in there and the content. When I hit the Save button, it sends back to the main activity the Strings of Title and the Details and the ID int:

public class EditNote extends Activity implements OnClickListener {

int curr_note_id;
String curr_note_title;
String curr_note_details;

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

    curr_note_id = getIntent().getExtras().getInt("CURR_NOTE_ID");
    curr_note_title = getIntent().getExtras().getString("CURR_NOTE_TITLE");
    curr_note_details = getIntent().getExtras().getString(
            "CURR_NOTE_DETAILS");

    ((EditText) findViewById(R.id.edtTitle)).setText(curr_note_title);
    ((EditText) findViewById(R.id.edtDetails)).setText(curr_note_details);

    findViewById(R.id.btnSave).setOnClickListener(this);
    findViewById(R.id.btnCancel).setOnClickListener(this);
}

@Override
public void onClick(View v) {

    switch (v.getId()) {
    case R.id.btnSave:
        String strNoteTitle = ((EditText) findViewById(R.id.edtTitle)).getText().toString().trim();
        String strNoteDetails = ((EditText) findViewById(R.id.edtDetails)).getText().toString().trim();

        if (!strNoteTitle.equals("")) {

            Intent data = new Intent();
            data.putExtra("NOTE_ID", curr_note_id);
            data.putExtra("NOTE_TITLE", strNoteTitle);
            data.putExtra("NOTE_DETAILS", strNoteDetails);
            setResult(RESULT_OK, data);
            finish();
        } else {
            ((EditText) findViewById(R.id.edtTitle))
                    .setError("A note must contain a title");
        }

        break;
    case R.id.btnCancel:
        setResult(RESULT_CANCELED);
        finish();
        break;
    }

}

}

And in the main activity it should update the DB and the ListView with the new Title, if there were changes. My code does update the DB, but I don't see the change in the ListView immediately, only if I exit the app and open it again. Here's the code (watch the second case, EDIT_NOTE):

protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (resultCode == RESULT_OK) {
        switch (requestCode) {
        case ADD_NEW_NOTE:
            String noteTitle = data.getExtras().getString("NOTE_TITLE");
            String noteDetails = data.getExtras().getString("NOTE_DETAILS");
            Note nNewNote = new Note(-1, noteTitle, noteDetails);
            adapter.add(nNewNote);
            manager.addNote(nNewNote);
            break;
        case EDIT_NOTE:
            int noteID = data.getExtras().getInt("NOTE_ID");
            noteTitle = data.getExtras().getString("NOTE_TITLE");
            noteDetails = data.getExtras().getString("NOTE_DETAILS");
            nNewNote = new Note(noteID, noteTitle, noteDetails);
            Toast.makeText(NotepadActivity.this, noteTitle + "\n" + noteDetails, Toast.LENGTH_SHORT).show();
            manager.updateNote(nNewNote);           
            break;
        }

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

After manager.updateNote(nNewNote); I tried to use adapter.notifyDataSetChanged(); but that didn't work - I didn't see the change in the ListView. Perhaps I used it wrong, perhaps I should use something else...

So how can I make the ListView refresh? How can I see change immediately, without restarting the app?

Thank you!

1
  • I have an issue where the onActivityResults is called upon clicking the list item, and not waiting for the opened activity to finish first Commented Mar 8, 2015 at 18:18

1 Answer 1

3

When you return from the edit activity, set the list adapter again to refresh the ListView. You have to manually refresh the ListView each time if you want to see any updates. Hope this helps.

Sign up to request clarification or add additional context in comments.

3 Comments

Tried it like this manager.updateNote(nNewNote); adapter = new ArrayAdapter<Note>(this, R.layout.note, lstAllNotes); but it still didn't work, still no refresh...
no, you have to setListadapter() again like you did in the onCreate() method. Make a new method to populate the Listview and call it in the onResume method of your activity. This way, it gets refreshed everytime you come back to your activity.
solved recreating the adapter then setting to listview in onActiviryResult()

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.