1

我已经搜索过其他主题以解决我的问题,但我没有找到好的主题。所以我会解释我想做什么。我在网站上恢复了一个这样的 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;
}
}
4

2 回答 2

1

像这样使用循环从数组中创建特殊字符分隔的字符串

“碳水化合物:10g,蛋白质:8g,脂质:0g”

然后把这整个字符串作为你的列值,当你回来时尝试用逗号分隔,然后用冒号分隔来得到你的值。

于 2013-07-26T08:39:25.327 回答
1

您可以通过两种方式实现它。

第一种方式:
引入一个新的入口表,使用你的 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

您可以在使用带分隔符的拆分检索数据时访问值。

于 2013-07-26T08:39:37.570 回答