I'm new to this multi-select listview. I want to save the checked state of the checkbox in the listview so that if user closes the app and then opens again, the selected checkbox still remain selected. Is there any way to do this. I searched for it and found that it can be done using SharedPreference but I didn't get more information on how to use it. Thanks
public class MainActivity extends AppCompatActivity {
ListView myList;
Button getChoice;
String[] listContent = {
"January",
"February",
"March",
"April",
"May",
"June",
"July",
"August",
"September",
"October",
"November",
"December"
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
myList = (ListView)findViewById(R.id.list);
getChoice = (Button)findViewById(R.id.getchoice);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_multiple_choice, listContent);
myList.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
myList.setAdapter(adapter);
getChoice.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String selected = "";
int cntChoice = myList.getCount();
SparseBooleanArray sparseBooleanArray = myList.getCheckedItemPositions();
for(int i = 0; i < cntChoice; i++){
if(sparseBooleanArray.get(i)) {
selected += myList.getItemAtPosition(i).toString() + "\n";
}
}
Toast.makeText(MainActivity.this, selected, Toast.LENGTH_LONG).show();
}
});
}
}