android - Saving ArrayList<Map<String, String>> to a SQLite Database? -


i have fragment called historyfragment contains listview. items of listview added class called datamodel. i'm trying save list whenever item added list. (at moment, views in list textviews, later hope add images them, , have multiple lines etc. these need saved well). list is: arraylist<map<string, string>> , called mplanetlist.

i think saving sqlitedatabase solution, seems complicated after reading multiple answers/tutorials.

some sites i've looked at:

http://www.vogella.com/articles/androidsqlite/ http://developer.android.com/guide/topics/data/data-storage.html#db http://stackoverflow.com/q/5482402/2442638 http://stackoverflow.com/q/3142285/2442638 

there lots of questions in "question" main question comes in 2 parts:

a) database right solution?

--> if 'yes' --> b) simple way make one? prefer not use json/gson

--> if 'no' --> c) should use instead? sharedpreferences?

assuming database correct solution, think need serialize arraylist or possibly use stringset - have no idea how have arraylist this: arraylist<map<string, string>> opposed arraylist without map object.

also, tried access database datamodel class (as add items list) can't work.

i have added current code - work database not complete, stuck should next.

this historyfragment class:

// remove imports  public class historyfragment extends fragmentbase implements onitemaddedhandler { // fragmentbase class extends fragment , moment.      listview lv;     simpleadapter simpleadpt;      @override     public view oncreateview(layoutinflater inflater, viewgroup container,             bundle savedinstancestate) {         // todo auto-generated method stub         view view = inflater.inflate(r.layout.history, container, false);          listview lv = (listview) view.findviewbyid(r.id.listview);           list<map<string, string>> planetslist = datamodel.getinstance()                 .getplanetlist();         simpleadpt = new simpleadapter(getactivity(), planetslist,                 android.r.layout.simple_list_item_1, new string[] { "planet" },                 new int[] { android.r.id.text1 });          lv.setadapter(simpleadpt);         lv.setonitemclicklistener(new adapterview.onitemclicklistener() {              public void onitemclick(adapterview<?> parentadapter, view view,                     int position, long id) {                   textview clickedview = (textview) view;          // display dialog             }         });          return view;     }      @override     public void ondetach() {         super.ondetach();         datamodel.getinstance().setonitemaddedhandler(null);     }      @override     public void onitemadded(object data) {         simpleadpt.notifydatasetchanged();         lv.scrollto(0, 0); // scrolls top of list     }      @override     public void onstart() {         super.onstart();         datamodel.getinstance().setonitemaddedhandler(this);     }  } 

this datamodel:

public class datamodel {      private list<map<string, string>> mplanetslist = new arraylist<map<string, string>>();      private static datamodel instance;     private static onitemaddedhandler monitemaddhandler;      private datamodel() {         initlist();     }      public static datamodel getinstance() {         if (null == instance) {             instance = new datamodel();         }         return instance;     }      private void initlist() {  // here want load saved listitems.     }      public list<map<string, string>> getplanetlist() {         return mplanetslist;     }      private hashmap<string, string> createplanet(string key, string name) {          hashmap<string, string> planet = new hashmap<string, string>();         planet.put(key, name);         return planet;     }      public void setonitemaddedhandler(onitemaddedhandler handler) {         monitemaddhandler = handler;     }      public void additem(object data) { // save stuff here?         bundle data1 = new bundle();         string string = ((bundle) data).getstring("keytitle");         mplanetslist.add(0, createplanet("planet", string));    // want save items database     historydatabase.getinstance();  //adding .getwritabledatabase()  gives me nullpointerexception.  // don't understand else need here add mplanetlist database          if (null != monitemaddhandler) {             monitemaddhandler.onitemadded(data1);         }     } } 

and here database:

public class historydatabase extends sqliteopenhelper {  private static historydatabase instance;  public static final string table_comments = "comments";   public static final string column_id = "_id";   public static final string column_comment = "comment";    private static final string database_name = "commments.db";   private static final int database_version = 1;    // database creation sql statement   private static final string database_create = "create table "       + table_comments + "(" + column_id       + " key, " + column_comment       + " text not null);";  static context c;  historydatabase(context context) {     super(c, database_name, null, database_version); }  @override public void oncreate(sqlitedatabase db) {     db.execsql(database_create);  }  @override public void onupgrade(sqlitedatabase arg0, int arg1, int arg2) {     // todo auto-generated method stub  }  public static historydatabase getinstance() {     if (null == instance) {          instance = new historydatabase(c);     }     return instance; } } 

very basically: want save listitems permanently, , able retreive, edit , delete them.

thank taking time read this. if clarification needed, please say.

the sqlitedatabase correct solution.

you can save data of every item database. there hashmap<string, object> instead of hashmap<string, string> in each item data, in case of want put integer or other simple type data hashmap.

then, can serialize hashmap json when going save database:

    // can put multiple data hashmap     hashmap<string, object> datahashmap = new hashmap<string, object>();      contentvalues values = new contentvalues();     values.put("data", new jsonobject(datahashmap).tostring());     db.insert(table_name, col_id, values); 

you can unserialize after fetch serialized data database:

    int rawdataindex = 6;     string rawdata = cursor.getstring(rawdataindex);     hashmap<string, object> datahashmap = new hashmap<string, object>();      try {         jsonobject json = new jsonobject(rawdata);         jsonarray names = json.names();         (int = 0; < names.length(); i++) {             string key = names.getstring(i);             datahashmap.put(key, json.opt(key));         }     } catch (jsonexception e) {         e.printstacktrace();     } 

Comments

Popular posts from this blog

javascript - Laravel datatable invalid JSON response -

java - Exception in thread "main" org.springframework.context.ApplicationContextException: Unable to start embedded container; -

sql server 2008 - My Sql Code Get An Error Of Msg 245, Level 16, State 1, Line 1 Conversion failed when converting the varchar value '8:45 AM' to data type int -