Using notifyDataSetChanged on SimpleCursorAdapter does not work

穿精又带淫゛_ 提交于 2019-12-21 22:36:57

问题


In my code, right now, in order to properly refresh a listview I have to re-fetch my database information and recreate the SimpleCursorAdapter.

For example, I have a button inside a listview. When this button is clicked, it removes the entry from the database for the listview item. So all I want to happen is have the item removed from the listview, without having to recreate the adapter.

I've tried changing my global from SimpleCursorAdapter to BaseAdapater (because it extends SimpleCursorAdapater and allows for the notifyDataSetChanged() function to be used), but it still doesn't work.

Here is the code I'm using now (which does work):

Code for global declarations and onCreate():

private RoutinesDataSource datasource;
private SimpleCursorAdapter dataAdapter;
private boolean isEditing = false;
private Toast toast_deleted;
private String[] columns = new String[] { MySQLiteHelper.COLUMN_NAME };
private int[] to;

@SuppressLint("ShowToast")
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_routines);

    toast_deleted = Toast.makeText(this, "", Toast.LENGTH_SHORT);
    datasource = new RoutinesDataSource(this);
    datasource.open();

    Cursor cursor = datasource.fetchAllRoutines();
    to = new int[] { R.id.listitem_routine_name };
    dataAdapter = new SimpleCursorAdapter(this, R.layout.listitem_routine, cursor, columns, to, 0);
    setListAdapter(dataAdapter);
}

Code for the delete button inside the listview item:

public void onClick(View view) {        
    ListView l = getListView();
    int position = l.getPositionForView(view);

    Cursor cursor = ((SimpleCursorAdapter)l.getAdapter()).getCursor();
    cursor.moveToPosition(position);
    long id = cursor.getLong(cursor.getColumnIndex(MySQLiteHelper.COLUMN_ID));
    String name = cursor.getString(cursor.getColumnIndex(MySQLiteHelper.COLUMN_NAME));

    switch (view.getId()) { 

        case R.id.button_routine_delete:
            toast_deleted.setText(getString(R.string.toast_routine_deleted));
            toast_deleted.show();
            datasource.deleteRoutine(id);
            onResume();
            break;
    }
}

Take note of me using onResume().

I know that datasource.deleteRoutine(id) works because when I close the activity and reopen it the list item is gone.

Code for onResume() which shows the list properly with the listview item removed:

@Override
protected void onResume() {
    datasource.open();
    Cursor cursor = datasource.fetchAllRoutines();

    if (isEditing) {
        to = new int[] { R.id.listitem_routine_edit_name };
        dataAdapter = new SimpleCursorAdapter(this, R.layout.listitem_routine_edit, cursor, columns, to, 0);
        setListAdapter(dataAdapter);
    }
    else {
        to = new int[] { R.id.listitem_routine_name };
        dataAdapter = new SimpleCursorAdapter(this, R.layout.listitem_routine, cursor, columns, to, 0);
        setListAdapter(dataAdapter);
    }

    super.onResume();
}

I just think its bad practice to recreate the adapter every time I simply want a list item removed that has been removed from the database. Like I said I've tried notifyDataSetChanged with a BaseAdapater and it simply does not work.

Also take note of the isEditing boolean. That is set to true if the edit button is clicked in the action bar, which shows the delete button. This is useful because I also have an edit button which starts an activity when clicked, so when they come back after they are done editing it still shows the buttons for the user.

So anyways, can someone point me out how to refresh the list without having to recreate the adapter - or is what I've done the best method?


回答1:


The URL in mango's comment to his resolution worked perfectly.

I just changed the code inside onResume() to this:

    datasource.open();
    Cursor cursor = datasource.fetchAllRoutines();
    dataAdapter.changeCursor(cursor);

    super.onResume();

Since onResume() is already called after someone adds or edits an item, I figured it wouldn't hurt to call it when the delete button is pressed considering it no longer recreates the adapter, rather simply changes the cursor.



来源:https://stackoverflow.com/questions/14034770/using-notifydatasetchanged-on-simplecursoradapter-does-not-work

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