在 SimpleCursorAdapter 上使用 notifyDataSetChanged 不起作用

Posted

技术标签:

【中文标题】在 SimpleCursorAdapter 上使用 notifyDataSetChanged 不起作用【英文标题】:Using notifyDataSetChanged on SimpleCursorAdapter does not work 【发布时间】:2012-12-11 16:02:15 【问题描述】:

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

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

我尝试将我的全局从 SimpleCursorAdapter 更改为 BaseAdapater(因为它扩展了 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);

列表视图项内删除按钮的代码:

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;
    

使用onResume() 记录我。

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

onResume() 的代码正确显示列表并删除了列表视图项:

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

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

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

所以无论如何,有人可以指出如何在无需重新创建适配器的情况下刷新列表 - 还是我所做的最好的方法?

【问题讨论】:

我在这里回答了一个类似的问题 - ***.com/questions/13953171/… 非常感谢芒果! 【参考方案1】:

mango 对他的决议的评论中的 URL 运行良好。

我刚刚把onResume()里面的代码改成了这样:

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

    super.onResume();

由于onResume() 在有人添加或编辑项目后已经被调用,我认为在按下删除按钮时调用它不会有什么坏处,因为它不再重新创建适配器,而是简单地更改光标。

【讨论】:

以上是关于在 SimpleCursorAdapter 上使用 notifyDataSetChanged 不起作用的主要内容,如果未能解决你的问题,请参考以下文章

SimpleCursorAdapter无法在MainActivity中使用

使用 EditText 过滤 SimpleCursorAdapter 支持的 ListView

使用 SimpleCursorAdapter 基于 sqlite 查询更新列表视图

使用 SimpleCursorAdapter 从 Cursor 更改值

SimpleCursorAdapter使用代码

在使用 SimpleCursorAdapter 时从联系人 ID 中检索和显示联系人姓名