Refresh ListView with ArrayAdapter after editing an Item

…衆ロ難τιáo~ 提交于 2019-12-23 16:23:17

问题


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:


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.



来源:https://stackoverflow.com/questions/11815109/refresh-listview-with-arrayadapter-after-editing-an-item

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!