最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

android - Androidx: Cannot resolve method 'getDialog' in 'ListPreference' - Stack Overflow

programmeradmin4浏览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.

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.

Share Improve this question edited Mar 26 at 9:33 Fisher asked Mar 8 at 7:34 FisherFisher 5197 silver badges30 bronze badges 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? – Fisher 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/reference/androidx/preference/… – Fisher 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. – Fisher Commented Mar 8 at 9:19
 |  Show 6 more comments

1 Answer 1

Reset to default 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);
            }
        }
...
发布评论

评论列表(0)

  1. 暂无评论