2010-12-20 40 views
0

如何在onCreateView()期間獲得「max」屬性? 如果我能得到attrs.getAttributeCount()來解決我的問題。Android - 偏好onCreateView 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

我明白了。

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", "")); 

} 

現在我可以使用相同的類多次,終於...