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

Android 如何使app:entries属性动态化?,android,android-xml,android-xml-attribute,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> m

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获得的动态数据填充我的微调器。但问题是,如何将此数据传递到自定义视图并填充微调器voucherList

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


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

您的customedView应该有一个自定义适配器或一个带有setter方法的私有变量。然后您可以将customedView填充到一个弹出窗口中,该窗口将显示活动B。

我的意思是,如何将动态数据传递到应用程序:条目