当我在Android中的sqliteDB中插入新行时,它将始终在数据库中占用其前一行吗?
模型类:
ModelProducts:
public class ModelProducts {
private String prodId;
private String prodName;
private String prodDesc;
private String prodPrice;
public ModelProducts() {
super();
}
public ModelProducts(String prodId, String prodName, String prodDesc, String prodPrice) {
this.prodId = prodId;
this.prodName = prodName;
this.prodDesc = prodDesc;
this.prodPrice = prodPrice;
}
public ModelProducts(String prodId, String prodName, String prodPrice){
this.prodId = prodId;
this.prodName = prodName;
this.prodPrice = prodPrice;
}
public String getProdId() {
return prodId;
}
public String getProdName() {
return prodName;
}
public String getProdDesc() {
return prodDesc;
}
public String getProdPrice() {
return prodPrice;
}
public void setProdId(String prodId) {
this.prodId = prodId;
}
public void setProdName(String prodName) {
this.prodName = prodName;
}
public void setProdDesc(String prodDesc) {
this.prodDesc = prodDesc;
}
public void setProdPrice(String prodPrice) {
this.prodPrice = prodPrice;
}
}
ModelCartItem:
public class ModelCartItem {
private ModelProducts product;
private int productQuantity;
public ModelCartItem() {
super();
}
public ModelProducts getProduct() {
return product;
}
public int getProductQuantity() {
return productQuantity;
}
public void setProduct(ModelProducts product) {
this.product = product;
}
public void setProductQuantity(int productQuantity) {
this.productQuantity = productQuantity;
}
}
在DatabaseHelper类
中// Database Version
private static final int DATABASE_VERSION = 1;
// Database Name
private static final String DATABASE_NAME = "shoppingCart";
// Table Names
private static final String TABLE_CART = "cart";
//CART Table - column names
private static final String KEY_PRODID = "prod_id";
private static final String KEY_PROD_NAME = "prod_name";
private static final String KEY_PROD_PRICE = "prod_price";
private static final String KEY_PROD_QUANTITY = "prod_quantity";
public DatabaseHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
创建表:
// Cart table create statement
private static final String CREATE_TABLE_CART = "CREATE TABLE " + TABLE_CART
+ "(" + KEY_PRODID + " TEXT PRIMARY KEY,"
+ KEY_PROD_NAME + " TEXT,"
+ KEY_PROD_PRICE + " TEXT,"
+ KEY_PROD_QUANTITY + " TEXT"
+ ")";
@Override
public void onCreate(SQLiteDatabase db) {
// creating required tables
db.execSQL(CREATE_TABLE_CART);
}
插入:
public void addtoCart(ModelCartItem item, int productQuantity) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(KEY_PRODID, item.getProduct().getProdId());
values.put(KEY_PROD_NAME, item.getProduct().getProdName());
values.put(KEY_PROD_PRICE, item.getProduct().getProdPrice());
values.put(KEY_PROD_QUANTITY, item.getProductQuantity());
// insert row
db.insert(TABLE_CART, null, values);
db.close();
}
当我在Activity 1中使用下面的代码添加Item时,即ProductActivity其中datamodel是ModelProducts类的对象,而ModelProducts类中的数据来自onclick of button。
在这个Activity中,我在onCreate()方法中创建了一个Db helper的对象
DatabaseHelper databaseHelper = new DatabaseHelper(this);
这是在onCreate()方法
中在DB中插入新行的代码 Log.d("TAG", "Product Id: "+ dataModel.getProdId());
Log.d(TAG, "Product Name: " + dataModel.getProdName());
Log.d(TAG, "Product Description: " + dataModel.getProdDesc());
Log.d(TAG, "Product Price: " + dataModel.getProdPrice());
int productQuantity = 1;
ModelCartItem item = new ModelCartItem();
item.setProduct(dataModel);
item.setProductQuantity(productQuantity);
databaseHelper.addtoCart(item, productQuantity);
之后我想将所有添加的数据检索到另一个活动,即BasketActivity,所以问题是最后插入的行值是否在行的列表项中。
在onCreate()方法的Basket Activity中,运行以下代码
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_basket);
databaseHelper = new DatabaseHelper(this);
listView = (ListView)findViewById(R.id.listViewProduct);
imagebtnCategory = (ImageButton)findViewById(R.id.imagebtnCategory);
ArrayList<ModelCartItem>arrayList3 = databaseHelper.getAllCartItem();
for(ModelCartItem cartItem: arrayList3){
Log.d("ID product: ", cartItem.getProduct().getProdId());
Log.d("Name product: ", cartItem.getProduct().getProdName());
Log.d("Price product: ", cartItem.getProduct().getProdPrice());
Log.d("Quantity product: ", ""+cartItem.getProductQuantity());
}
}
这是从DB
获取所有项目的方法public ArrayList<ModelCartItem> getAllCartItem() {
ModelProducts modelProducts = new ModelProducts();
ModelCartItem cartItem = new ModelCartItem();
ArrayList<ModelCartItem> todos = new ArrayList<ModelCartItem>();
String selectQuery = "SELECT * FROM " + TABLE_CART;
Log.e(LOG, selectQuery);
SQLiteDatabase db = this.getReadableDatabase();
Cursor c = db.rawQuery(selectQuery, null);
// looping through all rows and adding to list
if (c.moveToFirst()) {
do {
modelProducts.setProdId((c.getString((c.getColumnIndex(KEY_PRODID)))));
modelProducts.setProdName((c.getString((c.getColumnIndex(KEY_PROD_NAME)))));
modelProducts.setProdPrice((c.getString((c.getColumnIndex(KEY_PROD_PRICE)))));
cartItem.setProduct(modelProducts);
cartItem.setProductQuantity(Integer.parseInt(c.getString(c.getColumnIndex(KEY_PROD_QUANTITY))));
// adding to cartItem list
todos.add(cartItem);
} while (c.moveToNext());
}
return todos;
}
输入是:
Id 279的值是
Product Name is Mafalde ai calamari,
Product Price is 9.00
Quantity is 1
Id 310的值是
Product Name is Italiano,
Product Price is 5.00
Quantity is 2
然后输出将像LogCat中的那样:
ID Product: 279
Name Product: Mafalde ai calamari
Price Product: 9.00
Quantity product: 1
ID Product: 279
Name Product: Mafalde ai calamari
Price Product: 9.00
Quantity product: 1
这里,id 279的值重复两次,但我输入为id 279和310。
最后,我的问题是我想要所有那些存在于Arraylist之上的项目?还有如上所示不重复的项目输出?
我想要那样的输出
ID Product: 279
Name Product: Mafalde ai calamari
Price Product: 9.00
Quantity product: 1
ID Product: 310
Name Product: Italiano
Price Product: 5.00
Quantity product: 2
图像:
答案 0 :(得分:0)
在getAllCartItem()
中,移动
ModelProducts modelProducts = new ModelProducts();
ModelCartItem cartItem = new ModelCartItem();
在do
- while
循环中,以便为每一行创建新的模型对象,而不是之前重用的那些对象。