我只是想为这个问题寻求帮助。我在listview中填充了一个hashmap。下面的代码是我的hashmap代码:
mylist = new ArrayList<HashMap<String, String>>();
for (int i = 0; i < listSelectedFileNames.size(); i++)
{
HashMap<String, String> map = new HashMap<String, String>();
map.put(FILE_NAME, selectedFileNames[i]);
map.put(DESC, "");
map.put(UPLOADED_BY, "User");
map.put(DATE_UPLOADED, myDate);
map.put(ACTION, "Delete");
map.put(ID, String.valueOf(i));
map.put(FILE_URI, selectedFileUri[i]);
mylist.add(map);
}
我怎样才能将数据保存在数据库中。 FILE_NAME,DESC,UPLOADED_BY,FILE_URI是我数据库中的不同列。感谢任何能帮助我的人。这是可能有用的其他代码。
selectedFileNames = new String[listSelectedFileNames.size()];
for (int i = 0; i < listSelectedFileNames.size(); i++) {
selectedFileNames[i] = listSelectedFileNames.get(i);
}
selectedFileUri = new String[listSelectedFileUri.size()];
for (int i = 0; i < listSelectedFileUri.size(); i++) {
selectedFileUri[i] = listSelectedFileUri.get(i);
}
myCustomAdapterIreport = new CustomArrayAdapterIreport(getApplicationContext(), mylist, R.layout.attribute_selected_ireport_file,
new String[]{FILE_NAME, DESC, UPLOADED_BY, DATE_UPLOADED, ACTION, ID, FILE_URI},
new int[]{R.id.tv_iFile, R.id.txt_iDesc,R.id.tv_iUploadedBy,R.id.tv_iDateUploaded, R.id.tv_iAction, R.id.tv_RowId, R.id.tv_iUri}, true);
lv_AttachedFileData.setAdapter(myCustomAdapterIreport);
答案 0 :(得分:2)
SQLiteDatabase db = mDbHelper.getDatabase();
db.beginTransaction();
for(HashMap<String, String> map : mylist){
ContentValues cv = new ContentValues();
cv.put(FILE_NAME, map.get(FILE_NAME));
cv.put(DESC, map.get(DESC));
cv.put(UPLOADED_BY, map.get(DATE_UPLOADED));
cv.put(ACTION, map.get(FILE_NAME));
cv.put(ID, map.get(ID));
cv.put(FILE_URI, map.get(FILE_URI));
db.insert("tablename", null, cv);
}
db.setTransactionSuccessful();
db.endTransaction();
db.close();
您需要实现数据库帮助程序,字符串键需要匹配列名。您可以在此处阅读有关数据库助手的更多信息:http://developer.android.com/training/basics/data-storage/databases.html