2014-10-14 6 views
0

Я работаю с alertdialog с несколькими часами. и я хочу сохранить выбранные значения, поэтому, когда пользователь возвращается к alertdialog, значения, которые он выбрал ранее, по-прежнему отмечены. Мой код:persist multipleChoices контрольный список на AlertDialog

public class TimelineSettings extends DialogFragment { 

final ArrayList selected_categories = new ArrayList(); 
private SharedPreferences sharedPreferences; 
private SharedPreferences.Editor sharedPrefEditor; 
final CharSequence[]items = {"Fourniture","Nourriture","Voyages","Habillement","Médias","Autres"}; 
boolean[] itemsChecked = new boolean[items.length]; 
@Override 
public Dialog onCreateDialog(Bundle savedInstanceState) { 
    AlertDialog.Builder builder = new AlertDialog.Builder(getActivity()); 
    // Set the dialog title 
    builder.setTitle("Choisissez vos paramètres") 
      // Specify the list array, the items to be selected by default (null for none), 
      // and the listener through which to receive callbacks when items are selected 
      .setMultiChoiceItems(items, null, 
        new DialogInterface.OnMultiChoiceClickListener() { 
         @Override 
         public void onClick(DialogInterface dialog, int indexselected, 
              boolean isChecked) { 
          itemsChecked[indexselected] = 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) { 
        // User clicked OK, so save the mSelectedItems results somewhere 
        // or return them to the component that opened the dialog 

       } 
      }) 
      .setNegativeButton("Annuler", new DialogInterface.OnClickListener() { 
       @Override 
       public void onClick(DialogInterface dialog, int id) { 

       } 
      }); 
    return builder.create(); 
} 

} 

Есть ли способ сделать это с sharedpreferences (хранить arraylist)? такой же как this ответ.

спасибо.

ответ

0

проблема решена. here - ответ с решением и объяснением.

0

Вы можете сохранить данные в ArrayList с помощью SharedPreferences, как показано на this answer

+0

Я попытался следить за тем, что было в ответе .. но все равно не может заставить его работать .. вот мой обновленный код: https://gist.github.com/RidouaneHicham/54c1cb3b2d326a917175 вы можете взглянуть и посмотреть если мой код в порядке? – RidRoid

+0

Возможно, вам не нужно сохранять данные как заданные, лучше сохранить логический массив, как показано во втором ответе на связанный вопрос –

Смежные вопросы