I am new to android , I have two classes an Activity class BureauRateListing.java
and an Adapter class 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;
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. Below is my onBindViewHolder()
method where i want to access the variable and i send it as an 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.
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. Avoid the startActivity
from adapter. You could create an View.OnClickListener
member in adapter , provide a setter for the same in adapter setOnClickListener
. And from your activity you can set this listener directly. adapter.setOnClikListener(this)
. Implement View.OnClickListener
in activity , and in onClick
start the new activity.
In your adapter add
private View.OnClickListener mCLickListener;
Also add
public void setOnClickListener(View.OnClickListener listener){
mCLickListener = listener;
}
In onBindViewHolder
holder.root.setOnClickListener(mCLickListener)
make your BureauRateListingActivity
implements View.OnClickListener
Then , in BureauRateListingActivity
inside onClick
method
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).
in MainActivity.java
public static String bs;
in 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 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.
for eg
@Override
public void onBindViewHolder(CurrencyViewHolder holder, int position) {
String s = mAct.bs;
}
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.