Is any way to transfer the current list of items to a new Activity using OnClickListener of ViewHolder? Or any other way?
public class RVAdapter extends RecyclerView.Adapter<RVAdapter.EventsViewHolder> {
List<Event> events; //get this List
RVAdapter(List<Event> events) {
this.events = events;
}
public static class EventsViewHolder extends RecyclerView.ViewHolder {
TextView date;
TextView text;
ImageView photo;
EventsViewHolder(final View itemView) {
super(itemView);
date = (TextView) itemView.findViewById(R.id.date);
text = (TextView) itemView.findViewById(R.id.text);
photo = (ImageView) itemView.findViewById(R.id.photo);
itemView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Context context = v.getContext();
SendObject sendObject = new SendObject(setList); //and transfer it here
Intent intent = new Intent(context, ReadEvent.class);
Bundle bundle = new Bundle();
bundle.putSerializable("events", sendObject);
bundle.putInt("pos", getAdapterPosition());
bundle.putString("test", "test");
intent.putExtras(bundle);
context.startActivity(intent);
}
});
}
}
Solution 1: Create method in ViewHolder in which you pass your list
Solution 2: Define your ViewHolder as not static class and pass your list
Another approach:
// Solution 1:
public class EventsViewHolder extends RecyclerView.ViewHolder {
EventsViewHolder(final View itemView) {
super(itemView);
}
public void bind(List<Event> events) {
...
SendObject sendObject = new SendObject(events); //and transfer it here
...
}
}
// Solution 2:
public class EventsViewHolder extends RecyclerView.ViewHolder {
EventsViewHolder(final View itemView) {
super(itemView);
itemView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
SendObject sendObject = new SendObject(events); //and transfer it here
}
});
}
}
Adapter Class.
public ServicesHomeAdapter(Context context, ArrayList<HomeScreenData> homeScreenDataArrayList,OnItemClickedListener listener) {
this.context=context;
this.homeScreenDataArrayList=homeScreenDataArrayList;
this.listener=listener;
}
#Override
public ServicesHomeAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType)
{
View view= LayoutInflater.from(parent.getContext()).inflate(R.layout.item_service,parent,false);
ViewHolder viewHolder=new ViewHolder(view);
return viewHolder;
}
#Override
public void onBindViewHolder(final ServicesHomeAdapter.ViewHolder holder, int position) {
final HomeScreenData homeScreenData = homeScreenDataArrayList.get(position);
}
#Override
public int getItemCount()
{
return (null!=homeScreenDataArrayList?homeScreenDataArrayList.size():0);
}
public class ViewHolder extends RecyclerView.ViewHolder {
protected ImageView ivSection;
protected TextView txImageName;
public ViewHolder(View itemView)
{
super(itemView);
this.ivSection= (ImageView) itemView.findViewById(R.id.ivcard);
this.txImageName= (TextView) itemView.findViewById(R.id.txtimgname);
itemView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
listener.onItemClicked(getAdapterPosition());
}
});
}
}
public interface OnItemClickedListener{
void onItemClicked(int position);
}
}
Implement the interface in your Activity.
#Override
public void onItemClicked(int position) {
Intent intent = new Intent(getActivity(), A.class);
Pojo Class pojo = arrayList.get(position);
intent.putExtra(TAG, pojo);
startActivity(intent);
}`
Your Pojo should parseable or serialised but now android recommends to implement parseable then you can pass any Pojo through intent, How you can make your class parseable check this link.
https://developer.android.com/reference/android/os/Parcelable.html
intent.putExtra(key, Serializable/Parcelable) and then in second activity getParcelableExtra(key)/getSerializableExtra(key)
I had a Serializable object already:
public class SendObject implements Serializable{
private List<Event> events;
public SendObject(List<Event> events) {
this.events = events;
}
public List<Event> getEvents() {
return events;
}
}
The main problem was to receive current Adapter List. The problem is solved by using Singleton. The solution was simple.
public class RVAdapter extends RecyclerView.Adapter {
private static RVAdapter rvAdapter;
private List<Event> events;
public RVAdapter() {
}
public static RVAdapter getInstance() {
if (rvAdapter==null) {
rvAdapter = new RVAdapter();
}
return rvAdapter;
}
public void setList(List<Event> events) {
this.events = events;
}
public static class EventsViewHolder extends RecyclerView.ViewHolder {
TextView date;
TextView text;
ImageView photo;
EventsViewHolder(final View itemView) {
super(itemView);
date = (TextView) itemView.findViewById(R.id.date);
text = (TextView) itemView.findViewById(R.id.text);
photo = (ImageView) itemView.findViewById(R.id.photo);
itemView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Context context = v.getContext();
SendObject sendObject = null;
sendObject = new SendObject(RVAdapter.getInstance().events); //current List
Intent intent = new Intent(context, ReadEvent.class);
Bundle bundle = new Bundle();
bundle.putSerializable("events", sendObject);
bundle.putInt("pos", getAdapterPosition());
intent.putExtras(bundle);
context.startActivity(intent);
}
});
}
}
Anyway, thanks for your responses!
Related
I want to start new activity and also passing JSON data to the new activity when i click an item on the recyclerview. i followed a video in youtube: https://www.youtube.com/watch?v=OfsiccfUWVc&index=6&list=PLaoF-xhnnrRW_FGeacuT1VLqnRMKfpp4v but the video didn't show how to go to new activity once i clicked the item on recyclerview. from the video, i wanted to click on the item and then new activity will bring me to the 'more detail page' about the item.
i have tried to do the intent but i am not sure how to call from the new main activity
this is my adapter code:
public class LonghouseAdapter extends RecyclerView.Adapter<LonghouseViewHolder> {
Context context;
List<LonghousesCategoryOne> longhousesCategoryOneList;
List<Category> categories;
private OnItemClickListener mlistener;
public LonghouseAdapter(Context context, List<LonghousesCategoryOne> longhousesCategoryOneList) {
this.context = context;
this.longhousesCategoryOneList = longhousesCategoryOneList;
}
#NonNull
#Override
public LonghouseViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
View itemView = LayoutInflater.from(context).inflate(R.layout.longhouse_item_layout,null);
return new LonghouseViewHolder(itemView);
}
#Override
public void onBindViewHolder(#NonNull LonghouseViewHolder holder, final int position) {
holder.txt_price.setText(new StringBuilder("").append(longhousesCategoryOneList.get(position).Price));
holder.txt_longhouse_name.setText(new StringBuilder("").append(longhousesCategoryOneList.get(position).Name));
Picasso.with(context)
.load(longhousesCategoryOneList.get(position).Link)
.into(holder.img_longhouse);
holder.setItemClickListener(new IItemClickListener() {
#Override
public void onClick(View v) {
Common.currentCategory = categories.get(position);
context.startActivity(new Intent(context, descriptionsActivity.class));
}
});
}
#Override
public int getItemCount() {
return longhousesCategoryOneList.size();
}
}
this is my viewholder code:
public class LonghouseViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
ImageView img_longhouse;
TextView txt_longhouse_name, txt_price;
public void setItemClickListener(IItemClickListener itemClickListener) {
this.itemClickListener = itemClickListener;
}
IItemClickListener itemClickListener;
public LonghouseViewHolder(View itemView) {
super(itemView);
img_longhouse = (ImageView) itemView.findViewById(R.id.image_longhouse);
txt_longhouse_name = (TextView) itemView.findViewById(R.id.txt_longhouse_name);
txt_price = (TextView) itemView.findViewById(R.id.txt_longhouse_price);
itemView.setOnClickListener(this);
}
#Override
public void onClick(View v) {
itemClickListener.onClick(v);
}
}
in my descriptionActivity, how do i set the item to my xml view?so far i have done this.
public class descriptionsActivity extends AppCompatActivity {
retreatSecondApi mservice;
ImageView img_longhouse;
TextView txt_longhouse_description, txt_price;
Context context;
List<LonghousesCategoryOne> longhousesCategoryOneList;
CompositeDisposable compositeDisposable = new CompositeDisposable();
#Override
protected void onCreate(Bundle savedInstanceState) {
mservice = Common.getAPI();
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_descriptions);
img_longhouse = (ImageView)findViewById(R.id.placeImageView);
txt_longhouse_description = (TextView)findViewById(R.id.placeDescTextView);
txt_price = (TextView)findViewById(R.id.txt_longhouse_price);
txt_longhouse_description.setText()
}
}
Can anyone guide me on this? Thank you
As #rgaraisayev recommends in your onClick() listener uncomment the call to startActivity() i.e.
holder.setItemClickListener(new IItemClickListener() {
#Override
public void onClick(View v) {
Common.currentCategory = categories.get(position);
context.startActivity(new Intent(context, descriptionsActivity.class));
}
});
solved it, so in my holder view:
holder.setItemClickListener(new IItemClickListener() {
#Override
public void onClick(View v) {
Common.longhousedesc = longhousesCategoryOneList.get(position);
context.startActivity(new Intent(context, descriptionsActivity.class));
}
});
and in my new activity, i bind the data to my xml view like this:
txt_longhouse_description.setText(Common.longhousedesc.descriptions);
public class MyAdapter extends RecyclerView.Adapter<MyAdapter.MyViewHolder> {
private String[] mDatasetname;
private Integer[] mexp;
Context context;
public ToggleButton select;
Integer selectedcountint=0;
private Bitmap[] mpro;
private String[] mloc;
private String[] mobj;
private String[] mselected;
public ArrayList<String> nselected = new ArrayList<>();
public static class MyViewHolder extends RecyclerView.ViewHolder{
public CardView mCardView;
public TextView mTextView;
public TextView texp;
public Button pdf;
public ToggleButton select;
public ImageView pro;
public TextView loc;
public MyViewHolder(View v){
super(v);
mCardView = (CardView) v.findViewById(R.id.card_view);
mTextView = (TextView) v.findViewById(R.id.tv_text);
texp = (TextView) v.findViewById(R.id.setexp);
pdf = (Button) v.findViewById(R.id.moreinfo);
select = (ToggleButton) v.findViewById(R.id.select);
pro = (ImageView) v.findViewById(R.id.setpropic);
loc = (TextView) v.findViewById(R.id.setlocation);
}
}
public MyAdapter(String[] myDataset,Integer[] exp,Bitmap[] pro,String[] loc,String[] obj){
mDatasetname = myDataset;
mexp=exp;
mpro=pro;
mloc=loc;
mobj=obj;
}
#Override
public MyAdapter.MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType){
context=parent.getContext();
View vs = LayoutInflater.from(parent.getContext()).inflate(R.layout.card_item, parent, false);
MyViewHolder vh = new MyViewHolder(vs);
return vh;
}
#Override
public void onBindViewHolder(final MyViewHolder holder, final int position){
holder.mTextView.setText(mDatasetname[position]);
holder.texp.setText(String.valueOf(mexp[position])+" yrs");
holder.pro.setImageBitmap(mpro[position]);
holder.loc.setText(mloc[position]);
holder.select.setText("rejected");
holder.pdf.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent=new Intent();
intent = new Intent(context, PdfViewer.class);
intent.putExtra("obj",mobj[position]);
context.startActivity(intent);
}
});
holder.select.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked==true) {
holder.select.setChecked(true);
holder.select.setText("selected");
nselected.remove(mobj[position]+",rejected");
nselected.add(mobj[position]+",selected");
Log.d("dei", String.valueOf(nselected));
selectedcountint=selectedcountint+1;
Intent intent = new Intent("custom-message");
// intent.putExtra("quantity",Integer.parseInt(quantity.getText().toString()));
Bundle args = new Bundle();
args.putSerializable("ARRAYLIST",(Serializable)nselected);
intent.putExtra("BUNDLE",args);
intent.putExtra("totalval",selectedcountint);
LocalBroadcastManager.getInstance(context).sendBroadcast(intent);
} else {
holder.select.setChecked(false);
holder.select.setText("rejected");
nselected.remove(mobj[position]+",selected");
nselected.add(mobj[position]+",rejected");
Log.d("dei", String.valueOf(nselected));
selectedcountint=selectedcountint-1;
Intent intent = new Intent("custom-message");
// intent.putExtra("quantity",Integer.parseInt(quantity.getText().toString()));
Bundle args = new Bundle();
args.putSerializable("ARRAYLIST",(Serializable)nselected);
intent.putExtra("BUNDLE",args);
intent.putExtra("totalval",selectedcountint);
LocalBroadcastManager.getInstance(context).sendBroadcast(intent);
}
}
});
}
#Override
public int getItemCount() { return mDatasetname.length; }
}
I have a recycler view in my app. I am having a problem. There is an item so that when I click on the item a the funtion in the click b is executed. It would be very great when you clean up the below code for me because I am new to programming, so that I won't be able to solve the problem.
if(getAdapterPosition() != RecyclerView.NO_POSITION) {
...
mobj[getAdapterPosition()]
...
}
It's better if you set your data and listeners in the ViewHolder. You should create a method in your ViewHolder class and pass there item from your list as a parameter. In this method you copy all your code for setting text and listeners. Then in onBindViewHolder you call this method.
First create a class that will be our Adapter item (you should change the names of the fields because I don't know what should they represent, this is just an example):
class AdapterItem {
private String firstString;
private Integer integerValue;
private Bitmap bitmap;
private String secondString;
private String thirdString;
// create also getters, setters for fields
}
Your adapters constructor will look something like this:
public MyAdapter(ArrayList<AdapterItem> data){
this.data = data;
}
And the onBindViewHolder method:
#Override
public void onBindViewHolder(final MyViewHolder holder, final int position){
holder.bind(data[position]);
}
In the ViewHolder class:
void bind(AdapterItem item) {
mTextView.setText(item.getFirstString());
pdf.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(context, PdfViewer.class);
intent.putExtra("obj", item.getThirdString());
v.getContext().startActivity(intent);
}
});
}
As correctly pointed out by #andrei The code to get the correct string from the obj[] array should be as shown:
public MyAdapter(Context context, String[] myDataset,Integer[] exp,Bitmap[] pro,String[] loc,String[] obj){
mContext = context; // make mContext as a variable like others
mDatasetname = myDataset;
mexp=exp;
mpro=pro;
mloc=loc;
mobj=obj;
}
holder.pdf.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent=new Intent();
intent = new Intent(context, PdfViewer.class);
intent.putExtra("obj",mobj[holder.getAdapterPosition()]);
mContext.startActivity(intent);
}
});
And similarly replace all occurrences of position with holder.getAdapterPosition() because holder.getAdapterPosition() will always give you the updated position for an item of recycler view in your code as if you update or delete items without using notifyDataSetChanged() method then the onBindViewHolder will not be called again hence the position will become inconsistent. For detailed information you can refer to this answer.
public class MyAdapter extends RecyclerView.Adapter {
private String[] mDatasetname;
private Integer[] mexp;
private Context context;
public ToggleButton select;
Integer selectedcountint=0;
private Bitmap[] mpro;
private String[] mloc;
private String[] mobj;
public Button pdf;Context m;private String[] mselected;
public ArrayList<String> nselected = new ArrayList<>();
public static class MyViewHolder extends RecyclerView.ViewHolder{
public CardView mCardView;
public TextView mTextView;
public TextView texp;
public Button pdf;
public ToggleButton select;
public ImageView pro;
public MainActivity activity;
public Context ipaset;
public TextView loc;
public View layout;
View forresumeview;
String forresume;
private RecyclerView.ViewHolder s;
public MyViewHolder(final View v) {
super(v);
mCardView = (CardView) v.findViewById(R.id.card_view);
mTextView = (TextView) v.findViewById(R.id.tv_text);
texp = (TextView) v.findViewById(R.id.setexp);
pdf = (Button) v.findViewById(R.id.moreinfo);
select = (ToggleButton) v.findViewById(R.id.select);
pro = (ImageView) v.findViewById(R.id.setpropic);
loc = (TextView) v.findViewById(R.id.setlocation);
forresumeview=v;
}
void bind(String name, Integer exp, String location, final Bitmap image, final String obj ){
mTextView.setText(name);
texp.setText(String.valueOf(exp) + " yrs");
pro.setImageBitmap(image);
loc.setText(location);
}
public void doonce(final String s){
pdf.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View vs) {
Intent intent = new Intent(forresumeview.getContext(), PdfViewer.class);
intent.putExtra("obj", s);
forresumeview.getContext().startActivity(intent);
// Log.d("sdasd", obj);
}
});
}
}
public MyAdapter(String[] myDataset,Integer[] exp,Bitmap[] pro,String[] loc,String[] obj){
mDatasetname = myDataset;
mexp=exp;
mpro=pro;
mloc=loc;
mobj=obj;
}
#Override
public MyAdapter.MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType){
context=parent.getContext();
View vs = LayoutInflater.from(parent.getContext()).inflate(R.layout.card_item, parent, false);
MyViewHolder vh = new MyViewHolder(vs);
return vh;
}
#Override
public void onBindViewHolder(final MyViewHolder holder, final int position){
holder.bind(mDatasetname[position],mexp[position],mloc[position],mpro[position],mobj[position]);
holder.doonce(mobj[position]);
}
#Override
public int getItemCount() { return mDatasetname.length; }
}
this is the answer ,,,, it works i tried it !!!! Happy coding ......
i have a recyclerview and i want to when click on it receive the position and one String and sent to other activity and run it
i dont know where this code has problem.
Mainactivity
#Override
public void onItemClick(int i) {
datamodel clickedItem = postlist.get(i);
Intent detailIntent = new Intent(MainActivity.this, Main2Activity.class);
detailIntent.putExtra(EXTRA_URL, clickedItem.getmTitle());
startActivity(detailIntent);
}
Adapter codes:
public class rrecyclerviewadapter extends RecyclerView.Adapter<rrecyclerviewadapter.myholder> {
private OnItemClickListener mListener;
private Context context;
private List<datamodel>mylist;
public interface OnItemClickListener {
void onItemClick(int i);
}
public void setOnItemClickListener(OnItemClickListener listener) {
this.mListener = listener;
}
public rrecyclerviewadapter(Context context, List<datamodel>mylist){
this.context=context;
this.mylist=mylist;
}
#NonNull
#Override
public myholder onCreateViewHolder(#NonNull ViewGroup viewGroup, int i) {
View view= LayoutInflater.from(context).inflate(R.layout.each_item,viewGroup,false);
return new myholder(view);
}
#Override
public void onBindViewHolder(#NonNull myholder myholder, int i) {
datamodel datamodel=mylist.get(i);
myholder.eachitemtxt.setText(datamodel.getmTitle());
Picasso.with(context).load(datamodel.getMimageurl()).placeholder(R.drawable.pic3).error(R.drawable.pic4).into(myholder.eachitemimg);
}
#Override
public int getItemCount() {
return mylist.size();
}
public class myholder extends RecyclerView.ViewHolder {
private TextView eachitemtxt;
private ImageView eachitemimg;
public myholder(#NonNull final View itemView) {
super(itemView);
eachitemimg = (ImageView) itemView.findViewById(R.id.imageview_eachitem);
eachitemtxt = (TextView) itemView.findViewById(R.id.textview_eachitem);
itemView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Toast.makeText(context, "id:" + getAdapterPosition(),
Toast.LENGTH_SHORT).show();
int i=getAdapterPosition();
if (i != RecyclerView.NO_POSITION) {
mListener.onItemClick(i);
}}
});}}}
here there is full adapter code
on other activity i get intent.
in the adapter when testing Toast its completely works but on the mlistener and
setting id it does not work
found it.
just need to add
Adapter.setOnItemClickListener(Activitymain.this)
to your Mainactivity
i just changed your holder class in adapter try the below code
public class myholder extends RecyclerView.ViewHolder implements View.OnClickListener {
private TextView eachitemtxt;
private ImageView eachitemimg;
public myholder(#NonNull final View itemView) {
super(itemView);
eachitemimg = (ImageView) itemView.findViewById(R.id.imageview_eachitem);
eachitemtxt = (TextView) itemView.findViewById(R.id.textview_eachitem);
itemView.setOnClickListener(this);
}
#Override
public void onClick(View v) {
int p = getAdapterPosition();
datamodel clickedItem = postlist.get(p);
Intent detailIntent = new Intent(context, Main2Activity.class);
detailIntent.putExtra(EXTRA_URL, clickedItem.getmTitle());
context.startActivity(detailIntent);
}
}
I want to call a function in the ActivityClass.java, from RecyclerView.Adapter class.
Below is my ActivityClass.java function:
public void payslipActivityNav() {
Bundle b = new Bundle();
if (b != null) {
Intent intent = new Intent(HomePage.this, PayslipActivity.class);
b.putString("UserName", lblUserName.getText().toString());
b.putString("UserDesignation", lblDesignation.getText().toString());
intent.putExtras(b);
startActivity(intent);
}
}
And this is the class inside my RecyclerView.Adapter class:
public class SingleItemRowHolder extends RecyclerView.ViewHolder {
protected TextView tvTitle;
protected ImageView itemImage;
protected TextView lblDescription;
public SingleItemRowHolder(final View view) {
super(view);
this.tvTitle = (TextView) view.findViewById(R.id.tvTitle);
this.itemImage = (ImageView) view.findViewById(R.id.itemImage);
this.lblDescription = (TextView) view.findViewById(R.id.lblDescription);
view.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
strSelectedText = tvTitle.getText().toString();
Toast.makeText(v.getContext(), strSelectedText, Toast.LENGTH_SHORT).show();
switch (strSelectedText){
case "Pay":
((ActivityClass)mContext).payslipActivityNav();
break;
}
}
});
}
}
This is the error I am getting java.lang.ClassCastException: android.app.Application cannot be cast to packageName.ActivityClass
Is there anything wrong with which I am accessing the ActivityClass method?
Or should the method calling be performed inside that inner class or anywhere else inside the RecyclerView.Adapter class.
Help would be appreciated.
Thanks in advance!
UPDATE:
I solved it by using this. Something crossed my mind and I tried this and it worked:
switch (strSelectedText){
case "Pay":
Intent intent = new Intent(mContext.getApplicationContext(), ActivityClass.class);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
mContext.startActivity(intent);
break;
}
Thank You All Of You.
try this : ((YourActivity) context).your_method();//method should be public
public static void payslipActivityNav() {
Bundle b = new Bundle();
if (b != null) {
Intent intent = new Intent(HomePage.this, PayslipActivity.class);
b.putString("UserName", lblUserName.getText().toString());
b.putString("UserDesignation", lblDesignation.getText().toString());
intent.putExtras(b);
startActivity(intent);
}
}
public class SingleItemRowHolder extends RecyclerView.ViewHolder {
protected TextView tvTitle;
protected ImageView itemImage;
protected TextView lblDescription;
public SingleItemRowHolder(final View view) {
super(view);
this.tvTitle = (TextView) view.findViewById(R.id.tvTitle);
this.itemImage = (ImageView) view.findViewById(R.id.itemImage);
this.lblDescription = (TextView) view.findViewById(R.id.lblDescription);
view.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
strSelectedText = tvTitle.getText().toString();
Toast.makeText(v.getContext(), strSelectedText, Toast.LENGTH_SHORT).show();
switch (strSelectedText){
case "Pay":
ActivityClass.payslipActivityNav();
break;
}
}
});
}
}
You have to use callback interface to do that. And that is the safe way to implement this. Also casting context won't make the code reusable.
You should create an interface inside your adapter and pass the value through it.
Your adapter code should be like this:
public class MyAdapter extends RecyclerView.Adapter<MyAdapter.MyViewHolder> {
private OnItemClickListener onItemClickListener;
#Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
}
#Override
public void onBindViewHolder(MyViewHolder holder, int position) {
}
#Override
public int getItemCount() {
}
public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
this.onItemClickListener = onItemClickListener;
}
public void setData(List<AccountProvider> accountProviders) {
this.accountProviders = accountProviders;
notifyDataSetChanged();
}
class MyViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
MyViewHolder(View itemView) {
super(itemView);
itemView.setOnClickListener(this);
}
#Override
public void onClick(View v) {
if (onItemClickListener != null)
onItemClickListener.onItemClick(tvTitle.getText().toString());
}
}
}
Interface:
public interface OnItemClickListener {
void onItemClick(String value);
}
In your Activity:
public MyActivity implements OnItemClickListener{
#Override
protected void onCreate(Bundle savedInstanceState) {
MyAdapter adapter = new MyAdapter();
adapter.setOnItemClickListener(this);
yourRecyclerView.setAdapter(adapter);
}
#Override
public void onItemClick(String value){
switch (strSelectedText){
case "Pay":
payslipActivityNav();
break;
}
}
}
If you need any value from your adapter you can pass it via the interface.
Hope it helps:)
I have used CardView inside a RecyclerView in horizontal manner. Now what I want to do is, when user click on a card then the data which I have putted into the intent with the help of putExtra function should be passed to the next activity.
Here is the code
public class SectionListDataAdapter extends RecyclerView.Adapter<SectionListDataAdapter.SingleItemRowHolder> {
private List<Section> itemsList;
private Context mContext;
public SectionListDataAdapter(Context context, List<Section> itemsList) {
this.itemsList = itemsList;
this.mContext = context;
}
#Override
public SingleItemRowHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
View v = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.list_single_card, null);
SingleItemRowHolder mh = new SingleItemRowHolder(v);
return mh;
}
#Override
public void onBindViewHolder(SingleItemRowHolder holder, int i) {
Section singleItem = itemsList.get(i);
holder.tvTitle.setText(singleItem.getName());
holder.tvAuthor.setText(singleItem.getAuthorname());
holder.tvPremium.setText(singleItem.getPremium());
Glide.with(mContext)
.load(singleItem.getImage()
.into(holder.itemImage);
}
#Override
public int getItemCount() {
return (null != itemsList ? itemsList.size() : 0);
}
public class SingleItemRowHolder extends RecyclerView.ViewHolder {
protected TextView tvTitle, tvAuthor, tvPremium;
protected ImageView itemImage;
public SingleItemRowHolder(final View view) {
super(view);
this.tvTitle = (TextView) view.findViewById(R.id.tvTitle1);
this.tvAuthor = (TextView) view.findViewById(R.id.tvTitle2);
this.tvPremium = (TextView) view.findViewById(R.id.txtSubText1);
this.itemImage = (ImageView) view.findViewById(R.id.itemImage);
view.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(v.getContext(), BooksDescription.class);
intent.putExtra("id", NavigationDrawer.ID.get());
mContext.startActivity(intent);
}
});
}
}
}
Here in the line intent.putExtra("id", NavigationDrawer.ID); NavigationDrawe.ID having 20 integer data. Now which function should I use to get the data of only those card which is clicked.