0

I have used a custom ListPreference which worked well before. Then upgrading to Androidx, it appears this error:

> Cannot resolve method 'getDialog' in 'ListPreference'

import androidx.preference.ListPreference;
....
public static class myPreferenceFragment extends PreferenceFragmentCompat implements SharedPreferences.OnSharedPreferenceChangeListener {
...
ListPreference coustomListPreference = (ListPreference) findPreference("list_XXXX");
coustomListPreference.setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() {
   public boolean onPreferenceClick(Preference preference) {
      ... 
      if(!XXXXPermissionGranted(): when users don't grant some Permission, causing this ListPreference to have nothing to show) 
       coustomListPreference.getDialog().dismiss();
   }

I've searched this topic and I cannot find a straight answer.

11
  • ListPreference extend DialogPreference,DialogPreference have getDialog() Commented Mar 8 at 7:48
  • @卡尔斯路西法 Thank you for the comment! So, do you mean it should be able to directly use .getDialog()? why it still appears error and cannot build? Commented Mar 8 at 8:22
  • @卡尔斯路西法 I study this issue a little bit more. Actually in Androidx, the getDialog() is removed as th link below. I think maybe the dialog is created in another way now. developer.android.com/reference/androidx/preference/… Commented Mar 8 at 8:33
  • sorry,but not need called dismiss().it is auto dismiss Commented Mar 8 at 9:15
  • @卡尔斯路西法 No! It won't auto dismiss() before user click [OK] or [Cancel]. That's why you see this code there. Commented Mar 8 at 9:19

1 Answer 1

1

For Androidx, it needs to do these things in addition to your PreferenceFragmentCompat to accomplish the coustomListPreference.getDialog().dismiss() in the question statements.

This could be a template if you want to make the dialog dismiss or to customize the Entries/Values of the ListPreference in Java code.

public static class myDialogPrefFragCompat extends PreferenceDialogFragmentCompat {
        public static myDialogPrefFragCompat newInstance(String key) {
            final myDialogPrefFragCompat fragment = new myDialogPrefFragCompat();
            final Bundle bundle = new Bundle(1);
            bundle.putString(ARG_KEY, key);
            fragment.setArguments(bundle);
            return fragment;
        }
        @Override
        public void onDialogClosed(boolean positiveResult) {
            if (positiveResult) {
                // do things
            }
        }
}
public static class myPreferenceFragment extends PreferenceFragmentCompat implements SharedPreferences.OnSharedPreferenceChangeListener {
    ...
private static final String DIALOG_FRAGMENT_TAG = "myDialogPreference";
        @Override
        public void onDisplayPreferenceDialog(Preference preference) {
            if (getParentFragmentManager().findFragmentByTag(DIALOG_FRAGMENT_TAG) != null) {
                return;
            }

            if (preference instanceof ListPreference) {
                if (preference.getKey().equals("list_XXXX")){

                    initXXXXPermissionToGo(); //Request Android Permission such like Contacts etc. for customizing the Entries/Values of the ListPreference
                    if(XXXXPermissionGranted())
                        super.onDisplayPreferenceDialog(preference);
                    /*else
                        super.onDisplayPreferenceDialog(preference); 
when the Permission not granted, nothing can be set in ListPreference Entries/Values, and there's no need to see it. This equals 
coustomListPreference.getDialog().dismiss() in the question*/
                }else
                    super.onDisplayPreferenceDialog(preference);
            } else {
                super.onDisplayPreferenceDialog(preference);
            }
        }
...
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.