简体   繁体   English

从活动中获取数据到 RecyclerView 适配器?

[英]Getting data from activity into RecyclerView adapter?

    Intent i = getIntent();
    Bundle myBundle = i.getExtras();
    date1 = myBundle.getString("Date1");
    date2 = myBundle.getString("Date2");
    user = myBundle.getString("UserName");
    chain = myBundle.getString("ChainName");
    shop = myBundle.getString("ShopName");
    product_g = myBundle.getString("ProductGroup");
    product_c = myBundle.getString("ProductCategory");
    product = myBundle.getString("Product");
    count = myBundle.getInt("Count");
    type_c = myBundle.getInt("CountType");
    price = myBundle.getInt("Price");
    type_p = myBundle.getInt("PriceType");
    inch = myBundle.getInt("Inch");
    promotor = myBundle.getInt("Promotor");

I need these variables in recycler adapter to make retrofit request in RecyclerView adapter.我需要回收器适配器中的这些变量来在RecyclerView适配器中提出改造请求。

     holder.itemView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            android.app.AlertDialog.Builder mBuilder = new android.app.AlertDialog.Builder(context);
            LayoutInflater li = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            final View mView = li.inflate(R.layout.sales_filtered_shop_pop_up, null);
            final RecyclerView rd3 = (RecyclerView) mView.findViewById(R.id.sales_rv);
            Button mClose = (Button) mView.findViewById(R.id.sales_pop_up_btn_close);
            mBuilder.setView(mView);
            final android.app.AlertDialog dialog = mBuilder.create();
            dialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
            dialog.setCancelable(false);

            // here 
            dialog.show();

            mClose.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    dialog.dismiss();
                }
            });
        }
    });

This is my RecyclerView adapter item click.这是我的RecyclerView适配器项单击。 I need the variables in // here to make Retrofit api call.我需要// here的变量来进行Retrofit api 调用。

Intents are for communication between activity, what you need to do is to create a simple function in your adapter, and then call in the activity. Intents是用于Activity之间的通信,你需要做的是在你的adapter中创建一个简单的函数,然后在Activity中调用。

Adapter:适配器:

private String date1;
private String date2;

public void setData(String date1, String date2, ...) {
   this.date1 = date1;
   this.date2 = date2;
   ...
}

Activity:活动:

 adapter.setData(date1, date2, ..);

(I can't use the comment yet but) The idea which @DoubleD wrote is the right one, all you need to do is to create a function (much like a constructor) that will receive your data in the adapter class. (我还不能使用评论但是)@DoubleD 写的想法是正确的,您需要做的就是创建一个函数(很像构造函数),它将在适配器类中接收您的数据。 The NullPointerException you are getting isn't from the function, check your data.您获得的 NullPointerException 不是来自函数,请检查您的数据。 And I will suggest to create a custom object that will hold those things (depend on your goal).我会建议创建一个自定义对象来保存这些东西(取决于你的目标)。

Check this answer.检查这个答案。 Here you will have your onClickListener() in your Activity.在这里,您将在您的活动中拥有您的 onClickListener()。

Simple Android RecyclerView example 简单的 Android RecyclerView 示例

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM