[英]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");
我需要回收器適配器中的這些變量來在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();
}
});
}
});
這是我的RecyclerView
適配器項單擊。 我需要// here
的變量來進行Retrofit
api 調用。
Intents是用於Activity之間的通信,你需要做的是在你的adapter中創建一個簡單的函數,然后在Activity中調用。
適配器:
private String date1;
private String date2;
public void setData(String date1, String date2, ...) {
this.date1 = date1;
this.date2 = date2;
...
}
活動:
adapter.setData(date1, date2, ..);
(我還不能使用評論但是)@DoubleD 寫的想法是正確的,您需要做的就是創建一個函數(很像構造函數),它將在適配器類中接收您的數據。 您獲得的 NullPointerException 不是來自函數,請檢查您的數據。 我會建議創建一個自定義對象來保存這些東西(取決於你的目標)。
檢查這個答案。 在這里,您將在您的活動中擁有您的 onClickListener()。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.