1

私はクラスを持っています、それは拡張LinearLayoutします、そこにはとがButtonsありSpinnerます。

このオブジェクトは、レイアウト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がandroid:entriesSpinnerアダプターに「」を自動入力する場合。

コンポーネントcom.ics.spinn.ComboBoxを同じように動作させたい:xmlファイルを介して定義された配列にアクセスできるようにし、コンポーネント内のスピナーに次の方法で提供できるようにします。

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

これで、strings.xmlで定義された配列に直接アクセスできましたgetResources().getStringArray(R.array.suppliers) が、android:entries ...を介して提供されるため、コードは「suppliers」という名前を認識しないはずです。

これ+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);
            }
}
4

3 に答える 3

1

コンポーネントがSpinnerを拡張しない限り、属性を使用してそれを実行できるかどうかはわかりませんが、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:app="http://schemas.android.com/apk/res/yourPackageName"以下の行を追加します。xmlns:android="http://schemas.android.com/apk/res/android"

次に、xmlを介してコンポーネントとカスタム属性をインスタンス化できます。

<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" />
/>

この答えがまさにあなたが探しているものであるかどうかはわかりませんが、それはと同じように動作しますandroid:entries。それが役に立てば幸い。

于 2012-12-06T16:41:27.073 に答える
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);
于 2012-12-06T16:10:49.813 に答える
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);
于 2013-01-07T11:21:13.650 に答える