我在我的应用中实施了一个快餐栏,其中有一个撤销按钮来撤消删除。目前它的工作原理是,当用户删除项目时,我通过从数据库执行查询来保留项目的光标,然后从数据库中删除该项目,并通过notifyChange()
更新列表。当用户点击小吃栏上的撤消按钮时,我从保存的光标中提取内容,创建一个新的ContentValue并将其插入数据库,再次调用notifyChange()
来更新列表。
我不喜欢这种方法。数据库操作太多了
有什么方法可以从ListView中删除/隐藏项目,只在小吃栏消失时删除项目?
我尝试将可见性设置为GONE或INVISIBLE但是在ListView的中间留下了一个空白区域
我正在使用CursorAdapter来填充ListView。每个列表项上都有一个3点菜单,显示删除选项
这是我当前的CursorAdapter代码
public class ProductCursorAdapter extends CursorAdapter {
private static final String LOG_TAG = ProductCursorAdapter.class.getSimpleName();
protected ContentResolver contentResolver;
public ProductCursorAdapter(Context context, Cursor c, ContentResolver contentResolver) {
super(context, c);
this.contentResolver = contentResolver;
}
@Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
return LayoutInflater.from(context).inflate(R.layout.list_item, parent, false);
}
@Override
public void bindView(final View view, final Context context, final Cursor cursor) {
ImageView thumbnail = (ImageView) view.findViewById(R.id.thumbnail);
TextView name = (TextView) view.findViewById(R.id.name);
TextView quantity = (TextView) view.findViewById(R.id.quantity);
TextView price = (TextView) view.findViewById(R.id.price);
TextView supplier = (TextView) view.findViewById(R.id.supplier);
final long id = cursor.getLong(cursor.getColumnIndexOrThrow(ProductEntry._ID));
ImageView threeDotMenu = (ImageView) view.findViewById(R.id.three_dot_menu);
threeDotMenu.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
PopupMenu popup = new PopupMenu(context, v);
popup.getMenuInflater().inflate(R.menu.list_item_context_menu, popup.getMenu());
popup.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
@Override
public boolean onMenuItemClick(MenuItem item) {
switch (item.getItemId()) {
case R.id.context_delete_product:
//the uri of the product we're deleting
Uri currentProductUri = ContentUris.withAppendedId(ProductEntry.CONTENT_URI, id);
//What to extract from the database
String[] projection = {
ProductEntry._ID,
ProductEntry.COLUMN_NAME,
ProductEntry.COLUMN_QUANTITY,
ProductEntry.COLUMN_PRICE,
ProductEntry.COLUMN_SUPPLIER,
ProductEntry.COLUMN_IMAGE
};
//retrieve the product from the database before deleting in case
//we need to restore it later
Cursor cursor = contentResolver.query(currentProductUri,
projection,
null,
null,
null,
null);
cursor.moveToFirst();
Snackbar snackbar = Snackbar.make(view.getRootView().findViewById(R.id.coordinator_layout), "Product Deleted", Snackbar.LENGTH_LONG);
snackbar.setAction("Undo", new MyUndoListener(cursor));
snackbar.show();
contentResolver.delete(currentProductUri, null, null);
return true;
default:
return false;
}
}
});
popup.show();
}
});
. . .
}
public class MyUndoListener implements View.OnClickListener {
Cursor cursor = null;
public MyUndoListener(Cursor cursor) {
this.cursor = cursor;
}
@Override
public void onClick(View v) {
contentResolver.insert(ProductEntry.CONTENT_URI, extractFromCursor(cursor));
}
private ContentValues extractFromCursor(Cursor cursor) {
int id = cursor.getInt(cursor.getColumnIndexOrThrow(ProductEntry._ID));
String name = cursor.getString(cursor.getColumnIndexOrThrow(ProductEntry.COLUMN_NAME));
int quantity = cursor.getInt(cursor.getColumnIndexOrThrow(ProductEntry.COLUMN_QUANTITY));
int price = cursor.getInt(cursor.getColumnIndexOrThrow(ProductEntry.COLUMN_PRICE));
String supplier = cursor.getString(cursor.getColumnIndexOrThrow(ProductEntry.COLUMN_SUPPLIER));
byte[] image = cursor.getBlob(cursor.getColumnIndexOrThrow(ProductEntry.COLUMN_IMAGE));
ContentValues value = new ContentValues();
value.put(ProductEntry._ID, id);
value.put(ProductEntry.COLUMN_NAME, name);
value.put(ProductEntry.COLUMN_PRICE, price);
value.put(ProductEntry.COLUMN_QUANTITY, quantity);
value.put(ProductEntry.COLUMN_SUPPLIER, supplier);
value.put(ProductEntry.COLUMN_IMAGE, image);
cursor.close();
return value;
}
}
}
答案 0 :(得分:0)
Nishan,For This制作数据库值的Arraylist,然后绑定listview。每当你想删除一个项目(不是从你的数据库中),然后制作没有该项目的arraylist的副本,并再次绑定列表视图。
因此,您的数据库保持不变,并且可以在撤消时再次使用它。 那么你要删除哪个项目,在没有该项目的情况下从orignal arraylst绑定一个新的arraylist并使用它。