android - Adding data while parsing into next activity -
i doing app, takes name , phone number in first activity. should save values , parse next activity , vice versa.
eg:
in actvity 1 kept name1 & age1 --> save , in same activity again inserted name2 & age2--> save
and when click display should display both values saved in activity.
here mentioned 2 cases, may vary 10 or 20 or n.
kindly, provide on way can make it?
if want pass data between activities can use below cases :
case : 1
you can pass data between activites through intent.
in current activity, create new intent:
intent = new intent(getapplicationcontext(), newactivity.class); i.putextra("variable_key","value"); startactivity(i);
then in new activity, retrieve values:
bundle extras = getintent().getextras(); if (extras != null) { string value = extras.getstring("variable_key"); }
this passing data between activities.
case : 2
you can make class of static , public arraylist's getter , setter.
example :
class getterarraylist{ private static map<string, arraylist<string>> arraylist; public static init(){ if(arraylist== null) arraylist= new hashmap<string, arraylist<string>>(); } public static arraylist<string> getstringlist(string key){ return arraylist.get(key); } public static void putstringlist(arraylist<string> value, string key){ arraylist.put(key, value); } }
now initialize arraylist class:
getterarraylist.init();
now can add value in arraylist below :
arraylist<string> listnew = new arraylist<string>(); listnew .add("your data"); getterarraylist.putstringlist(listnew , "newlist");
and can arraylist in class key.
for(string s:getterarraylist.getstringlist("newlist")){ log.d("my list",s); }
case : 3
you can use shared preferences passing data between activities .
this link shared preferences.
but here trailor.
this how can set data
sharedpreferences settings = getsharedpreferences("prefs", mode_private); sharedpreferences.editor prefeditor = settings.edit(); prefeditor.putstring("string_preference", "some_string"); prefeditor.putint("int_preference", 18); prefeditor.commit();
and how can data :
sharedpreferences prefs = getsharedpreferences("prefs", mode_private); string string_preference= prefs.getstring("string_preference", "no name defined");//"no name defined" default value. int int_preference= prefs.getint("int_preference", 0); //0 default value.
case : 4
if want pass multiple data can use local storage sqlite database.
here link reference .
or
how save data on device, whether it's temporary files, downloaded app assets, user media, structured data, or else. can see more here.(see saving data section)
save them sqlite database.
Comments
Post a Comment