如何使app:entries属性动态化?

时间:2019-01-02 07:20:42

标签: android android-xml android-xml-attribute

attr.xml

<declare-styleable name="PaymentCustomView">
    <attr name="customViewTitle" format="string" />
    <attr name="customViewSubtitle" format="string" />
    <attr name="android:entries" />
</declare-styleable>

make_payment.xml

    <com.laterpay.MakePaymentCustomView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@drawable/blue_column"
        app:customViewSubtitle="100"
        app:customViewTitle="Maximum Spending"
        app:entries="@{vm.vouchers}"/>

CustomView.java

public class MakePaymentCustomView extends LinearLayout {
    private Context _context;
    public MakePaymentCustomView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        _context = context;
        setOrientation(LinearLayout.VERTICAL);
        LayoutInflater.from(context).inflate(R.layout.make_payment_custom_layout, this, true);
        String title;
        String subtitle;
        TypedArray a = context.getTheme().obtainStyledAttributes(attrs, R.styleable.PaymentCustomView, 0, 0);
        try {
            title = a.getString(R.styleable.PaymentCustomView_customViewTitle);
            subtitle = a.getString(R.styleable.PaymentCustomView_customViewSubtitle);
            CharSequence[] entries = a.getTextArray(R.styleable.PaymentCustomView_android_entries);
            if(entries != null){
                //do something
                Log.d("Entries:",entries.toString());
            }
        } finally {
            a.recycle();
        }
        // Throw an exception if required attributes are not set
        if (title == null) {
            throw new RuntimeException("No title provided");
        }
        if (subtitle == null) {
            throw new RuntimeException("No subtitle provided");
        }

        init(title, subtitle);
    }
    // Setup views
    private void init(String title, String subtitle) {
        List<String> categories = new ArrayList<String>();
        categories.add("Automobile");
        categories.add("Business Services");
        categories.add("Computers");
        categories.add("Education");
        categories.add("Personal");
        categories.add("Travel");
        TextView titleView = findViewById(R.id.customview_textview_title);
        TextView subtitleView = findViewById(R.id.customview_textview_subtitle);
        Spinner voucherList = findViewById(R.id.voucherSpinner);
        ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(_context, android.R.layout.simple_spinner_item, categories);
        voucherList.setAdapter(dataAdapter);
        titleView.setText(title);
        subtitleView.setText(subtitle);
    }

我想用从API获取的一组动态数据填充Spinner。但是问题是,如何将这些数据传递到自定义视图中并填充微调器(voucherList)

活动A会将一些数据传递到包含此自定义视图的活动B。如何将数据填充到自定义视图的微调器中?

2 个答案:

答案 0 :(得分:1)

您的customedView应该具有使用setter方法的自定义适配器或私有变量。然后,您可以将customedView填充到活动B将显示的popupWindow中。

答案 1 :(得分:0)

Check this
您将首先调用REST-API,然后将您提到的集合/动态数据传递给链接中显示的其他活动。