In compression of ListPreference, start method



  • Hey, how can you start a method when you press the ListPreference?

     <PreferenceCategory android:title="General" >
        <ListPreference
             android:id="@+id/settings_clear"
            android:key="pref_cachet"
            android:numeric="integer"
            android:summary=""
            android:title="Clear Cache" />
    </PreferenceCategory>
    

    ListPreference splashList = (ListPreference) PrefsActivity.this.findPreference("pref_cachet");

    splashList.setOnPreferenceClickListener(new OnPreferenceClickListener() {

        @Override
        public boolean onPreferenceClick(Preference preference) {
            // TODO Auto-generated method stub
    
            return false;
        }
    
     });
    



    1. Here. https://stackoverflow.com/a/10392850/3212712 They say that when applied to the building element of the list, onPreferenceClick and preference.getKey() inside the method. key by the position of the compressed list you're appointing xml Attribut android:entries;

      @Override
      public boolean onPreferenceClick(Preference preference) {
          if (preference.getKey().equals("ЗНАЧЕНИЕ_ИЗ_МАССИВА_ЗНАЧЕНИЙ_ПРИСВОЕННЫХ_В_XML")) {
              Toast.makeText(getBaseContext(), preference.getKey(), Toast.LENGTH_LONG).show();
          }
          return true;
      }
      
    2. You can also listen not to press on the list element, but to change the line. OnPreferenceChangeListener as written https://stackoverflow.com/a/24985733/3212712 :

      final ListPreference list = (ListPreference) preference;
      lista.setOnPreferenceChangeListener(new OnPreferenceChangeListener() {
      public boolean onPreferenceChange(Preference preference, Object newValue) {
      
      int index = list.findIndexOfValue(newValue.toString());
      if (index != -1) 
      {
          Toast.makeText(getBaseContext(), list.getEntries()[index], Toast.LENGTH_LONG).show();
      }
      return true;
      

      }
      });




Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2