我正在從使用光標適配器填充的listview中刪除項目。即使我使用了notifyDataSetChanged(),但它並沒有立即刷新。我可以看到之前的活動再次來到這個頁面後的區別。CursorAdapter ListView刷新
您的回答是更多的讚賞。
這裏是我的適配器代碼:
@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;
}
UPDATE:
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() – barq
更新代碼。請 –