开发者

How do I get the Category of an Android Preference?

How do I get the PreferenceCategory of a Preference? PreferenceManager has a findPreference method but a Preference does not have a getCategory method.

开发者_StackOverflow社区

Is there a way to get the PreferenceCategory of a Preference from it's key name?


There is no built-in method in the SDK to get the parent (either category or screen) of the preference. You should manually find it in the hierarchy to be able to write something like:

Preference preference = findPreference(key);
getParent(preference).removePreference(preference);

Below are the methods which enables it. Note, that it should be written in your preference activity (derived from the PreferenceActivity class), which can provide the hierarchy root by calling getPreferenceScreen()

private PreferenceGroup getParent(Preference preference)
{
    return getParent(getPreferenceScreen(), preference);
}

private PreferenceGroup getParent(PreferenceGroup root, Preference preference)
{
    for (int i = 0; i < root.getPreferenceCount(); i++)
    {
        Preference p = root.getPreference(i);
        if (p == preference)
            return root;
        if (PreferenceGroup.class.isInstance(p))
        {
            PreferenceGroup parent = getParent((PreferenceGroup)p, preference);
            if (parent != null)
                return parent;
        }
    }
    return null;
}


There is no getCategory() method but you could use this following workaround: If you don't use dependency attribute for your Preference you could set your Preference as dependent from PreferenceCategory and then use getDependency() method. For example:

<PreferenceCategory 
    android:title="MyPrefCat" 
    android:key="category" 
    android:selectable="false">  
    <CheckBoxPreference 
            android:key="chkbox" 
            android:dependency="category" />
</PreferenceCategory>

In the PreferenceActivity you could use now:

CheckBoxPreference currentPref = (CheckBoxPreference) findPreference("chkbox");
String prefCatKey = currentPref.getDependency();
PreferenceCategory catPref = (PreferenceCategory) findPreference(prefCatKey);
// Access PreferenceCategory's attributes such as its title:
String prefCatTitle = catPref.getTitle().toString();


Are you trying to do this from within the PreferenceActivity or elsewhere? If from there, try this out. It should loop through your categories and find the right one. Just make sure you update the catKeys array to include the categories you want to search each time you change your xml.

String catKeys = {"catOne","catTwo","catThree","catFour"};

String getCategoryKey(String prefKey)
{
    PreferenceCategory curCat;
    Preference test;
    for(String catKey : catKeys)
    {
        curCat = (PreferenceCategory)findPreference(catKey);
        if(curCat == null)
            continue;
        test = curCat.findPreference(prefKey);
        if(test != null)
            return catKey;
    }
}


I don't see a getCategory or getGroup neither in Preference.

But, you can use the methods of PreferenceCategory inherited from http://developer.android.com/reference/android/preference/PreferenceGroup.html

getPreference(int index)

int getPreferenceCount()

That's a long way indeed for what you want to do, but it looks like you should rebuild the preference menu indeed.

Other alternative would be to saxp your xml prefrence file.

Regards, Stéphane


You can find the entire child-parent tree by traversing over all of the preferences and then check which is the parent of any preference you wish, even without using the id of the parent:

public static Map<Preference,PreferenceGroup> buildPreferenceParentTree(final PreferenceActivity activity)
    {
    final Map<Preference,PreferenceGroup> result=new HashMap<Preference,PreferenceGroup>();
    final Stack<PreferenceGroup> curParents=new Stack<PreferenceGroup>();
    curParents.add(activity.getPreferenceScreen());
    while(!curParents.isEmpty())
      {
      final PreferenceGroup parent=curParents.pop();
      final int childCount=parent.getPreferenceCount();
      for(int i=0;i<childCount;++i)
        {
        final Preference child=parent.getPreference(i);
        result.put(child,parent);
        if(child instanceof PreferenceGroup)
          curParents.push((PreferenceGroup)child);
        }
      }
    return result;
    }

usage:

  final Map<Preference,PreferenceGroup> preferenceParentTree=buildPreferenceParentTree(SettingsActivity.this);
  final PreferenceGroup preferenceParent=preferenceParentTree.get(preference);

EDIT: If you wish to just hide it, you can also use this:

https://developer.android.com/reference/androidx/preference/Preference#setVisible(boolean)

I'm not sure if currently it's available or not, though.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜