2012-12-06 23 views
1

我有類,它擴展了LinearLayout,其中有ButtonsSpinnerWORKS:Android自定義組件 - 通過layout.xml提供的strings.xml中的訪問數組

這個對象獲得通過我的佈局XML文件包括:

<com.ics.spinn.ComboBox android:id="@+id/myautocombo" 
    android:layout_width="fill_parent" 
android:layout_height="wrap_content" 
android:completionThreshold="1" 
android:entries="@array/suppliers" /> 
/> 

陣列供應商在strings.xml中定義。

如果此組件現在不是com.ics.spinn.ComboBox,而是Spinner,則Android會 自動填充Spinner適配器的「android:entries」。

我想我的組件com.ics.spinn.ComboBox有同樣的表現方式: 到能夠訪問通過XML文件中定義的數組,這樣我就可以 它提供給我的微調組件內部,通過:

ArrayAdapter<String> a = new ArrayAdapter<String>(this.getContext(),android.R.layout.simple_spinner_dropdown_item, ARRAYINSIDEMYXML); 
    s.setAdapter(a); 

我現在我可以直接通過getResources().getStringArray(R.array.suppliers) 訪問strings.xml中定義的數組,但我的代碼不應該知道的名字「供應商」,因爲它應通過機器人提供:項..

這個+JoãoMelo xml中的條目解決方案WORK:

 public ComboBox(Context context, AttributeSet attrs) { 
     super(context, attrs); 

      TypedArray b = context.obtainStyledAttributes(attrs, 
        R.styleable.ComboBox, 0, 0); 

      CharSequence[] entries = b.getTextArray(R.styleable.ComboBox_myEntries); 
      if (entries != null) { 
       ArrayAdapter<CharSequence> adapter = 
         new ArrayAdapter<CharSequence>(context, 
           android.R.layout.simple_spinner_item, entries); 
       adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); 
       s.setAdapter(adapter); 
      } 
} 
+0

您的TypedArray在res/values文件夾內的attrs.xml文件中定義。在下面檢查我的答案。 –

回答

1

我不知道這是否是不可能的,除非你的組件擴展微調這樣做與android:entries屬性,但我只是猜測。

可以實現在attrs.xml

<declare-styleable name="ComboBox"> 
    <attr name="myEntries" format="reference"></attr> 
</declare-styleable> 

創建自己的自定義屬性,那麼您可以訪問組件內這個參考(INT),並設置一個ArrayAdapter到您的微調。

TypedArray customAttrs = context.obtainStyledAttributes(attrs, R.styleable.ComboBox); 
    for (int i = 0; i < customAttrs.length(); i++) { 
     int attrValue = customAttrs.getIndex(i); 
     switch (attrValue) { 
      case R.styleable.ComboBox_myEntries: 
       mArrayId = customAttrs.getResourceId(attrValue, 0); 
       ArrayAdapter<String> a = new ArrayAdapter<String>(this.getContext(),android.R.layout.simple_spinner_dropdown_item, mArrayId); 
       s.setAdapter(a); 
       break; 
     } 
    } 

在您的佈局,在根視圖中添加以下xmlns:android="http://schemas.android.com/apk/res/android"此行xmlns:app="http://schemas.android.com/apk/res/yourPackageName"

<com.ics.spinn.ComboBox android:id="@+id/myautocombo" 
android:layout_width="fill_parent" 
android:layout_height="wrap_content" 
android:completionThreshold="1" 
app:myEntries="@array/suppliers" /> 
/> 

不知道:

然後,你可以通過XML實例化你的組件和定製ATTRS這個答案正是你要找的,但它的行爲就像android:entries。希望能幫助到你。

+0

謝謝,在你的幫助下,我能夠解決問題,見上文。 只有downer是attr.xml +中的條目,它不被稱爲android:entries ... – Caaaaarl

0

嘗試加載你的陣列是這樣的:

String[] array = getResources().getStringArray(R.array.recipes_string_array); 

ArrayAdapter spinnerAdapter = new ArrayAdapter<String>(getActivity(), R.layout.simple_spinner_dropdown_item) { 

    @Override 
    public int getCount() { 
     return array.length; 
    } 

    @Override 
     public String getItem(int position) { 
     return array[position]; 
    } 

    @Override 
    public View getView(int position, View convertView, ViewGroup parent) { 
     //Create your item for the spinner and return it   
     return spinnerItemview; 
    } 

} 

spinner.setAdapter(spinnerAdapter); 
+0

他試圖從自定義組件的'android:entries'屬性中獲取數組。 – Luksprog

0

Joala的上述答案在技術上是正確的,但表達它的方法更爲簡單。

不是迭代StyledAttributes,而是直接詢問StringArray的resourceId。

// Get the DisplayValues from the XML config. 
final TypedArray customAttrs = getContext().obtainStyledAttributes(attrs, R.styleable.ComboBox); 
final int resourceId = customAttrs.getResourceId(R.styleable.ComboBox_myEntries, -1); 
if (resourceId == -1) { 
    throw new IllegalArgumentException("ComboBox requires a myEntries attribute that points to a string-array resource"); 
} 

final ArrayAdapter<String> a = new ArrayAdapter<String>(this.getContext(), android.R.layout.simple_spinner_dropdown_item, resourceId); 
s.setAdapter(a); 
相關問題