我是Android的新手,我有两个类活动类BureauRateListing.java
和适配器类CurrencySelectorAdapter.java
。
所以我想从Activity类访问此变量(public String bs;
)到RecyclerView Adapter
方法中的onBindViewHolder()
类,下面是我的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 bs;
访问变量(Activity class
)到onBindViewHolder()
方法内的Adapter类,我将它作为Extra发送给NextActivity。以下是我的onBindViewHolder()
方法,我想访问变量,我将其作为额外发送:
@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);
}
});
}
答案 0 :(得分:2)
试试这个
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();
.
.
.
}
答案 1 :(得分:1)
在适配器类中传递活动。
使用此语法访问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;
}}
答案 2 :(得分:1)
最好在BureauRateListing
活动中开始新活动。避免使用适配器startActivity
。您可以在适配器中创建View.OnClickListener
成员,在适配器setOnClickListener
中为其提供相同的setter。从您的活动中,您可以直接设置此侦听器。 adapter.setOnClikListener(this)
。在活动中实施View.OnClickListener
,在onClick
中启动新活动。
在适配器中添加
private View.OnClickListener mCLickListener;
同时添加
public void setOnClickListener(View.OnClickListener listener){
mCLickListener = listener;
}
在onBindViewHolder
holder.root.setOnClickListener(mCLickListener)
制作BureauRateListingActivity
工具View.OnClickListener
然后,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);
最后在你的BureauRateListingActivity
中设置适配器中的监听器
adapter.setOnClikListener(this).
答案 3 :(得分:0)
在适配器的构造函数中获取 BureauRateListing.java 类的实例并将其存储在成员变量中
BureauRateListing mAct;
public CurrencySelectorAdapter(BureauRateListing act){
this.mAct = act;
}
现在,您可以使用 CurrencySelectorAdapter 类的任何方法中的mAct访问BureauRateListing 类的公共实例变量。
例如
@Override
public void onBindViewHolder(CurrencyViewHolder holder, int position) {
String s = mAct.bs;
}
答案 4 :(得分:0)
在MainActivity.java
public static String bs;
在RecycleViewAdapter.java中
import static <app path>.MainActivity.bs;
现在可以访问了。