How To Store Check Box Value From One Activity To Another Activity
i have two activity with checkboxs in a list and i have to pass each value of checkbox to another activity and make it checked, where i can't use intent to pass that because both a
Solution 1:
I usually use a static class/methods for this kind of thing, utilising SharedPreferences
publicstaticclassHal {
privatestatic final StringPREFS = "com.xxx.xxx.xxx";
publicstaticStringgetPreferenceString(Context ctx, String key) {
SharedPreferences sharedPreferences = ctx.getSharedPreferences(Hal.PREFS, Activity.MODE_PRIVATE);
return sharedPreferences.getString(key, "");
}
publicstaticvoidsetPreferenceString(Context ctx, String key, String val) {
SharedPreferences sharedPreferences = ctx.getSharedPreferences(Hal.PREFS, Activity.MODE_PRIVATE);
Editor preferencesEditor = sharedPreferences.edit();
preferencesEditor.putString(key, val);
preferencesEditor.commit();
return;
}
}
Solution 2:
You can make a singleton class to store the value of the array. eg. 'Storage' class with has the getter, setter of an array. You can set the data here in the class:
publicclassStorage {
privatestaticStroragestrorageData=newStrorage ();
publicstaticsynchronized Strorage getInstance() {
if (strorageData == null) {
strorageData = newStrorage ();
}
return strorageData ;
}
// getter & setter
}
In your activity you can access the object of this class using
private Storage storageData;
storageData = Storage .getInstance();
In this get the data by using getter.
Solution 3:
Create one java class which will extends to Application class, and get the object of that class by calling App.getInstance() and then you can set or get your selected list item from any activity.
publicclassAppextendsandroid.app.Application {
privatestaticApp app;
privateArrayList<Integer> checkedItemList;
privateActivity activity;
@OverridepublicvoidonCreate() {
super.onCreate();
app = this;
}
publicstaticAppgetInstance(){
return app;
}
publicvoidsetCheckedItemList(ArrayList<Integer> checkedItemList){
this.checkedItemList = checkedItemList;
}
publicArrayList<Integer> getCheckedItemList(){
returnthis.checkedItemList;
}
}
Now register the Application class in Android Manifest.xml under application tag
<applicationandroid:name=".App"android:icon="@drawable/ic_launcher"android:label="@string/app_name"android:theme="@style/AppTheme" ><activityandroid:name=".YourActivity"android:label="@string/app_name"><intent-filter><actionandroid:name="android.intent.action.MAIN" /><categoryandroid:name="android.intent.category.LAUNCHER" /></intent-filter></activity></application>
Post a Comment for "How To Store Check Box Value From One Activity To Another Activity"