【发布时间】: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