android - How to store an ArrayList or a HashMap to a file or in SharedPreferences? -


i have searched, haven't been able find proper solution problem. receiving data server , displaying it, want data stored locally, preferably in sharedpreferences or file if there internet access, data should stored , updated. if there no internet access data should displayed.

below code

package com.timetable;  import java.io.fileinputstream; import java.io.fileoutputstream; import java.io.inputstream; import java.io.objectinputstream; import java.util.arraylist; import java.util.hashmap; import java.util.hashset; import java.util.set;  import org.apache.http.namevaluepair; import org.json.jsonarray; import org.json.jsonexception; import org.json.jsonobject;  import android.app.listactivity; import android.content.context; import android.content.intent; import android.net.parseexception; import android.os.asynctask; import android.os.bundle; import android.view.view; import android.widget.adapterview; import android.widget.adapterview.onitemclicklistener; import android.widget.listadapter; import android.widget.listview; import android.widget.simpleadapter; import android.widget.textview; import android.widget.toast;  public class mondaytimetable_te_elex extends listactivity {     // json node names     private static string tag_id = "id";     private static string tag_name = "name";     string filename = "monday_te_elex_file";     fileinputstream fis;      listview listviewitem;     // contacts jsonarray     jsonarray monday_te_elex = null;      @override     protected void oncreate(bundle savedinstancestate) {         super.oncreate(savedinstancestate);         setcontentview(r.layout.activity_monday_time_table_te_elex);          new placeorder().execute();         listviewitem = getlistview();      }      arraylist<hashmap<string, string>> categorieslist;      class placeorder extends asynctask<void, void, void> {          @override         protected void doinbackground(void... params) {              // todo auto-generated method stub             inputstream inputstream = null;             string result = null;              arraylist<namevaluepair> namevaluepairs = new arraylist<namevaluepair>();             // example param :             // namevaluepairs.add(new basicnamevaluepair("id", 1));             inputstream = request.sethttprequest(                     "http://asimmerchant.hostoi.com/te/elex/monday/monday_te_elex.php",                     namevaluepairs);              // hashmap listview             categorieslist = new arraylist<hashmap<string, string>>();              if (inputstream != null) {                 result = stringresponse.convertresponsetostring(inputstream);                 result = result.trim();             }              if (result == null || result.equals("null")) {                 system.out.println("invalid request");              } else {                  try {                     jsonarray jarray = new jsonarray(result);                     jsonobject json_data = null;                     (int = 0; < jarray.length(); i++) {                         json_data = jarray.getjsonobject(i);                          try {                             string id = json_data.getstring(tag_id);                             string name = json_data.getstring(tag_name);                              // creating new hashmap                             hashmap<string, string> map = new hashmap<string, string>();                              // adding each child node hashmap key => value                             map.put(tag_id, id);                             map.put(tag_name, name);                              categorieslist.add(map);                          } catch (parseexception e) {                             e.printstacktrace();                         }                     }                 } catch (jsonexception e1) {                     toast.maketext(getbasecontext(), "error", toast.length_long)                             .show();                 } catch (parseexception e1) {                     toast.maketext(getbasecontext(), "error", toast.length_long)                             .show();                 }              }             return null;          }          @override         protected void onpostexecute(void result) {             // todo auto-generated method stub             super.onpostexecute(result);             /**              * updating parsed json data listview              * */             listadapter adapter = new simpleadapter(mondaytimetable_te_elex.this,                     categorieslist, r.layout.list, new string[] { tag_name},                     new int[] { r.id.name });              setlistadapter(adapter);              /**              * selecting single listview item              */         }     } } 

sharedpreferences can't store primitive type, can't use store collection. use serialization write on internal storage, can tricky. suggest store jsonobject/jsonarray (result case) in sharedpreferences string.


Comments

Popular posts from this blog

java - Andrioid studio start fail: Fatal error initializing 'null' -

android - Gradle sync Error:Configuration with name 'default' not found -

StringGrid issue in Delphi XE8 firemonkey mobile app -