In my app i need to start RecyclerAdapter.class and wait for result, but i cant write lines setResult and finish:
public static class MyViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
TextView title;
ImageView icon;
public MyViewHolder(View itemView) {
super(itemView);
title = (TextView) itemView.findViewById(R.id.list_title);
icon = (ImageView) itemView.findViewById(R.id.list_item);
icon.setOnClickListener(this);
}
@Override
public void onClick(View v) {
String selectedCountry;
int country = getPosition();
String ololo = Integer.toString(country);
Log.i("WORKS" ,"Выюрана страна под номером" +ololo);
switch (getPosition()) {
case 0:
selectedCountry = "ukr";
Log.i ("1" ,"" +selectedCountry);
Intent intent= new Intent();
intent.putExtra("country", selectedCountry);
break;
case 1:
selectedCountry = "by";
Log.i ("1" ,"" +selectedCountry);
break;
case 2:
selectedCountry = "rus";
Log.i ("1" ,"" +selectedCountry);
break;
case 3:
selectedCountry = "nrus";
Log.i ("1" ,"" +selectedCountry);
break;
}
//intent. putExtra("country")
}
}
how i need to write it?Please help me and explain what i do wrong I need to setResult from all my cases
EDIT:
public class RecyclerAdapterChooseCountry extends RecyclerView.Adapter<RecyclerAdapterChooseCountry.MyViewHolder> {
private LayoutInflater inflater;
List<CountryInformation> data = Collections.emptyList();
public RecyclerAdapterChooseCountry(Context context, List<CountryInformation> data) {
inflater = LayoutInflater.from(context);
this.data = data;
}
@Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = inflater.inflate(R.layout.country_list, parent, false);
MyViewHolder holder = new MyViewHolder(view, mActivity /*<- cant add this*/ );
return holder;
}
@Override
public void onBindViewHolder(MyViewHolder holder, int position) {
CountryInformation information = data.get(position);
holder.title.setText(information.title);
holder.icon.setImageResource(information.iconId);
}
@Override
public int getItemCount() {
return data.size();
}
public static class MyViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
TextView title;
ImageView icon;
private Activity mActivity;
public MyViewHolder(View itemView, Activity mActivity ) {
super(itemView);
title = (TextView) itemView.findViewById(R.id.list_title);
icon = (ImageView) itemView.findViewById(R.id.list_item);
icon.setOnClickListener(this);
this.mActivity = mActivity;
}
@Override
public void onClick(View v) {
String selectedCountry;
int country = getPosition();
String ololo = Integer.toString(country);
Log.i("WORKS", "Выюрана страна под номером" + ololo);
switch (getPosition()) {
case 0:
selectedCountry = "ukr";
Log.i("1", "" + selectedCountry);
Intent intent = new Intent();
intent.putExtra("country", selectedCountry);
mActivity.setResult(mActivity.RESULT_OK, intent);
mActivity.finish();
break;
case 1:
selectedCountry = "by";
Log.i("1", "" + selectedCountry);
break;
case 2:
selectedCountry = "rus";
Log.i("1", "" + selectedCountry);
break;
case 3:
selectedCountry = "nrus";
Log.i("1", "" + selectedCountry);
break;
}
}
}
}
Can't set method setResult in RecyclerAdapter
setResult
is not accessible inside MyViewHolder
class becuase setResult is define inside Activity class
For accessing setResult
from non Activity class pass Activity context in Adapter constructor when creating object of Adapter:
private Activity mActivity;
public MyViewHolder(View itemView,Activity mActivity) {
super(itemView);
/...your code here..
this.mActivity=mActivity;
}
use mActivity
for accessing setResult
inside onClick of Button
You can use setResult()
and finish()
by passing activity as a parameter to RecyclerAdapter
, then access it using activity.setResult()
, activity.finish()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With