I've been struggling with this for more than two weeks. been on all the SO questions about sharedpreferenes and other 'hacks' to persist a multiple choice alertdialog. but unfortunately i still can't make it work.
Can someone please explain to me how to make this thing work ? my multiple choice alertdialog works. but i still can't save the selected items..
My code :
public class TimelineSettings extends DialogFragment {
Context context;
final ArrayList selected_categories = new ArrayList();
final String[]items = {"Fourniture","Nourriture","Voyages","Habillement","Médias","Autres"};
TinyDB tinydb = new TinyDB(context);
private SharedPreferences sharedPreference;
private SharedPreferences.Editor sharedPrefEditor;
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
// Set the dialog title
builder.setTitle("Choisissez vos paramètres")
.setMultiChoiceItems(items, null,
new DialogInterface.OnMultiChoiceClickListener() {
@Override
public void onClick(DialogInterface dialog, int indexselected,
boolean isChecked) {
if (isChecked) {
// If the user checked the item, add it to the selected items
selected_categories.add(indexselected);
} else if (selected_categories.contains(indexselected)) {
// Else, if the item is already in the array, remove it
selected_categories.remove(Integer.valueOf(indexselected));
}
}
})
// Set the action buttons
.setPositiveButton("OK", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int id) {
tinydb.putList("selected",selected_categories);
}
})
.setNegativeButton("Annuler", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int id) {
}
});
return builder.create();
}
}
Thanks for your help.
PS : I came across this answer, if you can explain to me how to make this work, it would be great.