【发布时间】:2021-12-01 21:59:03
【问题描述】:
我正在尝试通过长按该行从我的 SQLite 表中删除项目(最终将使用一个按钮,但现在只是长按测试)。但是我正在努力删除正确的。对于上下文,当我向表中添加新行时,它们会得到一个 ID 号。然后我想在长按时使用该 ID 号删除它们。我已经删除了一些下面不相关的代码。
MainActivity.java:
public class MainActivity extends AppCompatActivity implements StockViewInterface {
private DBHandler db;
private Stock stock;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
RecyclerView stockView = findViewById(R.id.rvStock);
LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
stockView.setLayoutManager(linearLayoutManager);
stockView.setHasFixedSize(true);
db = new DBHandler(this);
stock = new Stock();
ArrayList<Stock> allStock = db.getAllStock();
if (allStock.size() > 0) {
stockView.setVisibility(View.VISIBLE);
StockAdapter stockAdapter = new StockAdapter(this, allStock, this);
stockView.setAdapter(stockAdapter);
}
else {
stockView.setVisibility(View.GONE);
Toast.makeText(this, "No Items!", Toast.LENGTH_LONG).show();
}
}
@Override
public void onLongItemClick(int position) {
Toast.makeText(this, "long pressed", Toast.LENGTH_SHORT).show();
db.deleteStockItem(db.getStockItem(stock.getID()));
}
}
DBHandler.java:
public class DBHandler extends SQLiteOpenHelper {
private static final int DATABASE_VERSION = 1;
private static final String DATABASE_NAME = "ITStock";
private static final String TABLE_STOCK = "stock";
private static final String KEY_ID = "id";
private static final String KEY_ITEM = "item";
private static final String KEY_CONDITION = "condition";
private static final String KEY_QUANTITY = "quantity";
private static final String KEY_LOCATION = "location";
private static final String KEY_CATEGORY = "category";
public DBHandler(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
// Creating Table
@Override
public void onCreate(SQLiteDatabase db) {
String CREATE_STOCK_TABLE = "CREATE TABLE " + TABLE_STOCK + "(" + KEY_ID
+ " INTEGER PRIMARY KEY,"
+ KEY_ITEM + " TEXT,"
+ KEY_CONDITION + " TEXT,"
+ KEY_QUANTITY + " INT,"
+ KEY_LOCATION + " TEXT,"
+ KEY_CATEGORY + " TEXT" + ")";
db.execSQL(CREATE_STOCK_TABLE);
}
// Upgrading Database
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// Drop older table if existed
db.execSQL("DROP TABLE IF EXISTS " + TABLE_STOCK);
// Create tables again
onCreate(db);
}
// Get a single stock item
Stock getStockItem(int id) {
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = db.query(TABLE_STOCK, new String[] { KEY_ID, KEY_ITEM, KEY_CONDITION, KEY_QUANTITY, KEY_LOCATION, KEY_CATEGORY}, KEY_ID + "=?",
new String[] {String.valueOf(id)}, null, null, null, null);
if (cursor != null)
cursor.moveToFirst();
Stock stock = new Stock(Integer.parseInt(cursor.getString(0)), cursor.getString(1), cursor.getString(2), cursor.getInt(3),
cursor.getString(4), cursor.getString(5));
return stock;
}
// Get all stock items
public ArrayList<Stock> getAllStock() {
String selectQuery = "SELECT * FROM " + TABLE_STOCK;
SQLiteDatabase db = this.getWritableDatabase();
ArrayList<Stock> stockList = new ArrayList<>();
Cursor cursor = db.rawQuery(selectQuery, null);
if (cursor.moveToFirst()) {
do {
int id = Integer.parseInt(cursor.getString(0));
String item = cursor.getString(1);
String condition = cursor.getString(2);
int quantity = cursor.getInt(3);
String location = cursor.getString(4);
String category = cursor.getString(5);
stockList.add(new Stock(id, item, condition, quantity, location, category));
} while (cursor.moveToNext());
}
cursor.close();
return stockList;
}
// Delete single stock item
public void deleteStockItem(Stock stock) {
SQLiteDatabase db = this.getWritableDatabase();
db.delete(TABLE_STOCK, KEY_ID + " = ?", new String[] {String.valueOf(stock.getID())});
db.close();
}
}
Stock.java:
public class Stock {
int id;
String item;
String condition;
int quantity;
String location;
String category;
public Stock(){ }
public Stock(int id, String item, String condition, int quantity, String location, String category) {
this.id = id;
this.item = item;
this.condition = condition;
this.quantity = quantity;
this.location = location;
this.category = category;
}
public Stock(String item, String condition, int quantity, String location, String category) {
this.item = item;
this.condition = condition;
this.quantity = quantity;
this.location = location;
this.category = category;
}
// Getters and setters
public int getID() {
return this.id;
}
public void setID(int id) {
this.id = id;
}
public String getItem() {
return this.item;
}
public void setItem(String item) {
this.item = item;
}
public String getCondition() {
return this.condition;
}
public void setCondition(String condition) {
this.condition = condition;
}
public int getQuantity() {
return this.quantity;
}
public void setQuantity(int quantity) {
this.quantity = quantity;
}
public String getLocation() {
return this.location;
}
public void setLocation(String location) {
this.location = location;
}
public String getCategory() {
return this.category;
}
public void setCategory(String category) {
this.category = category;
}
}
StockAdapter.java:
public class StockAdapter extends RecyclerView.Adapter<StockAdapter.ViewHolder> {
private Context context;
private ArrayList<Stock> listStock;
private ArrayList<Stock> mArrayList;
private DBHandler db;
private MainActivity mainActivity;
private StockViewInterface stockViewInterface;
StockAdapter(Context context, ArrayList<Stock> listStock, StockViewInterface stockViewInterface) {
this.context = context;
this.listStock = listStock;
this.mArrayList = listStock;
this.stockViewInterface = stockViewInterface;
db = new DBHandler(context);
mainActivity = new MainActivity();
}
@NonNull
@Override
public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
LayoutInflater inflater = LayoutInflater.from(parent.getContext());
View stockView = inflater.inflate(R.layout.stock_row, parent, false);
return new ViewHolder(stockView);
}
@Override
public void onBindViewHolder(ViewHolder holder, int position) {
final Stock stock = listStock.get(position);
holder.tvItem.setText(stock.getItem());
holder.tvCondition.setText(stock.getCondition());
holder.tvQuantity.setText(String.valueOf(stock.getQuantity()));
holder.tvLocation.setText(stock.getLocation());
holder.tvCategory.setText(stock.getCategory());
}
@Override
public int getItemCount() {
return listStock.size();
}
class ViewHolder extends RecyclerView.ViewHolder {
final TextView tvItem;
final TextView tvCondition;
final TextView tvQuantity;
final TextView tvLocation;
final TextView tvCategory;
View stockView;
ViewHolder(View stockView) {
super(stockView);
this.stockView = stockView;
stockView.setOnClickListener(v -> stockViewInterface.onItemClick(getAdapterPosition()));
stockView.setOnLongClickListener(v -> {
stockViewInterface.onLongItemClick(getAdapterPosition());
return false;
});
tvItem = stockView.findViewById(R.id.tvItem);
tvCondition = stockView.findViewById(R.id.tvCondition);
tvQuantity = stockView.findViewById(R.id.tvQuantity);
tvLocation = stockView.findViewById(R.id.tvLocation);
tvCategory = stockView.findViewById(R.id.tvCategory);
}
}
}
我的 deleteStockItem 方法正在 MainActivity 中使用以下内容(1 是 ID 号):
db.deleteStockItem(db.getStockItem(1))
我不想手动输入 ID 号,所以它需要类似于
db.deleteStockItem(db.getStockItem(getID))
但我不知道该怎么做。 我试过这样做:
db.deleteStockItem(db.getStockItem(stock.getID()));
并得到这个错误:
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.it_stock, PID: 14325
android.database.CursorIndexOutOfBoundsException: Index 0 requested, with a size of 0
at android.database.AbstractCursor.checkPosition(AbstractCursor.java:460)
at android.database.AbstractWindowedCursor.checkPosition(AbstractWindowedCursor.java:136)
at android.database.AbstractWindowedCursor.getString(AbstractWindowedCursor.java:50)
at com.example.it_stock.DBHandler.getStockItem(DBHandler.java:71)
at com.example.it_stock.MainActivity.onLongItemClick(MainActivity.java:74)
at com.example.it_stock.StockAdapter$ViewHolder.lambda$new$1$StockAdapter$ViewHolder(StockAdapter.java:71)
at com.example.it_stock.-$$Lambda$StockAdapter$ViewHolder$glwJYV6g13zGeNPhabWeayxaKNY.onLongClick(lambda)
at android.view.View.performLongClickInternal(View.java:5714)
at android.view.View.performLongClick(View.java:5672)
at android.view.View.performLongClick(View.java:5690)
at android.view.View$CheckForLongPress.run(View.java:22402)
at android.os.Handler.handleCallback(Handler.java:751)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6121)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:889)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:779)
【问题讨论】:
标签: java android sqlite android-sqlite