【问题标题】:How to pass data from an Activity to a CustomView?如何将数据从 Activity 传递到 CustomView?
【发布时间】:2019-01-02 09:35:34
【问题描述】:

Activity B 有如下自定义视图:

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;
        String[] listOfVouchers = ((MerchantDetailsActivity) getContext()).listOfVouchers;
        Log.d("LIZ OF VOUCHERS", listOfVouchers.toString());
        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);
        } 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);
    }
}

活动 A 有一个名为 listOfVouchers 的公共变量。我正在尝试从自定义视图访问此变量,如下所示:

Log.d("LIZ OF VOUCHERS", listOfVouchers.toString());

但它返回以下错误:

com.app.ActivityA cannot be cast to com.app.ActivityB

如何在活动 B 的自定义视图中访问活动 A 中的变量?需要它来填充自定义视图中的微调器。

【问题讨论】:

  • 您应该就这个问题提供反馈

标签: android


【解决方案1】:

您的自定义视图位于活动 B 中,以便在本地保存数据并将其传递,请查看涵盖它的 my answer here

【讨论】:

    【解决方案2】:

    我认为您的方法不正确。如果您需要自定义视图中的凭证列表,您应该在自定义视图上提供一个名为“setVouchers”的公共方法,例如:

    public void setVouchers(String[] listOfVouchers){
      mVouchers = listOfVouchers;
    }
    

    其中 mVouchers 是 MakePaymentCustomView 类的实例变量。定义方法后,在您需要的活动/片段中调用它,在您的情况下为 Activity A

    【讨论】:

      【解决方案3】:

      使用共享偏好。 请在共享偏好中保存您的价值。 在自定义视图中获取您的价值。 使用值后从共享偏好中删除值以避免未知错误。

      这不是最佳答案,只是建议。

      【讨论】:

        猜你喜欢
        • 2017-06-21
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-12-12
        • 1970-01-01
        相关资源
        最近更新 更多