Android - Preference onCreateView attrs.getAttributeCount()

时间:2010-12-20 16:15:46

标签: android

如何在onCreateView()期间获取“max”属性? 如果我可以使用attrs.getAttributeCount()来解决我的问题。

<?xml version="1.0" encoding="utf-8"?>
    <PreferenceScreen
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:title="@string/livewallpaper_settings"
        android:key="livewallpaper_settings"
    >
        <com.example.myapp.SeekBarPreference1
            android:persistent="true"
            android:key="keyItems"
            android:title="Items"
            android:defaultValue="50"
            android:max="200" />
        <com.example.myapp.SeekBarPreference1
            android:persistent="true"
            android:key="keyItemsTwo"
            android:title="Items Two"
            android:defaultValue="5"
            android:max="10" />
    </PreferenceScreen>  

这是简化的课程。 XmlPullParser不会返回任何要解析的内容。

    public final class SeekBarPreference1 extends Preference implements OnSeekBarChangeListener {

        private static int _maxValue = 0;
        private int _currentValue = 0;
        private TextView _value;
        private SharedPreferences _preferences;

        public SeekBarPreference1(Context context, AttributeSet attrs) {
            super(context, attrs);
            _preferences = PreferenceManager.getDefaultSharedPreferences(context);

            //
            // These values are correct
            //
            Log.d("PREFS", " ");
            Log.d("PREFS", "SeekBarPreference1");
            Log.d("PREFS", "Key: " + getKey());
            Log.d("PREFS", "AttrCount: " + attrs.getAttributeCount());
            Log.d("PREFS", "####");
        }

        @Override
        protected View onCreateView(ViewGroup parent) {

            XmlPullParser attrs = parent.getResources().getXml(R.xml.livewallpaper_settings);

            Log.d("PREFS", " ");
            Log.d("PREFS", "onCreateView");
            Log.d("PREFS", "Key: " + getKey()); // Correct key is output
            Log.d("PREFS", "AttrCount: " + attrs.getAttributeCount()); // -1
            Log.d("PREFS", "####");  

        }
}

记录输出

SeekBarPreference1
Key: keyItems
AttrCount: 7
####

SeekBarPreference1
Key: keyItemsTwo
AttrCount: 7
####

onCreateView
Key: keyItems
AttrCount: -1
####

onCreateView
Key: keyItemsTwo
AttrCount: -1
####

在我看来,如果我能获得正确的密钥,标题,夏季等(来自onCreateView),应该有一种简单的方法来获取其他属性。?

如果我尝试存储SeekBarPreference1()中的属性,一旦调用onCreateView(),它们就会丢失。

总结:从当前“this”onCreateView();

获取属性

1 个答案:

答案 0 :(得分:1)

我明白了。

public SeekBarPreference1(Context context, AttributeSet attrs) {
    super(context, attrs);
    _preferences = PreferenceManager.getDefaultSharedPreferences(context);

    // Save your shared prefs here
    // I saved the seekbar max && current value
    // Something like below
    for (int i = 0; i < attrs.getAttributeCount(); i++) {
        if (attrs.getAttributeName(i).equals("max"))
            _preferences.edit().putString(getKey() +"Max", ""+ 
                    attrs.getAttributeValue(i)).commit();
    }
}

然后检索onCreateView()

上的数据
protected View onCreateView(ViewGroup parent) {

    // Retrieve the data
    // Now you will always have the correct data
    _maxValue = Integer.parseInt(_preferences.getString(getKey() +"Max", ""));

}

现在我可以多次使用同一个班级,最后......