2012-12-09 3 views
1

코드에서 MultiSelectListPreference 기호를 사용하는 방법을 알고 싶습니다. 누군가가 내가 온라인 예제를 찾을 수 없습니다 :MultiSelectListPreference from code

// List preference 
ListPreference listPref = new ListPreference(this); 
listPref.setEntries(R.array.entries_list_preference); 
listPref.setEntryValues(R.array.entryvalues_list_preference); 
listPref.setDialogTitle(R.string.dialog_title_list_preference); 
listPref.setKey("list_preference"); 
listPref.setTitle(R.string.title_list_preference); 
listPref.setSummary(R.string.summary_list_preference); 
dialogBasedPrefCat.addPreference(listPref); 

답변

3

내 하루 만들 것 MultiSelectListPreference에 대한 유사한 예를 들어 줄 수 있다면

다음 코드는 API의 예에서 PreferencesFromCode.java 파일 촬영, 하지만 나는 이것을 함께 넣어 그것을 작동합니다.

편집 :이 솔루션은 ICS +에서만 작동합니다. Honeycomb은 .setValues ​​()를 완전히 무시하고 리스너에 전달 된 HashSet에는 다른 값이 포함됩니다. 이것은 알려진 버그입니다. 그러나 이것이 Android v4 이상에서 구현하고자하는 사람들에게 도움이되기를 바랍니다.

MultiSelectListPreference listPreference = new MultiSelectListPreference(context); 
    listPreference.setTitle(R.string.configure_category_title); 
    listPreference.setDialogTitle(R.string.configure_category_title); 
    listPreference.setSummary(R.string.configure_category_summary); 
    listPreference.setEntries(R.array.configure_category_array); 
    listPreference.setEntryValues(new CharSequence[]{ 
      ProcessList.PREF_SERVICES + mAppWidgetId, 
      ProcessList.PREF_INACTIVE + mAppWidgetId, 
      ProcessList.PREF_INTERNAL + mAppWidgetId 
    }); 

    //Create a Set<String> with list items that should be selected 
    SharedPreferences sharedPref = PreferenceManager.getDefaultSharedPreferences(context); 
    boolean showServices = sharedPref.getBoolean(ProcessList.PREF_SERVICES + mAppWidgetId, true); 
    boolean showInactive = sharedPref.getBoolean(ProcessList.PREF_INACTIVE + mAppWidgetId, true); 
    boolean showInternal = sharedPref.getBoolean(ProcessList.PREF_INTERNAL + mAppWidgetId, true); 

    String[] strings = new String[3]; 
    int cnt = 0; 
    if (showServices) 
     strings[cnt++] = ProcessList.PREF_SERVICES + mAppWidgetId; 
    if (showInactive) 
     strings[cnt++] = ProcessList.PREF_INACTIVE + mAppWidgetId; 
    if (showInternal) 
     strings[cnt] = ProcessList.PREF_INTERNAL + mAppWidgetId; 

    Set<String> mySet = new HashSet<String>(); 
    Collections.addAll(mySet, strings); 

    //Add the set 
    listPreference.setValues(mySet); 

    //Listen for changes, I'm not sure if this is how it's meant to work, but it does :/ 
    listPreference.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() { 
     public boolean onPreferenceChange(Preference preference, Object o) { 

      HashSet hashSet = (HashSet) o; 
      Iterator stringIterator = hashSet.iterator(); 
      boolean[] states = {false, false, false}; 
      String prefString; 

      while (stringIterator.hasNext()) { 

       prefString = (String) stringIterator.next(); 

       if (prefString == null) 
        continue; 

       if (prefString.compareTo(ProcessList.PREF_SERVICES + mAppWidgetId) == 0) 
        states[0] = true; 
       else if (prefString.compareTo(ProcessList.PREF_INACTIVE + mAppWidgetId) == 0) 
        states[1] = true; 
       else if (prefString.compareTo(ProcessList.PREF_INTERNAL + mAppWidgetId) == 0) 
        states[2] = true; 

      } 

      PreferenceManager 
        .getDefaultSharedPreferences(getActivity()) 
        .edit() 
        .putBoolean(ProcessList.PREF_SERVICES + mAppWidgetId, states[0]) 
        .putBoolean(ProcessList.PREF_INACTIVE + mAppWidgetId, states[1]) 
        .putBoolean(ProcessList.PREF_INTERNAL + mAppWidgetId, states[2]) 
        .commit(); 

      return true; 
     } 
    }); 

    preferenceCategory.addPreference(listPreference); 
+0

위대한 작품 :) 고맙습니다 :) –