在SimpleCursorAdapter上使用notifyDataSetChanged不起作用

sca*_*and 2 android listview simplecursoradapter baseadapter

在我的代码中,现在,为了正确刷新列表视图,我必须重新获取我的数据库信息并重新创建SimpleCursorAdapter.

例如,我在listview中有一个按钮.单击此按钮时,它将从数据库中删除listview项的条目.所以我想要发生的是将项目从列表视图中删除,而不必重新创建适配器.

我已经尝试将我的全局更改SimpleCursorAdapterBaseAdapater(因为它扩展SimpleCursorAdapater并允许使用该notifyDataSetChanged()函数),但它仍然无效.

这是我现在使用的代码(它确实有效):

global声明代码和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);
}
Run Code Online (Sandbox Code Playgroud)

listview项目中删除按钮的代码:

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;
    }
}
Run Code Online (Sandbox Code Playgroud)

记下我使用onResume().

我知道这是datasource.deleteRoutine(id)有效的,因为当我关闭活动并重新打开它时,列表项就消失了.

onResume()的代码,它删除了listview项,正确显示了列表:

@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();
}
Run Code Online (Sandbox Code Playgroud)

我只是认为每次我只想删除已从数据库中删除的列表项时重新创建适配器是不好的做法.就像我说的,我已经尝试使用BaseAdapater的notifyDataSetChanged,它根本不起作用.

还要注意isEditing布尔值.如果在操作栏中单击编辑按钮(显示删除按钮),则设置为true.这很有用,因为我还有一个编辑按钮,可以在单击时启动活动,因此当它们在完成编辑后返回时,它仍会显示用户的按钮.

所以无论如何,有人可以指出我如何刷新列表而不必重新创建适配器 - 或者是我做过最好的方法?

sca*_*and 5

芒果对他的决议的评论中的URL工作得很好.

我只是将内部代码更改为onResume():

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

    super.onResume();
Run Code Online (Sandbox Code Playgroud)

既然onResume()已经被称为有人添加或编辑一个项目后,我想它不会伤害的时候调用它删除按钮被按下考虑不再重现适配器,而不是简单地改变光标.