Android 游标适配器列表视图刷新

Android 游标适配器列表视图刷新,android,android-cursoradapter,Android,Android Cursoradapter,我正在从使用游标适配器填充的listview中删除一个项。甚至我也使用了notifyDataSetChanged。但它没有立即刷新。我可以在再次访问此页面后看到与以前活动的区别 谢谢你的回答 以下是我的适配器代码: @Override public void onClick(View view) { switch (view.getId()) { case R.id.remove: dbUtil.open(); String

我正在从使用游标适配器填充的listview中删除一个项。甚至我也使用了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();
    } 
完美的结果:

使用必须使用光标刷新Listview


请使用在后台线程上创建原始光标时使用的代码,通过再次运行代码获取光标来刷新[您的]光标。通过在游标适配器上调用changeCursor或swapCursor来刷新列表视图。

如果使用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方法

在调用notifyDatasetChangedUpdated代码的位置显示代码。Please@ParamaSudha很高兴帮助你。
        @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);
        }