我无法从 SQLite 数据库和 RecyclerView 中删除项目。现在我的代码将从 recyclerview 中删除该行,但不会从数据库中删除。关于如何获取适配器位置以转换为从数据库中删除数据的任何提示?
库存.java
public class Inventory extends AppCompatActivity implements RVInterface{
private AlertDialog.Builder dialogBuilder;
private AlertDialog dialog;
private EditText addItemName, addItemQuantity;
private Button addItemBtn, cancelAddBtn;
private ImageView settingsButton, deleteItemBtn;
private InventoryDB DB;
private RecyclerView recyclerView;
private ArrayList<String> ids, names, qtys;
private RVAdapter rvAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_inventory_database);
DB = new InventoryDB(this);
ids = new ArrayList<>();
names = new ArrayList<>();
qtys = new ArrayList<>();
recyclerView = findViewById(R.id.rv_items);
displayItems();
rvAdapter = new RVAdapter(this, ids, names, qtys, this);
recyclerView.setAdapter(rvAdapter);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
settingsButton = findViewById(R.id.settingsButton);
settingsButton.setOnClickListener(view -> {
Intent intent=new Intent(Inventory.this, SMSAlertScreen.class);
startActivity(intent);
});
}
private void displayItems() {
Cursor cursor = DB.getItemsInDB();
if(cursor.getCount()==0) {
Toast.makeText(Inventory.this, "No items in inventory database!", Toast.LENGTH_LONG).show();
return;
} else {
while(cursor.moveToNext()) {
ids.add(cursor.getString(0));
names.add(cursor.getString(1));
qtys.add(cursor.getString(2));
}
}
}
public void addItem(View view) {
createNewAddItemDialog();
}
public void createNewAddItemDialog(){
dialogBuilder = new AlertDialog.Builder(this);
final View addItemPopupView = getLayoutInflater().inflate(R.layout.popup_additem, null);
addItemName = (EditText) addItemPopupView.findViewById(R.id.additempopup_itemname);
addItemQuantity = (EditText) addItemPopupView.findViewById(R.id.additempopup_itemquantity);
addItemBtn = (Button) addItemPopupView.findViewById(R.id.additempopup_add);
cancelAddBtn = (Button) addItemPopupView.findViewById(R.id.additempopup_cancel);
dialogBuilder.setView(addItemPopupView);
dialog = dialogBuilder.create();
dialog.show();
addItemBtn.setOnClickListener(v -> {
String itemName = addItemName.getText().toString();
String itemQuantity = addItemQuantity.getText().toString();
if (itemName.equals("") || itemQuantity.equals("")) {
//addError.setText("Please ensure both fields are filled out!");
} else {
Boolean itemExists = DB.checkItemExists(itemName);
if (itemExists){
//addError.setText("Item already exists!");
} else {
DB.addItem(itemName, Integer.parseInt(itemQuantity));
rvAdapter.addItem(itemName, itemQuantity);
dialog.dismiss();
Toast.makeText(Inventory.this, "Item added!", Toast.LENGTH_LONG).show();
}
}
});
cancelAddBtn.setOnClickListener(v -> dialog.dismiss());
}
@Override
public void onItemClick(int pos) {
}
}
RVAdapter.java
private Context context;
private ArrayList item_id, item_name, item_qty;
private final RVInterface rvInterface;
private InventoryDB inventoryDB;
public RVAdapter(Context context, ArrayList item_id, ArrayList item_name, ArrayList item_qty,
RVInterface rvInterface) {
this.context = context;
this.item_id = item_id;
this.item_name = item_name;
this.item_qty = item_qty;
this.rvInterface = rvInterface;
}
@NonNull
@Override
public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View v = LayoutInflater.from(context).inflate(R.layout.inventory_row,parent,false);
return new MyViewHolder(v, inventoryDB).linkAdapter(this);
}
@Override
public void onBindViewHolder(@NonNull MyViewHolder holder, int position) {
holder.item_id.setText(String.valueOf(item_id.get(position)));
holder.item_name.setText(String.valueOf(item_name.get(position)));
holder.item_qty.setText(String.valueOf(item_qty.get(position)));
}
@Override
public int getItemCount() {
return item_id.size();
}
public void addItem(String name, String qty){
item_id.add(item_id.size() + 1);
item_name.add(name);
item_qty.add(qty);
}
public void deleteItem(int id, InventoryDB inventoryDB) {
inventoryDB.deleteItem(id);
}
public static class MyViewHolder extends RecyclerView.ViewHolder {
TextView item_id, item_name, item_qty;
ImageView item_delete;
private RVAdapter rvAdapter;
public MyViewHolder(@NonNull View itemView, InventoryDB inventoryDB) {
super(itemView);
item_id = itemView.findViewById(R.id.item_id_text);
item_name = itemView.findViewById(R.id.item_name_text);
item_qty = itemView.findViewById(R.id.item_qty_text);
item_delete = itemView.findViewById(R.id.delete_item_btn);
item_delete.setOnClickListener(view -> {
rvAdapter.item_id.remove(getAdapterPosition());
rvAdapter.item_name.remove(getAdapterPosition());
rvAdapter.item_qty.remove(getAdapterPosition());
rvAdapter.notifyItemRemoved(getAdapterPosition());
});
}
public MyViewHolder linkAdapter(RVAdapter rvAdapter){
this.rvAdapter = rvAdapter;
return this;
}
}
}
库存数据库.java
public class InventoryDB extends SQLiteOpenHelper{
private static final String DATABASE_NAME = "inventory.db";
private static final int VERSION = 1;
public InventoryDB(Context context) {
super(context, DATABASE_NAME, null, VERSION);
}
private static final class InventoryTable {
private static final String TABLE = "inventory";
private static final String COL_ID = "_id";
private static final String COL_ITEM = "item";
private static final String COL_QTY = "quantity";
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("create table " + InventoryTable.TABLE + " (" +
InventoryTable.COL_ID + " integer primary key autoincrement, " +
InventoryTable.COL_ITEM + " text, " +
InventoryTable.COL_QTY + " integer " + ")");
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion,
int newVersion) {
db.execSQL("drop table if exists " + InventoryDB.InventoryTable.TABLE);
onCreate(db);
}
public Boolean checkForData() {
SQLiteDatabase db = getWritableDatabase();
Cursor cursor = db.rawQuery("select * from " + DATABASE_NAME, null);
if (cursor.moveToFirst())
return true;
else
return false;
}
public Boolean checkItemExists(String item) {
SQLiteDatabase db = getWritableDatabase();
Cursor cursor = db.rawQuery("Select * from inventory where item =?", new String[] {item});
if (cursor.getCount() > 0)
return true;
else
return false;
}
public void addItem(String item, int qty) {
SQLiteDatabase db = getWritableDatabase();
ContentValues values = new ContentValues();
values.put(InventoryDB.InventoryTable.COL_ITEM, item);
values.put(InventoryDB.InventoryTable.COL_QTY, qty);
long result = db.insert(InventoryDB.InventoryTable.TABLE, null, values);
if (result == -1) {
//failed
} else {
//added
}
}
public Boolean editItem(String item, int qty) {
SQLiteDatabase db = getWritableDatabase();
ContentValues values = new ContentValues();
values.put(InventoryTable.COL_QTY, qty);
Cursor cursor = db.rawQuery("select * from " + InventoryTable.TABLE + " where item = ?", new String[]{item});
if(cursor.getCount()>0){
long result = db.update(InventoryTable.TABLE, values, "item=?", new String[]{item});
if (result == -1){
return false;
} else {
return true;
}
} else {
return false;
}
}
public Boolean deleteItem(int id) {
SQLiteDatabase db = getWritableDatabase();
Cursor cursor = db.rawQuery("Select * from inventory where id = ?", new String[] { String.valueOf(id) });
if (cursor.getCount() > 0) {
db.delete(InventoryTable.TABLE, InventoryTable.COL_ID + " = ?", new String[]{ String.valueOf(id) });
return true;
} else {
return false;
}
}
public Cursor getItemsInDB() {
SQLiteDatabase db = getWritableDatabase();
Cursor cursor = db.rawQuery("select * from inventory", null);
return cursor;
}
}