获取ArrayList 来自SQLite

问题描述 投票:0回答:1

我正在SQLite中保存我的ArrayList。保存代码:

public void HistoryADD(ArrayList<String> full, String owner){
    int size = full.size();
    SQLiteDatabase db = getWritableDatabase();
    try{
        for (int i = 0; i < size ; i++){
            ContentValues cv = new ContentValues();
            cv.put(FULL, full.get(i));
            cv.put(OWNER, owner);
            db.insert(TABLE_NAME, null, cv);
        }
        System.out.println("added:" + full);
        db.close();
    }catch (Exception e){
        System.out.println("Failed to add" + full);
    }
}

而且它正在发挥作用。我想从SQLite获取这个ArrayLists。清单代码:

 public ArrayList<String> History_list(String owner) {
    SQLiteDatabase db = this.getReadableDatabase();
    ArrayList<String> history_list = new ArrayList<>();

    Cursor cursor = db.rawQuery("SELECT * from " + TABLE_NAME + " WHERE owner='"+owner+"'",
            new String[] {});

    cursor.close();
    return history_list;
}

但列表代码不起作用。这段代码有什么问题?谢谢。

java android android-sqlite
1个回答
0
投票

你需要做那样的事情:

Cursor cursor = db.rawQuery(...);
try {
    while (cursor.moveToNext()) {
        //Here you have to add the item in your array list

    }
} finally {
    cursor.close();
}
© www.soinside.com 2019 - 2024. All rights reserved.