从SQLite获取ArrayList <string>

时间:2018-09-01 16:32:17

标签: java android android-sqlite

我将ArrayList保存在SQLite中。保存代码:

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);
    }
}

并且IT在起作用。我想从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;
}

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

1 个答案:

答案 0 :(得分:0)

您需要执行以下操作:

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

    }
} finally {
    cursor.close();
}