2

すべての設定をフラットに繰り返す方法はありますか?
今、私はそれぞれの好みを尋ねています

private void doSomething( Preference p ) {
    if( p instanceof PreferenceCategory || p instanceof PreferenceScreen ) {
        PreferenceGroup pGroup = (PreferenceGroup) p;
        int pCount = pGroup.getPreferenceCount();
        for(int i = 0; i < pCount; i++) {
            doSomething(pGroup.getPreference(i)); // recursive call
        }
    } else {
        ... process single preference ...
    }
}

私はこのようにしたいと思います:

int pCount = getTotalPreferenceCount();
for(int i = 0; i < pCount; i++) {
    // process getFlatPreference(i);
}
4

2 に答える 2

6

I'm not sure I see your problem with iterating though it this way, but if you want you could implement the following

private void callingMethod(){
    ArrayList<Preference> list = getPreferenceList(prefScreen, new ArrayList<Preference>());
    for (Preference p : list) {
        doSomething(p);
    }
}

private ArrayList<Preference> getPreferenceList(Preference p, ArrayList<Preference> list) {
    if( p instanceof PreferenceCategory || p instanceof PreferenceScreen) {
        PreferenceGroup pGroup = (PreferenceGroup) p;
        int pCount = pGroup.getPreferenceCount();
        for(int i = 0; i < pCount; i++) {
            getPreferenceList(pGroup.getPreference(i), list); // recursive call
        }
    } else {
        list.add(p);
    }
    return list;
}

this would allow you to make a flat preference list and then iterate through that list with a foreach loop.

于 2013-02-22T15:05:45.917 に答える