I have a custom listview with checkboxes. I want the state of the checkboxes to be saved for the entire lifetime of the app. How can I achieve this? Thank you
-
Like this your question can not be answered. Please provide some more information. like where do the data you show come from. If its a sqlite database then store the state in your data base. If your data are parsed from a server... – KarlKarlsom Feb 18 '12 at 09:41
-
The checkboxes are checked by the user, they are not influenced by any other service. – Dinesh Feb 18 '12 at 09:47
2 Answers
The SharedPreferences class provides a general framework that allows you to save and retrieve persistent key-value pairs of primitive data types.
For the sake of completeness, I include sample code here.
public class Xxx extends Activity {
public static final String PREFS_NAME = "MyPrefsFile";
@Override
protected void onCreate(Bundle state){
super.onCreate(state);
. . .
// Restore preferences
SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
boolean silent = settings.getBoolean("silentMode", false);
setSilent(silent);
}
@Override
protected void onStop(){
super.onStop();
// We need an Editor object to make preference changes.
// All objects are from android.context.Context
SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
SharedPreferences.Editor editor = settings.edit();
editor.putBoolean("silentMode", mSilentMode);
// Commit the edits!
editor.commit();
}
}

- 1,807
- 3
- 22
- 48
-
I know that but I have about 16 checkboxes in my listview, I want save the state of every checkbox. – Dinesh Feb 18 '12 at 09:44
-
I think then a [Preference Activity](http://developer.android.com/reference/android/preference/Preference.html) is something that you are looking for. This [Tutorial](http://www.javacodegeeks.com/2011/01/android-quick-preferences-tutorial.html) should get you upto speed! – Anand Sainath Feb 18 '12 at 09:47
-
I have a boolean array that stores the state of each checkbox. The problem is that this array is not persistent. But sharedprefrences does not support arrays. – Dinesh Feb 18 '12 at 09:54
-
What is it exactly that you are trying to accomplish as a result of those check boxes? – Anand Sainath Feb 18 '12 at 10:00
-
I'm implementing something like a subscription activity where the user can subscribe to a service by checking and unsubscribe by unchecking. I want the state of these checkboxes to be persistent so that they can be used by the user for reference. – Dinesh Feb 18 '12 at 10:10
-
Yea, so as you had already pointed out, there is no way to store an array in SharedPreferences. In that case, you are left with one of two choices, 1) either implement that using an SQLiteDB or 2) Use a Preference Activity. I would personally prefer the latter. – Anand Sainath Feb 18 '12 at 10:13
-
I cannot afford Preference Activity as I'm using a custom listview which has hardly to do anything with xml. Actually there is a way where my boolean array for checkboxes can be converted into a string and then used for sharedprefernces. But I don't exactly know how this can be done. – Dinesh Feb 18 '12 at 10:18
-
You can always create a Preference Activity on the fly. If you are interested in that - [refer](http://stackoverflow.com/questions/5375363/dynamic-listpreference-in-android) this. – Anand Sainath Feb 18 '12 at 10:22
You shouldn't use checkboxes in ListView, I prefer to use TableLayout for them. However if you want to use ListView, consider the following previous questions:-
Android save Checkbox State in ListView with Cursor Adapter
Finding the Checked state of checkbox in a custom listview
Code does not extend ListViewActivity, but does have a listview
There're lots of other questions on same topic, consider searching for them too.
EDIT: Example for a dynamic TableLayout to replace ListView
XML:-
<ScrollView
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TableLayout
android:id="@+id/table"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
</TableLayout>
</ScrollView>
Code excerpt in Java:-
TableLayout table=(TableLayout)findViewByid(R.id.table);
ArrayList<CheckBox> cbData=new ArrayList<CheckBox>();
for(int i=0;i<rowCount;i++){
TextView t1=new TextView(this);
t1.setText("Text1");
TextView t2=new TextView(this);
t2.setText("Text2");
CheckBox cb=new CheckBox(this);
TableRow row=new TableRow(this);
row.addView(t1);
row.addView(t2);
row.addView(cb);
cbData.add(cb);
table.addView(row);
}
Using the above code you can add as much as rows you want in the TableLayout, and since the TableLayout does not recycles it's view, you won't have to cache your checkbox data and state. To access the CheckBoxes you can use cbData.get(index) method. Lot easier than coding for caching the checkbox state.

- 1
- 1

- 19,522
- 20
- 117
- 184
-
My problem is different, I want the state of the checkboxes to be persisted. – Dinesh Feb 18 '12 at 09:49
-
Then using the TableLayout or Linear Layout in a ScrollView would be the easiest option. If you want to go for ListView still, you have to put the method to update the checkboxes in the onFling or onScroll Listener, once you got the data from checkboxes. But let me tell you it's not worth the effort. – 0xC0DED00D Feb 18 '12 at 10:02
-
I have a listview that sucessfully works with checkboxes but teh problem is that I want the state of the checkboxes to be application persistent, not activity persistent. – Dinesh Feb 18 '12 at 10:12
-
I know about the problem, I've used checkboxes in ListView once and had the same problem, but had to switch towards TableLayout instead, due to too much extra confusing code to be wrote. It works fine now. – 0xC0DED00D Feb 18 '12 at 10:26
-
-
I didn't got what do you mean, using layouts will give you more flexibility and customization, however more the layouts more slow your activity will be to load. So if you have more than 20 rows to be added, use listview with MULTIPLE_SELECT_MODE instead of using checkboxes. – 0xC0DED00D Feb 18 '12 at 10:46
-
-
Have a look at the edited part in my answer. The code will help you replace your ListView. rowCount is the count of how many rows do you want. Your UI will be preserved and the end-user may not know that you have used tablelayout instead of ListView. – 0xC0DED00D Feb 18 '12 at 16:18