简体   繁体   中英

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.

     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. I need the variables in // here to make Retrofit api call.

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.

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. The NullPointerException you are getting isn't from the function, check your data. 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.

Simple Android RecyclerView example

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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