【问题标题】:CursorAdapter ListView refreshCursorAdapter ListView 刷新
【发布时间】:2016-03-03 07:01:47
【问题描述】:

我正在从使用光标适配器填充的列表视图中删除一个项目。即使我使用了 notifyDataSetChanged()。但它并没有立即刷新。我可以从以前的活动再次来到此页面后看到不同。

您的回答更受欢迎。

这是我的适配器代码:

@Override
public void onClick(View view) {
    switch (view.getId()) {
        case R.id.remove:
            dbUtil.open();
            String delItem = viewHolder.cartProduct.getText().toString();
            Cursor Cartcursor = dbUtil.getCartID(delItem);
            if (Cartcursor != null && Cartcursor.moveToFirst()) {
                Cartcursor.moveToFirst();
                String strCartProductID = Cartcursor.getString(Cartcursor.getColumnIndex(DbHelper.CART_PDT_ID));
                dbUtil.deleteCart(strCartProductID, delItem);
                Toast.makeText(contextNew, "Cart Item " + "RowId" + strCartProductID + " Product Id" + delItem, Toast.LENGTH_SHORT).show();
                Toast.makeText(contextNew, "Deleted Successfully", Toast.LENGTH_SHORT).show();
            }
            break;
}

更新:

MyFragment.class

dbUtil.open();
    cartcursor = dbUtil.getCartItem();

    txtCartCount.setText("" + cartcursor.getCount());

    if (cartcursor != null && cartcursor.moveToFirst()) {
        cartcursor.moveToFirst();
        cartAdapter = new CartCursorAdapter(context, cartcursor, MYFRAGMENT, true);
        cartList.setAdapter(cartAdapter);
        cartList.setTextFilterEnabled(true);
        cartAdapter.notifyDataSetChanged();
    } 

完美结果:

  @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.remove:
                dbUtil.open();
                String delItem = viewHolder.cartProduct.getText().toString();
                Cursor Cartcursor = dbUtil.getCartID(delItem);
                if (Cartcursor != null && Cartcursor.moveToFirst()) {
                    Cartcursor.moveToFirst();
                    String strCartProductID = Cartcursor.getString(Cartcursor.getColumnIndex(DbHelper.CART_PDT_ID));
                    dbUtil.deleteCart(strCartProductID, delItem);

                    Cartcursor = dbUtil.getCartItem();
                    swapCursor(Cartcursor);
                    notifyDataSetChanged();
                }
                break;
    }

【问题讨论】:

  • 在调用 notifyDatasetChanged() 的位置显示代码
  • 更新了代码。请

标签: android android-cursoradapter


【解决方案1】:

使用 Have to Refresh Listview using Cursor。

您“刷新 [您的] 光标”,再次运行您的代码以获取 Cursor,使用您用于创建原始 Cursor 的代码(请在后台线程上)。您可以通过在CursorAdapter 上调用changeCursor()swapCursor() 来刷新您的ListView

【讨论】:

  • @ParamaSudha 很高兴为您提供帮助。
【解决方案2】:

如果您使用的是 LoaderManager.LoaderCallbacks,请尝试以下方法:

        @Override
        public Loader<Cursor> onCreateLoader(int id, Bundle args) {
            return new CursorLoader(...);
        }

        @Override
        public void onLoadFinished(Loader<Cursor> loader, Cursor data) {
            adapter.swapCursor(data);
        }

        @Override
        public void onLoaderReset(Loader<Cursor> loader) {
            adapter.swapCursor(null);
        }

关键是用新数据调用swapCursor()方法

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-06-29
    • 2012-06-18
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多