我已经搜索过用其他主题解决我的问题,但我找不到好的问题。所以我会解释我想做什么。我在网站上恢复了像这样的JSON文件:
{
"prod": {
"ean": "000000000000",
"cat": "category ...",
"title": "title ...",
"brand": "brand ...",
"intake": [
{
"carbohydrates": "10g"
},
{
"protein": "8g"
},
{
"lipids": "0g"
}
],
"others": "blablabla"
}
}
我可以很好地解析它,并在不同的字段中显示它,如TextViews,我使用ListView来显示“进入”数组。现在我想将所有这些值放在数据库中。我可以将所有内容放在我的DataBase中,除了这个数组(我使用的是HashMap)。知道这个阵列的元素数量可能会有所不同,有人知道如何继续吗?
谢谢,我为我糟糕的英语道歉。如果你问我,我可以提供更多信息。
有我的DataBase处理程序:
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;
}
}
你可以通过两种方式实现它。
第一种方式: 引入一个新的表用于摄入使用你id作为外键并将摄入量及其值存储在该表中,并且可以使用简单的联合查询轻松访问它。
第二种方式: 将所有入口和值组合成具有预定义分隔符的字符串,并将其存储在当前字段中,并在检索时使用用于解析它们的分隔符并根据需要显示
String value_delimter = ":";
String intake_delimter = "&"
String intak_value = "intake1"+value_delimter +"value1"+intake_delimter
"intake2"+value_delimter +"value2"+intake_delimter
"intake3"+value_delimter +"value3"+intake_delimter
您可以使用带分隔符的拆分来检索数据时访问值。
使用循环从这个数组创建特殊字符分隔的字符串
“碳水化合物:10克,蛋白质:8G,血脂:0克”
然后把整个字符串作为你的列值,当你回来时尝试conmma分离然后冒号分离以获得你的价值。