将 arraylist<String> 值插入 android 中的 sqlite
inserting arraylist<String> values to sqlite in android
我在 2 个不同的 arraylist 中有来自 listview 的名称和 id,我想将 arraylist 中的 id 和名称存储到字符串,然后将它们保存在单击按钮时保存到 sqlite。
以下代码的问题是,当检查了2个项目并单击按钮时,项目将插入两次。当 id 设置为 UNIQUE 时,插入到 3 个 id 时,唯一的一个是保存在 sqlite 中,...
这是我目前尝试过的方法:
public void addSelected(ArrayList<String> selNames, ArrayList<String> selID) {
SQLiteDatabase db = getWritableDatabase();
try{
for (String str: selID){
for (String stl:selNames){
ContentValues cv = new ContentValues();
cv.put(KEY_ID, str);
cv.put(KEY_STATUS, stl);
db.insertOrThrow(TABLE_SELECTED, null, cv);
}
}
db.close();
}catch (Exception e){
Log.e("Problem", e + " ");
}
}
您正在迭代 ID,然后为每个 ID 迭代所有名称,导致您插入两者的所有组合。相反,您可以迭代两个列表的 length:
public void addSelected(ArrayList<String> selNames, ArrayList<String> selID) {
int size = selNames.size();
// Check that both lists have the same size
if (size != selID.size()) {
throw new IllegalArgumentException();
// Or some more elegant way to handle this error condition
}
SQLiteDatabase db = getWritableDatabase();
try{
for (int i = 0; i < size; ++i) {
ContentValues cv = new ContentValues();
cv.put(KEY_ID, selID.get(i));
cv.put(KEY_STATUS, selNames.get(i));
db.insertOrThrow(TABLE_SELECTED, null, cv);
}
db.close();
} catch (Exception e){
Log.e("Problem", e + " ");
}
}
注意:对 db.close()
的调用可能应该在 finally 块中,而不是 try
块中。我把它留在那里就像它在 OP 中一样,因为这不是问题的问题,但你可能也应该在你的真实代码中处理它。
我认为如果你的两个数组大小相同,你应该简单地尝试这个......并且还像这样在 db.insertorThrow 中传递你的简历
public void addSelected(ArrayList<String> selList, ArrayList<String> selID){
int size = selID.size();
SQLiteDatabase db = getWritableDatabase();
try{
for (int i = 0; i < size ; i++){
ContentValues cv = new ContentValues();
cv.put(KEY_ID, selID.get(i));
cv.put(KEY_STATUS, selList.get(i));
Log.d("Added ",""+ cv);
db.insertOrThrow(TABLE_SELECTED, null, cv);
}
db.close();
}catch (Exception e){
Log.e("Problem", e + " ");
}
如果你想在不迭代的情况下放置数组列表,你可以执行以下方法。
/此代码可用于插入/
JSONObject json = new JSONObject();// Create one Jsonobject
json.put("uniqueArrays", new JSONArray(items));// put the ArrayList You have in hand by converting that to JSONArray.
String arrayList = json.toString();//Convert json to string
/此代码可用于获取数据/
JSONObject json = new JSONObject(stringreadfromsqlitetable);// Convert the String to JsonObject
ArrayList items = json.optJSONArray("uniqueArrays");// Conver the jsonObject to Arraylist
我在 2 个不同的 arraylist 中有来自 listview 的名称和 id,我想将 arraylist 中的 id 和名称存储到字符串,然后将它们保存在单击按钮时保存到 sqlite。
以下代码的问题是,当检查了2个项目并单击按钮时,项目将插入两次。当 id 设置为 UNIQUE 时,插入到 3 个 id 时,唯一的一个是保存在 sqlite 中,...
这是我目前尝试过的方法:
public void addSelected(ArrayList<String> selNames, ArrayList<String> selID) {
SQLiteDatabase db = getWritableDatabase();
try{
for (String str: selID){
for (String stl:selNames){
ContentValues cv = new ContentValues();
cv.put(KEY_ID, str);
cv.put(KEY_STATUS, stl);
db.insertOrThrow(TABLE_SELECTED, null, cv);
}
}
db.close();
}catch (Exception e){
Log.e("Problem", e + " ");
}
}
您正在迭代 ID,然后为每个 ID 迭代所有名称,导致您插入两者的所有组合。相反,您可以迭代两个列表的 length:
public void addSelected(ArrayList<String> selNames, ArrayList<String> selID) {
int size = selNames.size();
// Check that both lists have the same size
if (size != selID.size()) {
throw new IllegalArgumentException();
// Or some more elegant way to handle this error condition
}
SQLiteDatabase db = getWritableDatabase();
try{
for (int i = 0; i < size; ++i) {
ContentValues cv = new ContentValues();
cv.put(KEY_ID, selID.get(i));
cv.put(KEY_STATUS, selNames.get(i));
db.insertOrThrow(TABLE_SELECTED, null, cv);
}
db.close();
} catch (Exception e){
Log.e("Problem", e + " ");
}
}
注意:对 db.close()
的调用可能应该在 finally 块中,而不是 try
块中。我把它留在那里就像它在 OP 中一样,因为这不是问题的问题,但你可能也应该在你的真实代码中处理它。
我认为如果你的两个数组大小相同,你应该简单地尝试这个......并且还像这样在 db.insertorThrow 中传递你的简历
public void addSelected(ArrayList<String> selList, ArrayList<String> selID){
int size = selID.size();
SQLiteDatabase db = getWritableDatabase();
try{
for (int i = 0; i < size ; i++){
ContentValues cv = new ContentValues();
cv.put(KEY_ID, selID.get(i));
cv.put(KEY_STATUS, selList.get(i));
Log.d("Added ",""+ cv);
db.insertOrThrow(TABLE_SELECTED, null, cv);
}
db.close();
}catch (Exception e){
Log.e("Problem", e + " ");
}
如果你想在不迭代的情况下放置数组列表,你可以执行以下方法。
/此代码可用于插入/
JSONObject json = new JSONObject();// Create one Jsonobject
json.put("uniqueArrays", new JSONArray(items));// put the ArrayList You have in hand by converting that to JSONArray.
String arrayList = json.toString();//Convert json to string
/此代码可用于获取数据/
JSONObject json = new JSONObject(stringreadfromsqlitetable);// Convert the String to JsonObject
ArrayList items = json.optJSONArray("uniqueArrays");// Conver the jsonObject to Arraylist