sqlite - Right way to store data of android app -
i'm develop android app wish publish play store well. app have 2 activities
activity - 1
display listview user. each item of listview have image , little description. each item can clicked further infomation product. upon clicking item, user sent activity - 2
activity - 2
this activity display bigger picture of product , more details product.
now, i'm thinking how i'm going store data. , have come following options:-
local database
since, i'm sure app use limited amount of data may updated in 2 - 3 months. can store data in sqlite database. but, think way insert data in sqlite database writing insert query each row (i can wrong here). so, time consuming me , doesn't practice well..
remote database
this second option, can create tables in remote database , fetch contents there in list views. but! don't want depend on internet time. want data present @ finger tips of user 24/7. 1 resolution problem can store data remote local database. but, practice?
store data in file
i have randomly come option, idk if possible in android. but, think can store data in txt dat file , read it. when want update app, need update file.
please, advice me above options. also, can come own ways , best practices.
storing data in files
here way of saving have worked on through several games. add more methods need them:
public class saver { static fileoutputstream fos; static fileinputstream fis; public static void save(string filename, string data, context c){ try { fos = c.openfileoutput(filename, context.mode_private); fos.write(data.getbytes()); fos.close(); } catch (exception e) { // todo auto-generated catch block e.printstacktrace(); } } string file; public string getfile(){return file;} public void setfile(string loc){ file = loc; } string result; private static string mainload(string fn, context c){ string collected = null; try{ fis = c.openfileinput(fn); byte[] dataarray = new byte[fis.available()]; while(fis.read(dataarray) != -1){ collected = new string(dataarray); } }catch(exception e){ e.printstacktrace(); return null; }finally{ try { fis.close(); return collected; } catch (exception e) { // todo auto-generated catch block e.printstacktrace(); return null; } } } public static int loadint(string fn, context c){ if(mainload(fn,c) == null) return 0; else return integer.parseint(mainload(fn,c)); } public static double loaddouble(string fn, context c){ if(mainload(fn,c) == null) return 0; else return double.parsedouble(mainload(fn,c)); } public static float loadfloat(string fn, context c){ return float.parsefloat(mainload(fn,c)); } public static string loadstring(string fn, context c){ return mainload(fn, c); } public static boolean loadboolean(string fn, context c){ if(mainload(fn,c) == null) return false; else return boolean.parseboolean(mainload(fn,c)); } public static biginteger loadbiginteger(string fn, context c){ return new biginteger(mainload(fn,c)); } public static bigdecimal loadbigdecimal(string fn, context c){ return new bigdecimal(mainload(fn,c)); } }
this form of saving uses internal data no permissions required. saves files cannot accessed in same way external data saves outside environement.
and not want go near shared preferences. data in wiped if app crashes or forced stop.
Comments
Post a Comment