[英]Accessing a Variable from an Activity to a RecyclerView Adapter
I am new to android , I have two classes an Activity class BureauRateListing.java
and an Adapter class CurrencySelectorAdapter.java
. 我是android的新手,我有两个类: Activity类
BureauRateListing.java
和Adapter类CurrencySelectorAdapter.java
。
So i want to access this variable ( public String bs;
)from an Activity class to a RecyclerView Adapter
class in the onBindViewHolder()
method, below is my Activity class: 所以我想从一个活动类访问此变量(
public String bs;
)到onBindViewHolder()
方法中的RecyclerView Adapter
类,以下是我的活动类:
public String bs;
private Intent i;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.rate_recycler_view);
raterv = (RecyclerView) findViewById(R.id.raterv_rv);
i = getIntent();
bs = i.getStringExtra("buysell");
}
Now how can i access the variable ( public String bs;
) from an Activity class
to an Adapter class inside onBindViewHolder()
method, and i send it as an Extra to the NextActivity. 现在,我该如何从
onBindViewHolder()
方法内的Activity class
到Adapter类中访问变量 ( public String bs;
),并将其作为Extra发送给NextActivity。 Below is my onBindViewHolder()
method where i want to access the variable and i send it as an Extra : 下面是我的
onBindViewHolder()
方法,我想在其中访问变量并将其作为Extra发送:
@Override
public void onBindViewHolder(CurrencyViewHolder holder, int position) {
final BureauRateObject br = itemList.get(position);
holder.bureauname.setText(br.getBureau_name());
holder.rates.setText(br.getBuysell());
final String BureauId = br.getBureau_id();
final String BureauName = br.getBureau_name();
final String BureauPhone = br.getBureau_phone();
final String BureauEmail = br.getBureau_email();
final String Website = br.getWebsite();
final String Branches = br.getBranches();
final String BureauProfile = br.getBureau_profile();
final String BureauLogo = br.getBureau_logo();
final String BureauPhoto = br.getBureau_photo();
final String currency = br.getBuysell();
final String BuySell = "Buying";
final String BuySell_two ="Selling";
holder.root.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent i = new Intent(CurrencySelectorAdapter.this.context, SingleForexDetails.class);
// i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
i.putExtra("bureau_id", BureauId);
i.putExtra("bureau_name",BureauName);
i.putExtra("bureau_phone",BureauPhone);
i.putExtra("bureau_email",BureauEmail);
i.putExtra("website",Website);
i.putExtra("branches",Branches);
i.putExtra("bureau_profile",BureauProfile);
i.putExtra("bureau_logo",BureauLogo);
i.putExtra("bureau_photo",BureauPhoto);
i.putExtra("currency",currency);
i.putExtra("Buying",BuySell);
i.putExtra("Selling",BuySell_two);
context.startActivity(i);
}
});
}
try this 尝试这个
BureauRateListing extends Activity{
public String bs;
private Intent i;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.rate_recycler_view);
raterv = (RecyclerView) findViewById(R.id.raterv_rv);
i = getIntent();
bs = i.getStringExtra("buysell");
}
public string getbs(){
return bs;
}
}
CurrencySelectorAdapter extends ...{
BureauRateListing mBureauRateListing; // it's Activity
CurrencySelectorAdapter (Activity activity){
mBureauRateListing =activity;
}
.
.
.
@Override
public void onBindViewHolder(CurrencyViewHolder holder, int position) {
String bs = mBureauRateListing.getbs();
.
.
.
}
Pass the Activity in Adapter Class. 在适配器类中传递活动。
Used this syntax for accessing bs variable in Adapter. 使用此语法访问适配器中的bs变量。
BureauRateListing extends Activity{
public String bs;
private Intent i;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.rate_recycler_view);
CurrencySelectorAdapter currencySelectorAdapter=new (BureauRateListing.this);
raterv = (RecyclerView) findViewById(R.id.raterv_rv);
i = getIntent();
bs = i.getStringExtra("buysell");
}}
CurrencySelectorAdapter extends ...{
Activity mBureauRateListing; // it's Activity
CurrencySelectorAdapter (Activity activity){
mBureauRateListing =activity;
}
@Override
public void onBindViewHolder(CurrencyViewHolder holder, int position) {
String bsFromActivity = ((BureauRateListing)mBureauRateListing).bs;
}}
Better to start the new activity in your BureauRateListing
activity itself. 最好在
BureauRateListing
活动本身中启动新活动。 Avoid the startActivity
from adapter. 避免使用适配器的
startActivity
。 You could create an View.OnClickListener
member in adapter , provide a setter for the same in adapter setOnClickListener
. 你可以创建一个
View.OnClickListener
适配器成员,提供适配器同一个setter setOnClickListener
。 And from your activity you can set this listener directly. 从您的活动中,您可以直接设置此侦听器。
adapter.setOnClikListener(this)
. adapter.setOnClikListener(this)
。 Implement View.OnClickListener
in activity , and in onClick
start the new activity. 在activity中实现
View.OnClickListener
,然后在onClick
启动新活动。
In your adapter add 在您的适配器中添加
private View.OnClickListener mCLickListener;
Also add 还添加
public void setOnClickListener(View.OnClickListener listener){
mCLickListener = listener;
}
In onBindViewHolder 在onBindViewHolder中
holder.root.setOnClickListener(mCLickListener)
make your BureauRateListingActivity
implements View.OnClickListener
使您的
BureauRateListingActivity
实现View.OnClickListener
Then , in BureauRateListingActivity
inside onClick
method 然后,在
BureauRateListingActivity
内部的onClick
方法
Intent i = new Intent(this, SingleForexDetails.class);
// i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
i.putExtra("bureau_id", BureauId);
i.putExtra("bureau_name",BureauName);
i.putExtra("bureau_phone",BureauPhone);
i.putExtra("bureau_email",BureauEmail);
i.putExtra("website",Website);
i.putExtra("branches",Branches);
i.putExtra("bureau_profile",BureauProfile);
i.putExtra("bureau_logo",BureauLogo);
i.putExtra("bureau_photo",BureauPhoto);
i.putExtra("currency",currency);
i.putExtra("Buying",BuySell);
i.putExtra("Selling",BuySell_two);
startActivity(i);
Finally in your BureauRateListingActivity
set the listener in adapter adapter.setOnClikListener(this).
最后,在
BureauRateListingActivity
,在适配器adapter.setOnClikListener(this).
设置侦听器adapter.setOnClikListener(this).
in MainActivity.java 在MainActivity.java中
public static String bs;
in RecycleViewAdapter.java 在RecycleViewAdapter.java中
import static <app path>.MainActivity.bs;
now it is accessable. 现在可以访问了。
in the constructor of your adapter get an instance of BureauRateListing.java class and store it in a member variable 在适配器的构造函数中,获取BureauRateListing.java类的实例并将其存储在成员变量中
BureauRateListing mAct;
public CurrencySelectorAdapter(BureauRateListing act){
this.mAct = act;
}
now you can access the public instance variables of BureauRateListing class using mAct from any method of CurrencySelectorAdapter class. 现在,您可以从CurrencySelectorAdapter类的任何方法中使用mAct访问BureauRateListing类的公共实例变量 。
for eg 例如
@Override
public void onBindViewHolder(CurrencyViewHolder holder, int position) {
String s = mAct.bs;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.