我在DB中将两个Office记录插入到Office表中。
@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
public void insertOffices(SQLiteDatabase db) {
ContentValues values = new ContentValues();
//Insert Orange tours.
Bitmap bitmap = ((BitmapDrawable) context.getDrawable(R.drawable.orange)).getBitmap();
byte[] temp = convertImage.bitmapToByte(bitmap);
values.put(DBContract.Office.COL_OFFNAME, "اورنج تورز");
values.put(DBContract.Office.COL_OFFIMAGE, temp);
values.put(DBContract.Office.COL_OFFDESC, "شركة سياحة وسفر");
values.put(DBContract.Office.COL_OFFLOCATION, "بديا" + "الشارع الرئيسي");
values.put(DBContract.Office.COL_OFFPHONE, "0554544545");
values.put(DBContract.Office.COL_OFFRATE, "10");
long newRowId;
newRowId = db.insert(
DBContract.Office.TABLE_NAME,
null,
values);
Log.d("Database", "insertBooks: new record id : " + newRowId);
db.insert(DBContract.Office.TABLE_NAME, null, values);
values.clear();
//Insert arsema
Bitmap bitmap1 = ((BitmapDrawable) context.getDrawable(R.drawable.orange)).getBitmap();
byte[] temp1 = convertImage.bitmapToByte(bitmap1);
values.put(DBContract.Office.COL_OFFNAME, "ارسيما");
values.put(DBContract.Office.COL_OFFIMAGE, temp1);
values.put(DBContract.Office.COL_OFFDESC, "شركة سياحة وسفر");
values.put(DBContract.Office.COL_OFFLOCATION, "جنين" + "الشارع الرئيسي");
values.put(DBContract.Office.COL_OFFPHONE, "0554544545");
values.put(DBContract.Office.COL_OFFRATE, "10");
newRowId = db.insert(
DBContract.Office.TABLE_NAME,
null,
values);
Log.d("Database", "insertBooks: new record id : " + newRowId);
db.insert(DBContract.Office.TABLE_NAME, null, values);
values.clear();
}
选择所有offices并返回offices列表的getOffices方法:
public List<Office> getOffices() {
List<Office> offices = new ArrayList<Office>();
String selectQuery = "SELECT * FROM " + DBContract.Office.TABLE_NAME;
SQLiteDatabase db = this.getWritableDatabase();
Cursor cursor = db.rawQuery(selectQuery, null);
if (cursor!=null && cursor.moveToFirst()) {
do {
Office office = new Office();
office.setOfficeID(Integer.parseInt(cursor.getString(cursor.getColumnIndex(DBContract.Office._ID))));
office.setOfficeName(cursor.getString(cursor.getColumnIndex(DBContract.Office.COL_OFFNAME)));
office.setOfficeLocation(cursor.getString(cursor.getColumnIndex(DBContract.Office.COL_OFFLOCATION)));
office.setOfficeImage(cursor.getBlob(cursor.getColumnIndex(DBContract.Office.COL_OFFIMAGE)));
office.setOfficeDescreption(cursor.getString(cursor.getColumnIndex(DBContract.Office.COL_OFFDESC)));
office.setOfficeRate(cursor.getInt(cursor.getColumnIndex(DBContract.Office.COL_OFFRATE)));
// Adding offices to list
offices.add(office);
} while (cursor.moveToNext());
}
return offices;
}
然后,我使用自定义适配器将数据填充到office RecyclerView
中。
虽然我只插入了两个办公室,但为什么返回一个4个办公室的列表?
发布于 2018-03-26 12:38:09
对于,两个记录的每个 (//Orange和//arsema)都有两个插入,如下所示:-
// 1st insert
newRowId = db.insert(
DBContract.Office.TABLE_NAME,
null,
values);
Log.d("Database", "insertBooks: new record id : " + newRowId);
//Duplicate insert (albeit with different id)
db.insert(DBContract.Office.TABLE_NAME, null, values);
相反,使用类似的(即每只插入1次) :-
//Now just 1 insert
Log.d("Database",
"insertBooks: new record id : " +
db.insert(DBContract.Office.TABLE_NAME, null, values)
);
P.S.也许对于第二个(//arsema)您应该使用:-
Bitmap bitmap1 = ((BitmapDrawable) context.getDrawable(R.drawable.aresma)).getBitmap(); //arsema bitmap not orange bitmap
https://stackoverflow.com/questions/49489635
复制相似问题