Saving records into the database from the hashmap in android

牧云@^-^@ 提交于 2020-01-11 12:51:32

问题


I just wanted to ask help for this one. I have a hashmap populate in the listview. the code below is my code for my 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);
}

How can I possibly save that data in the database. FILE_NAME, DESC, UPLOADED_BY, FILE_URI are the different columns in my database. Thanks for anyone who would help me. Here's the other code that maybe could help.

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

回答1:


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

A database helper will need to be implemented by you, the string keys will need to match the column names. You can read more on database helpers here: http://developer.android.com/training/basics/data-storage/databases.html



来源:https://stackoverflow.com/questions/17666722/saving-records-into-the-database-from-the-hashmap-in-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!