在数据库中创建表: -
@覆盖 public void onCreate(SQLiteDatabase db){
String CREATE_TABLE = "CREATE TABLE " + TABLE_NAME + "("
+ KEY_ID + " INTEGER PRIMARY KEY," + KEY_ITEMNAME + " TEXT,"
+ KEY_ITEMPRICE + " TEXT," + KEY_ITEMQUANTITY + " TEXT," + KEY_ITEMIMAGE + " TEXT" + ")";
db.execSQL(CREATE_TABLE);
}
在databasehandler类中插入数据的方法:
public void insertData(String itemname,String itemprice,String itemimage)抛出SQLiteException {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(KEY_ITEMNAME, itemname);
values.put(KEY_ITEMPRICE, itemprice);
values.put(KEY_ITEMIMAGE, itemimage);
db.insert(TABLE_NAME, null, values);
db.close();
}
数据插入数据库: -
dbh.insertData(jsonObj.getString( “DishName”),jsonObj.getString( “费率”), jsonObj.getString( “图像”));
从数据库中检索数据并添加到模型类中: -
Cursor c = dbh.getSingledData(id);
if (c.moveToFirst()) {
int id = c.getInt(0);
ItemName = c.getString(1);
ItemPrice = c.getString(2);
ItemImage = c.getString(4);
for (int i = 0; i < ItemName.length(); i++) {
PojoCart pojoCart = new PojoCart();
pojoCart.setmItemName(ItemName);
pojoCart.setmItemPrice(ItemPrice);
pojoCart.setmItemImage(ItemImage);
mCartList.add(pojoCart);
我的Adapter类是: -
PojoCart pojoCart = (PojoCart) getItem(position);
viewHolder.mTvItemName.setText(pojoCart.getmItemName());
viewHolder.mTvItemPrice.setText(pojoCart.getmItemPrice());
try {
URL url = new URL(pojoCart.getmItemImage());
Bitmap bmp = BitmapFactory.decodeStream(url.openConnection().getInputStream());
viewHolder.mImgItem.setImageBitmap(bmp);
} catch (Exception e) {
e.printStackTrace();
}
}