Android - Preference onCreateView attrs.getAttributeCount()
How do I get the "max" attribute during onCreateView()? If I can get attrs.getAttributeCount() to work that would solve my problem.
<?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>
Here is the sim开发者_运维问答plified class. XmlPullParser does not return anything to parse.
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", "####");
}
}
Log output
SeekBarPreference1
Key: keyItems
AttrCount: 7
####
SeekBarPreference1
Key: keyItemsTwo
AttrCount: 7
####
onCreateView
Key: keyItems
AttrCount: -1
####
onCreateView
Key: keyItemsTwo
AttrCount: -1
####
It seems to me that if I can get the correct key, title, summery, etc. (from onCreateView) there should be a simple way to get other attributes.?
If I try and store the attributes from SeekBarPreference1(), they are lost as soon as onCreateView() is called.
Summery: get the attributes from the current "this" onCreateView();
I got it figured out.
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();
}
}
Then retrieve the data onCreateView()
protected View onCreateView(ViewGroup parent) {
// Retrieve the data
// Now you will always have the correct data
_maxValue = Integer.parseInt(_preferences.getString(getKey() +"Max", ""));
}
Now I can use the same class multiple times, finally...
精彩评论