【发布时间】:2021-04-17 17:39:32
【问题描述】:
我是 Android Studio 的新手,我正在尝试创建一个允许用户创建自己的库存项目的库存应用程序。我正在使用 SQlite 数据库来执行此操作。我已经创建了存储结果的 DBHelper 类和 Item 类。
但是,我不确定如何在我在一些 FIXME cmets 中编写的 DisplayInventory 类 (MainActivity) 中继续此操作,以帮助显示我在说什么。
DisplayInventory 类
// TESTING SQLite CODE HERE
public void insertItem(int position) {
//FIXME: This is where I need to ADD items to the database
// inventoryDB.addItem();
mInventoryItemList.add(position, new InventoryItem(R.drawable.ic_delete, "Tap to Edit", "Items: ", (android.widget.Button) Button, (android.widget.Button) Button));
mAdapter.notifyItemInserted(position);
}
public void removeItem(int position) {
// FIXME: This is where I DELETE items from database
// inventoryDB.deleteItem(); <== This method needs to be written in InventoryDBHelper class
mInventoryItemList.remove(position);
mAdapter.notifyItemRemoved(position);
}
public void createExampleList() {
mInventoryItemList = new ArrayList<>();
// get all items from the database
Item[] items = inventoryDB.getAllItems();
// loop through all items to build the array list
for (int i = 0; i < items.length; ++i){
// FIXME: this is where I need to build my ArrayList based on the contents of the DB
// for new users this will be empty. However, as they add items then the database
// will be populated and this will return data the next time.
}
}
//TESTING CODE ENDS HERE
InventoryDBHelper
public class InventoryDBHelper extends SQLiteOpenHelper {
private Context context;
private static final String DATABASE_NAME = "InventoryList.db";
private static final int DATABASE_VERSION = 1;
private static final String TABLE_NAME = "my_inventory";
private static final String COLUMN_ID = "_id";
private static final String COLUMN_TITLE = "item_name";
private static final String COLUMN_OWNER = "item_owner";
private static final String COLUMN_ITEMNUMBER = "number_items";
public InventoryDBHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
this.context = context;
}
@Override
public void onCreate(SQLiteDatabase sqLiteDatabase) {
String query = "CREATE TABLE " + TABLE_NAME +
" (" + COLUMN_ID + " INTEGER PRIMARY KEY AUTOINCREMENT," +
COLUMN_TITLE + " TEXT, " +
COLUMN_OWNER + " TEXT, " +
COLUMN_ITEMNUMBER + " INTEGER);";
sqLiteDatabase.execSQL(query);
}
@Override
public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i1) {
sqLiteDatabase.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
onCreate(sqLiteDatabase);
}
void addItem(String title, String owner, int numItem) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues cv = new ContentValues();
cv.put(COLUMN_TITLE, title);
cv.put(COLUMN_OWNER, owner);
cv.put(COLUMN_ITEMNUMBER, numItem);
long result = db.insert(TABLE_NAME, null, cv);
if(result == -1) {
Toast.makeText(context, "FAILED", Toast.LENGTH_SHORT).show();
}
else {
Toast.makeText(context, "Added Successful", Toast.LENGTH_SHORT).show();
}
}
public Item[] getAllItems(){
// list of items to return
Item[] returnItems;
// initialize a readable database connection
SQLiteDatabase db = this.getReadableDatabase();
String query = "SELECT " +
COLUMN_ID + "," +
COLUMN_TITLE + "," +
COLUMN_OWNER + "," +
COLUMN_ITEMNUMBER +
" FROM " + TABLE_NAME + ";";
Cursor cursor = db.rawQuery(query, null);
returnItems = unpackRecordData(cursor);
cursor.close();
return returnItems;
}
private Item[] unpackRecordData(Cursor cursor){
Item[] items = new Item[cursor.getCount()];
int i = 0;
while (cursor.moveToNext()){
Item item = new Item(
// Item.id
cursor.getInt(
cursor.getColumnIndexOrThrow(COLUMN_ID)
),
// Item.title
cursor.getString(
cursor.getColumnIndexOrThrow(COLUMN_TITLE)
),
// Item.owner
cursor.getString(
cursor.getColumnIndexOrThrow(COLUMN_OWNER)
),
// Item.itemNumber
cursor.getInt(
cursor.getColumnIndexOrThrow(COLUMN_ITEMNUMBER)
)
);
// add Item to the list of items
items[i] = item;
}
return items;
}
}
物品类
public class Item {
// Item Database columns to properties
private int id;
private String title;
private String owner;
private int itemNumber;
// class constructor
public Item(int id, String title, String owner, int itemNumber){
this.id = id;
this.title = title;
this.owner = owner;
this.itemNumber = itemNumber;
}
// getters and setters
public void setId(int id){
this.id = id;
}
public int getId(){
return this.id;
}
public void setTitle(String title){
this.title = title;
}
public String getTitle(){
return this.title;
}
public void setOwner(String owner){
this.owner = owner;
}
public String getOwner(){
return this.owner;
}
public void setItemNumber(int itemNumber){
this.itemNumber = itemNumber;
}
public int getItemNumber(){
return this.itemNumber;
}
}
【问题讨论】:
-
使用你的 db 类作为单例,那么所有对 db 的操作都应该在后台线程中
标签: java sqlite android-studio