google maps - Best way to store small amounts of data in android -


i have basic application uses maps api display map, , after long press put marker down @ location. if select different location, delete current marker , make new one.

the app want @ moment, can data persist after app closed.

this i'm trying do:

    @override     protected void oncreate(bundle savedinstancestate) {       super.oncreate(savedinstancestate);       setcontentview(r.layout.activity_maps);       // obtain supportmapfragment , notified when map ready used.       supportmapfragment mapfragment = (supportmapfragment) getsupportfragmentmanager()             .findfragmentbyid(r.id.map);       mapfragment.getmapasync(this);        bufferedreader reader;        try {           final inputstream file = getassets().open("userlatlng");           reader = new bufferedreader(new inputstreamreader(file));           string line = reader.readline();           line = reader.readline();           system.out.print("getting data");       } catch (ioexception ioe) {           ioe.printstacktrace();       }   } 

that used reading data after application has started

            string filename = "userlatlng";             fileoutputstream outputstream;              try {                 outputstream = openfileoutput(filename, context.mode_private);                  outputstream.write(integer.parseint(yayaparking.tostring()));                 outputstream.close();             } catch (exception e) {                 e.printstacktrace();             } 

that should how saves data. have yet work, appreciated!

use sharedpreference api small data storage.

sharedpreferences sharedpref = context.getsharedpreferences(         "lat_pref", context.mode_private);      sharedpreferences.editor editor = sharedpref.edit();     editor.putstring(key_lat, "12.7333");   // lat , lng     editor.putstring(key_lng, "12.7333");     editor.commit(); 

you can retrieve below

sharedpreferences sharedpref = context.getsharedpreferences("lat_pref",context.mode_private);  string slat = sharedpref.getstring(key_lat, "0.0");  //0.0 default value, can change value like. default value applied when key not present in sharedprefernce  string slng = sharedpref.getstring(key_lng, "0.0");  //convert string lat , lng double values double lat = double.parsedouble(slat); double lng = double.parsedouble(slng); 

you can declare key_lat & key_lng in constant file.

public static final string key_lat = "latitude"; public static final string key_lng = "longitude"; 

if in activity use this context, if in fragment use getactivity()

edit

editor.putlong(key_lat, double.doubletolongbits(location.getlatitude())); 

retrieve like,

double lat = double.longbitstodouble(sharedpref.getlong(key_lat, 0);  

do same longitude


Comments

Popular posts from this blog

javascript - Thinglink image not visible until browser resize -

firebird - Error "invalid transaction handle (expecting explicit transaction start)" executing script from Delphi -

mongodb - How to keep track of users making Stripe Payments -