从Android的HashMap中保存记录到数据库中 [英] Saving records into the database from the hashmap in android

查看:78
本文介绍了从Android的HashMap中保存记录到数据库中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只是想问问帮助这一个。我已经在ListView一个HashMap填充。低于code是我的code我的HashMap的:

  mylist中=新的ArrayList< HashMap的<字符串,字符串>>();
的for(int i = 0; I< listSelectedFileNames.size();我++)
{
   HashMap的<字符串,字符串>图=新的HashMap<字符串,字符串>();
   map.put(FILE_NAME,selectedFileNames [I]);
   map.put(DESC,);
   map.put(UPLOADED_BY,用户);
   map.put(DATE_UPLOADED,指明MyDate);
   map.put(ACTION,删除);
   map.put(ID,将String.valueOf(ⅰ));
   map.put(FILE_URI,selectedFileUri [I]);
   mylist.add(图)
}
 

我怎么可能保存在数据库中的数据。 FILE_NAME,DESC,UPLOADED_BY,FILE_URI都在我的数据库中的不同列。感谢的人谁也帮我。这里的另一个code,也许会有所帮助。

  selectedFileNames =新的String [listSelectedFileNames.size()];
        的for(int i = 0; I< listSelectedFileNames.size();我++){
            selectedFileNames [I] = listSelectedFileNames.get(ⅰ);
        }

        selectedFileUri =新的String [listSelectedFileUri.size()];
        的for(int i = 0; I< listSelectedFileUri.size();我++){
            selectedFileUri [I] = listSelectedFileUri.get(ⅰ);
        }

myCustomAdapterIreport =新CustomArrayAdapterIreport(getApplicationContext(),mylist中,R.layout.attribute_selected_ireport_file,
新的String [] {FILE_NAME,DESC,UPLOADED_BY,DATE_UPLOADED,动作ID,FILE_URI},
新的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},真) ;
lv_AttachedFileData.setAdapter(myCustomAdapterIreport);
 

解决方案

  SQLiteDatabase DB = mDbHelper.getDatabase();
db.beginTransaction();

对(HashMap的<字符串,字符串>地图:mylist中){
   ContentValues​​ CV =新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(表名,空,CV);
}
db.setTransactionSuccessful();
db.endTransaction();
db.close();
 

一个数据库助手将需要由你来实现,该字符串键将需要匹配的列名。 http://developer.android:您可以在这里数据库助手阅读更多。 COM /培训/基础知识/数据存储/ databases.html

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

解决方案

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

这篇关于从Android的HashMap中保存记录到数据库中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆