我已经搜索过其他主题以解决我的问题,但我没有找到好的主题。所以我会解释我想做什么。我在网站上恢复了一个这样的 JSON 文件:
{
"prod": {
"ean": "000000000000",
"cat": "category ...",
"title": "title ...",
"brand": "brand ...",
"intake": [
{
"carbohydrates": "10g"
},
{
"protein": "8g"
},
{
"lipids": "0g"
}
],
"others": "blablabla"
}
}
我可以很好地解析它,并在 TextViews 等不同的字段中显示它,我使用 ListView 来显示“摄入”数组。现在我想将所有这些值放入数据库中。除了这个数组(我使用 HashMap)之外,我可以将所有内容都放在我的数据库中。知道这个数组的元素数量可以变化,有人知道如何进行吗?
谢谢,我为我糟糕的英语道歉。如果你问我,我可以提供更多信息。
有我的数据库处理程序:
public class Base extends SQLiteOpenHelper {
private static int DATABASE_VERSION = 1;
private static final String DATABASE_NAME = "etiquettesDatabase";
private static final String TABLE_ETIQUETTE = "etiquettes";
private static final String KEY_ID = "id";
private static final String KEY_TITLE = "title";
private static final String KEY_BRAND = "brand";
private static final String KEY_INTAKE = "intake"; // i don't know how to proceed here
private static final String KEY_FAMILY = "family";
private static final String KEY_OTHERS = "others";
public Base(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
String CREATE_CONTACTS_TABLE =
"CREATE TABLE " + TABLE_ETIQUETTE + "("
+ KEY_ID + " INTEGER PRIMARY KEY,"
+ KEY_TITLE + " TEXT,"
+ KEY_BRAND + " TEXT,"
+ KEY_FAMILY + " TEXT,"
+ KEY_INTAKE + " TEXT,"
+ KEY_OTHERS + " TEXT" + ")";
db.execSQL(CREATE_CONTACTS_TABLE);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS " + TABLE_ETIQUETTE);
onCreate(db);
}
void addEtiquette(Etiquette etiquette) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(KEY_TITLE, etiquette.get_titre());
values.put(KEY_BRAND, etiquette.get_marque());
values.put(KEY_FAMILY, etiquette.get_famille());
//values.put(KEY_INTAKE, etiquette.get_nutrition());
values.put(KEY_OTHERS, etiquette.get_autres());
db.insert(TABLE_ETIQUETTE, null, values);
db.close();
}
Etiquette getEtiquette(int id) {
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = null;
if (db != null) {
cursor = db.query(TABLE_ETIQUETTE, new String[]{KEY_ID,
KEY_TITLE, KEY_BRAND, KEY_INTAKE, KEY_OTHERS}, KEY_ID + "=?",
new String[]{String.valueOf(id)}, null, null, null, null);
}
Etiquette etiquette;
/*
if (cursor != null && cursor.moveToFirst()) {
cursor.moveToFirst(); Log.v("test", "cursor /null");
etiquette = new Etiquette(Integer.parseInt(cursor.getString(0)),
cursor.getString(1),
cursor.getString(2),
cursor.getString(3),
cursor.getClass(),
cursor.getString(5)
);
} else {
etiquette=null; Log.v("test", "etiquette = null");
}
*/
cursor.close();
//Log.v("test", "cursor"+String.valueOf(Integer.parseInt(cursor.getString(0))));
/*
Etiquette etiquette = new Etiquette(Integer.parseInt(cursor.getString(0)),
cursor.getString(1),
cursor.getString(2),
cursor.getString(3),
cursor.getString(4));
*/
// return etiquette;
return null;
}
public List<Etiquette> getAllEtiquettes() {
List<Etiquette> etiquettesList = new ArrayList<Etiquette>();
String selectQuery = "SELECT * FROM " + TABLE_ETIQUETTE;
SQLiteDatabase db = this.getWritableDatabase();
Cursor cursor = db.rawQuery(selectQuery, null);
if (cursor.moveToFirst()) {
do {
Etiquette etiquette = new Etiquette();
etiquette.set_id(Integer.parseInt(cursor.getString(0)));
etiquette.set_titre(cursor.getString(1));
etiquette.set_famille(cursor.getString(2));
etiquette.set_marque(cursor.getString(3));
//etiquette.set_nutrition(cursor.getString(4));
etiquette.set_autres(cursor.getString(5));
etiquettesList.add(etiquette);
} while (cursor.moveToNext());
}
return etiquettesList;
}
public int updateEtiquette(Etiquette etiquette) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(KEY_TITLE, etiquette.get_titre());
values.put(KEY_BRAND, etiquette.get_famille());
values.put(KEY_OTHERS, etiquette.get_autres());
// values.put(KEY_INTAKE, etiquette.get_nutrition());
values.put(KEY_OTHERS, etiquette.get_autres());
return db.update(TABLE_ETIQUETTE, values, KEY_ID + " = ?",
new String[]{String.valueOf(etiquette.get_id())});
}
public void deleteEtiquette(Etiquette etiquette) {
SQLiteDatabase db = this.getWritableDatabase();
db.delete(TABLE_ETIQUETTE, KEY_ID + " = ?",
new String[]{String.valueOf(etiquette.get_id())});
db.close();
}
public int getEtiquettesCount() {
SQLiteDatabase db = this.getReadableDatabase();
String countQuery = "SELECT * FROM " + TABLE_ETIQUETTE;
assert db != null;
Cursor cursor = db.rawQuery(countQuery, null);
int n = cursor.getCount();
cursor.close();
return n;
}
int getDATABASE_VERSION(){
return this.DATABASE_VERSION;
}
void setDATABASE_VERSION(int newDATABASE_VERSION){
this.DATABASE_VERSION = newDATABASE_VERSION;
}
}