Java 获取ArrayList<;字符串>;来自SQLite
我正在用SQLite保存我的ArrayList。保存代码:Java 获取ArrayList<;字符串>;来自SQLite,java,android,android-sqlite,Java,Android,Android Sqlite,我正在用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++){ ContentValue
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);
}
}
public void HistoryADD(ArrayList已满,字符串所有者){
int size=full.size();
SQLiteDatabase db=getWritableDatabase();
试一试{
对于(int i=0;i
它正在工作。我想从SQLite获取这个ArrayList。列表代码:
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;
}
public ArrayList History\u列表(字符串所有者){
SQLiteDatabase db=this.getReadableDatabase();
ArrayList history_list=新建ArrayList();
Cursor Cursor=db.rawQuery(“从“+TABLE_NAME+”中选择*,其中owner=”“+owner+”,
新字符串[]{});
cursor.close();
返回历史记录列表;
}
但是列表代码不起作用。这个密码怎么了?
谢谢。您需要这样做:
Cursor cursor = db.rawQuery(...);
try {
while (cursor.moveToNext()) {
//Here you have to add the item in your array list
}
} finally {
cursor.close();
}
您从未将光标中的项目添加到ArrayList,因此History_list函数始终返回空数组。您应该迭代光标,然后将每个项目添加到History_list array中。如何添加它?