My this question is different from the previous one.
I have this genetic ViewHolder for RecyclerView,
public class BaseViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener, View.OnLongClickListener {
private CardView cardView;
private AppCompatImageView imgEdit;
private AppCompatImageView imgDelete;
private ItemClickListener itemClickListener;
public BaseViewHolder(View itemView, ItemClickListener itemClickListener) {
super(itemView);
cardView = itemView.findViewById(R.id.cardView);
imgEdit = itemView.findViewById(R.id.imgEdit);
imgDelete = itemView.findViewById(R.id.imgDelete);
imgDelete.setOnClickListener(this);
cardView.setOnClickListener(this);
cardView.setOnLongClickListener(this);
this.itemClickListener = itemClickListener;
}
#Override
public void onClick(View view) {
if (view.getId() == cardView.getId()) {
itemClickListener.onClick(view, getLayoutPosition(), ConstantCodes.ACTION_CLICK);
} else if (view.getId() == imgDelete.getId()) {
itemClickListener.onClick(view, getLayoutPosition(), ConstantCodes.ACTION_DELETE);
} else if (view.getId() == imgEdit.getId()) {
itemClickListener.onClick(view, getLayoutPosition(), ConstantCodes.ACTION_EDIT);
}
}
#Override
public boolean onLongClick(View view) {
itemClickListener.onLongClick(view, getLayoutPosition());
return false;
}
}
Here is my genetic Adapter class for RecyclerViewer
public class BaseRecyclerViewAdapter extends RecyclerView.Adapter<BaseViewHolder> {
private ItemClickListener itemClickListener;
private List<? extends Object> objectArrayList;
private int layout;
public BaseRecyclerViewAdapter(int layout, ItemClickListener itemClickListener, List<? extends Object> objectArrayList) {
this.layout = layout;
this.itemClickListener = itemClickListener;
this.objectArrayList = objectArrayList;
}
#NonNull
#Override
public BaseViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
View itemView = LayoutInflater.from(parent.getContext()).inflate(layout, parent, false);
return new BaseViewHolder(itemView, itemClickListener);
}
#Override
public void onBindViewHolder(#NonNull BaseViewHolder holder, int position) {
}
#Override
public int getItemCount() {
return objectArrayList.size();
}
}
When I implement the adapter in my activity class, it shows me an compile time error "Method does not override from its superclass". I already checked similar questions on google & in SO. You can see that there is not typing issue in my implementation. Then what is the actual problem & how can I resolve it ?
Related
I want to implement a recycler view click event. I intended to have an event when I clicked tvRoomNum. But error 'TextView.setOnClickListener(android.view.View$OnClickListener)' on a null object reference' occurs. So I notice that tvRoomNum is null object and attempt to use 'findViewById'. But the function is not recognized. How can I solve the problem?
public class RoomAdapter extends RecyclerView.Adapter<RoomAdapter.CustomViewHolder> {
private Context context;
private ArrayList<RoomData> rooms;
public ArrayList<StudnetInRoomData> students;
private LayoutInflater inflater;
private RoomData room;
View view;
public RoomAdapter(Context context, ArrayList<RoomData> rooms) {
this.context = context;
this.rooms = rooms;
this.inflater = LayoutInflater.from(context);
students = new ArrayList<>();
}
#Override
public CustomViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
view = inflater.inflate(R.layout.single_room, parent, false);
return new CustomViewHolder(view);
}
#Override
public void onBindViewHolder(#NonNull CustomViewHolder holder, int position) {
room = rooms.get(position);
holder.tvRoomNum.setText(String.valueOf(room.roomNum));
}
#Override
public int getItemCount() {
return rooms.size();
}
public class CustomViewHolder extends RecyclerView.ViewHolder {
public TextView tvRoomNum;
public CustomViewHolder(View itemView) {
super(itemView);
tvRoomNum.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View view) {
Intent intent = new Intent(view.getContext(), PlusStudentActivity.class);
intent.putExtra("studentList", room.students);
context.startActivity(intent);
}
});
}
}
}
The code below is the adapter of the recycler view that implements the recycler view. This is a dual recircular view structure.
public class FloorAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> implements OnFloorItemClickListener {
OnFloorItemClickListener listener;
static public ArrayList<FloorData> floors;
private Context context;
private LayoutInflater layoutInflater;
private OnItemClickListener mListener = null;
public FloorAdapter(ArrayList<FloorData> floors, Context context) {
this.floors = floors;
this.context = context;
this.layoutInflater = LayoutInflater.from(context);
}
public interface OnItemClickListener{
void onItemClick(View v, int pos);
}
public void setOnItemClickListener(OnItemClickListener listener) {
this.mListener = listener ;
}
#Override
public RecyclerView.ViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
View view = layoutInflater.inflate(R.layout.signle_floor, parent, false);
return new GridViewHolder(view);
}
#Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
((GridViewHolder)holder).recyclerView.setAdapter(new RoomAdapter(context, floors.get(position).rooms));
((GridViewHolder)holder).recyclerView.setLayoutManager(new GridLayoutManager(context, 5));
((GridViewHolder)holder).recyclerView.setHasFixedSize(true);
((GridViewHolder)holder).tvFloorNum.setText(String.valueOf(floors.get(position).floorNum));
}
#Override
public int getItemCount() {
return floors.size();
}
#Override public void onItemClick(RecyclerView.ViewHolder holder, View view, int position) {
if(listener != null){
listener.onItemClick(holder,view,position);
}
}
#Override
public int getItemViewType(int position) {
return floors.get(position).id;
}
public class GridViewHolder extends RecyclerView.ViewHolder {
RecyclerView recyclerView;
TextView tvFloorNum;
Button btnPlusRoom;
public GridViewHolder(View itemView) {
super(itemView);
recyclerView = itemView.findViewById(R.id.rvRooms);
tvFloorNum = itemView.findViewById(R.id.tvFloorNum);
btnPlusRoom = (Button)itemView.findViewById(R.id.btnPlusRoom);
btnPlusRoom.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View v)
{
int pos = getAdapterPosition();
if (pos != RecyclerView.NO_POSITION)
{
if(mListener != null){
mListener.onItemClick(v, pos);
}
}
}
});
}
}
}
Your TextView is null, because you are missing the findViewById():
You find the Id on a View, in this case, it is the itemView
The function was not recognised because you would've just called findViewById() & ViewHolder does not have that method,
You should've called itemView.findViewById() instead.
Try this:
public class CustomViewHolder extends RecyclerView.ViewHolder {
public TextView tvRoomNum;
public CustomViewHolder(View itemView) {
super(itemView);
tvRoomNum = itemView.findViewById(R.id.your_textview_id) // THIS
tvRoomNum.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View view) {
Intent intent = new Intent(view.getContext(), PlusStudentActivity.class);
intent.putExtra("studentList", room.students);
context.startActivity(intent);
}
});
}
}
I am bumbing my head on trying to implement a ClickListener in an adapter for a DialogFragment.
I am getting the error on the codeline:
holder.bind(tzmodel.get(position), listener);
Error says (bind() in ViewHolder cannot be applied). Seems it does not match the ViewHolder code.
Could somebody please assist?
The code for my timeZoneAdapter:
public class timeZoneAdapter extends RecyclerView.Adapter<timeZoneAdapter.ViewHolder> {
public interface OnItemClickListener {
void onItemClick(TimezoneModel item);
}
private Context c;
private List<TimezoneModel> tzmodel;
private final OnItemClickListener listener;
public timeZoneAdapter(Context c, List<TimezoneModel> tzModel, OnItemClickListener listener) {
this.c = c;
this.tzmodel = tzModel;
this.listener = listener;
}
//INITIALIE TZ
#NonNull
#Override
public ViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.timezonemodel, parent, false);
return new ViewHolder(v);
}
#Override
public void onBindViewHolder(#NonNull ViewHolder holder, int position) {
holder.bind(tzmodel.get(position), listener); <-- error
}
#Override
public int getItemCount() {
return tzmodel.size();
}
class ViewHolder extends RecyclerView.ViewHolder {
TextView timeTxt, currenttimeTxt, regionTxt;
public ViewHolder(View itemView) {
super(itemView);
timeTxt= (TextView) itemView.findViewById(R.id.timeTxt);
currenttimeTxt= (TextView) itemView.findViewById(R.id.currentTimeTxt);
regionTxt= (TextView) itemView.findViewById(R.id.regionTxt);
}
public void bind(ViewHolder viewHolder, int i, final OnItemClickListener listener) {
viewHolder.timeTxt.setText(tzmodel.get(i).getTime());
viewHolder.currenttimeTxt.setText(tzmodel.get(i).getCurrentTime());
viewHolder.regionTxt.setText(tzmodel.get(i).getRegion());
itemView.setOnClickListener(new View.OnClickListener() {
#Override public void onClick(View v) {
listener.onItemClick((TimezoneModel) tzmodel);
}
});
}
}
}
The problem is the parameters that you put in the bind method that you define.
You define the public method bind in the ViewHolder class in this way:
public void bind(ViewHolder viewHolder, int i, final OnItemClickListener listener){
//...
}
You can fix your compile error change the call to :
#Override
public void onBindViewHolder(#NonNull ViewHolder holder, int position) {
holder.bind(holder,position, listener);
}
I also share with you a couple of correction because if you create a public method in the ViewHolder class, you don't need to recipe the viewHolder instance because you can access with this operator:
public class TimeZoneAdapter extends RecyclerView.Adapter<TimeZoneAdapter.ViewHolder> {
public interface OnItemClickListener {
void onItemClick(TimezoneModel item);
}
private Context c;
private List<TimezoneModel> tzmodel;
private final OnItemClickListener listener;
public TimeZoneAdapter(Context c, List<TimezoneModel> tzModel, OnItemClickListener listener) {
this.c = c;
this.tzmodel = tzModel;
this.listener = listener;
}
#NonNull
#Override
public ViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.item, parent, false);
return new ViewHolder(v);
}
#Override
public void onBindViewHolder(#NonNull ViewHolder holder, int position) {
holder.bind(tzmodel.get(position), listener);
}
#Override
public int getItemCount() {
return tzmodel.size();
}
class ViewHolder extends RecyclerView.ViewHolder {
TextView timeTxt, currenttimeTxt, regionTxt;
public ViewHolder(View itemView) {
super(itemView);
timeTxt= itemView.findViewById(R.id.timeTxt);
currenttimeTxt= itemView.findViewById(R.id.currentTimeTxt);
regionTxt= itemView.findViewById(R.id.regionTxt);
}
public void bind(TimezoneModel item , final OnItemClickListener listener) {
this.timeTxt.setText(item.getTime());
this.currenttimeTxt.setText(item.getCurrentTime());
this.regionTxt.setText(item.getRegion());
itemView.setOnClickListener(new View.OnClickListener() {
#Override public void onClick(View v) {
listener.onItemClick((TimezoneModel) tzmodel);
}
});
}
}
}
For some reason the on bindviewholder is not accepting the variables created by Viewholder(holder.name and holder.dexription). I have tried everything to fix it but nothing has worked Here is the code if you could help I would be grateful.
public class CityAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
private ArrayList<City> cities;
public CityAdapter(ArrayList<City> cities) {
this.cities =cities;
}
#Override
public int getItemCount() {
if (cities != null) {
return cities.size();
} else {
return 0;
}
}
public ViewHolder onCreateViewHolder(#NonNull ViewGroup parent,int viewType){
View v =(View) LayoutInflater.from(parent.getContext()).inflate(R.layout.items,parent,false);
return new ViewHolder(v);
}
#Override
public void onBindViewHolder(#NonNull RecyclerView.ViewHolder holder, int position) {
City city = cities.get(position);
holder.name.setText(city.getName());
holder.description.setText(city.getDescription());
}
static class ViewHolder extends RecyclerView.ViewHolder {
public View view;
public TextView name;
public TextView description;
public ImageView image;
public ViewHolder(#NonNull View itemView) {
super(itemView);
this.name=(TextView) itemView.findViewById(R.id.name);
this.description=(TextView) itemView.findViewById(R.id.description);
this.image=(ImageView) itemView.findViewById(R.id.image);
}
}
}
You are extending from wrong class
change CityAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder>
to
CityAdapter extends RecyclerView.Adapter<CityAdapter.ViewHolder>
And change your "onBindViewHolder" method's first argument from
RecyclerView.ViewHolder
to
ViewHolder
In my android project, I created Generic RecyclerView's Adapter class & Viewholder class like below,
Adapter class,
public class BaseRecyclerViewAdapter extends RecyclerView.Adapter<BaseViewHolder> {
private ItemClickListener itemClickListener;
private List<? extends Object> objectArrayList;
private int layout;
private BaseViewHolder baseViewHolder;
public BaseRecyclerViewAdapter(int layout, ItemClickListener itemClickListener, List<? extends Object> objectArrayList) {
this.layout = layout;
this.itemClickListener = itemClickListener;
this.objectArrayList = objectArrayList;
}
public BaseRecyclerViewAdapter(BaseViewHolder baseViewHolder, int layout,
ItemClickListener itemClickListener, List<? extends Object> objectArrayList) {
this.baseViewHolder = baseViewHolder;
this.layout = layout;
this.itemClickListener = itemClickListener;
this.objectArrayList = objectArrayList;
}
#NonNull
#Override
public BaseViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
View itemView = LayoutInflater.from(parent.getContext()).inflate(layout, parent, false);
return new BaseViewHolder(itemView, itemClickListener);
}
#Override
public void onBindViewHolder(#NonNull BaseViewHolder holder, int position) {
}
#Override
public int getItemCount() {
return objectArrayList.size();
}
}
ViewHolder class
public class BaseViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener, View.OnLongClickListener {
private CardView cardView;
private AppCompatImageView imgEdit;
private AppCompatImageView imgDelete;
private ItemClickListener itemClickListener;
public BaseViewHolder(View itemView, ItemClickListener itemClickListener) {
super(itemView);
cardView = itemView.findViewById(R.id.cardView);
imgEdit = itemView.findViewById(R.id.imgEdit);
imgDelete = itemView.findViewById(R.id.imgDelete);
imgDelete.setOnClickListener(this);
cardView.setOnClickListener(this);
cardView.setOnLongClickListener(this);
this.itemClickListener = itemClickListener;
}
#Override
public void onClick(View view) {
if (view.getId() == cardView.getId()) {
itemClickListener.onClick(view, getLayoutPosition(), ConstantCodes.ACTION_CLICK);
} else if (view.getId() == imgDelete.getId()) {
itemClickListener.onClick(view, getLayoutPosition(), ConstantCodes.ACTION_DELETE);
} else if (view.getId() == imgEdit.getId()) {
itemClickListener.onClick(view, getLayoutPosition(), ConstantCodes.ACTION_EDIT);
}
}
#Override
public boolean onLongClick(View view) {
itemClickListener.onLongClick(view, getLayoutPosition());
return false;
}
}
Here is the way I implement the above adapter & viewholder in my activity class
private class DispatchViewHolder extends BaseViewHolder {
AppCompatTextView txtInvoiceNo;
AppCompatTextView txtVehicleNo;
AppCompatTextView txtPartyName;
AppCompatTextView txtNoOfBags;
AppCompatTextView txtMaterialType;
AppCompatTextView txtWeight;
DispatchViewHolder(View itemView, ItemClickListener itemClickListener) {
super(itemView, itemClickListener);
}
}
private class DispatchMaterialAdapter extends BaseRecyclerViewAdapter {
ItemClickListener itemClickListener;
List<? extends Object> objectArrayList;
DispatchMaterialAdapter(ItemClickListener itemClickListener, List<? extends Object> objectArrayList) {
super(R.layout.dispatch_material_row, itemClickListener, objectArrayList);
this.itemClickListener = itemClickListener;
this.objectArrayList = objectArrayList;
}
#NonNull
#Override
public DispatchViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
View itemView = LayoutInflater.from(parent.getContext()).inflate(R.layout.dispatch_material_row, parent, false);
return new DispatchViewHolder(itemView, itemClickListener);
}
#Override
public void onBindViewHolder(#NonNull BaseViewHolder holder, int position) {
try {
DispatchViewHolder dispatchViewHolder = (DispatchViewHolder) holder;
DispatchMaterialMd dispatchMaterialMd = (DispatchMaterialMd) objectArrayList.get(position);
dispatchViewHolder.txtInvoiceNo.setText(dispatchMaterialMd.getInvoiceNo());
dispatchViewHolder.txtVehicleNo.setText(dispatchMaterialMd.getVehicleNo());
dispatchViewHolder.txtPartyName.setText(dispatchMaterialMd.getPartyName());
dispatchViewHolder.txtNoOfBags.setText(dispatchMaterialMd.getNoOfBags());
dispatchViewHolder.txtMaterialType.setText(dispatchMaterialMd.getMaterialType());
dispatchViewHolder.txtWeight.setText(dispatchMaterialMd.getWeight());
} catch (Exception e) {
e.printStackTrace();
}
}
#Override
public int getItemCount() {
return dispatchMaterialMds.size();
}
}
I have data in my objectArrayList list. I can print it in console, but nothing is coming in recyclerview. In recyclerview, it is only displaying empty Textview.
Following way, implement recyclerview,
layoutManager = new LinearLayoutManager(this);
dispatchMaterialAdapter =
new DispatchMaterialAdapter(itemClickListener, dispatchMaterialMds);
recycleListDetail.setLayoutManager(layoutManager);
recycleListDetail.setAdapter(dispatchMaterialAdapter);
dispatchMaterialMds is an arraylist of model class which is filled from the database's data.
Can you please help me, why my data is not getting displayed ?
You made BaseViewHolder that did the inflation then redone it in the DispatchViewHolder not sure if this will solve the problem but there was repeated code.
private class DispatchViewHolder extends BaseViewHolder {
AppCompatTextView txtInvoiceNo;
AppCompatTextView txtVehicleNo;
AppCompatTextView txtPartyName;
AppCompatTextView txtNoOfBags;
AppCompatTextView txtMaterialType;
AppCompatTextView txtWeight;
DispatchViewHolder(View itemView, ItemClickListener itemClickListener) {
super(itemView, itemClickListener);
}
}
private class DispatchMaterialAdapter extends BaseRecyclerViewAdapter {
ItemClickListener itemClickListener;
List<? extends Object> objectArrayList;
DispatchMaterialAdapter(ItemClickListener itemClickListener, List<? extends Object> objectArrayList) {
super(R.layout.dispatch_material_row, itemClickListener, objectArrayList);
this.itemClickListener = itemClickListener;
this.objectArrayList = objectArrayList;
}
#Override
public DispatchViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
View itemView = LayoutInflater.from(parent.getContext()).inflate(R.layout.dispatch_material_row, parent, false);
return new DispatchViewHolder(itemView, itemClickListener);
}
#Override
public void onBindViewHolder(#NonNull BaseViewHolder holder, int position) {
try {
DispatchViewHolder dispatchViewHolder = (DispatchViewHolder) holder;
DispatchMaterialMd dispatchMaterialMd = (DispatchMaterialMd) objectArrayList.get(position);
dispatchViewHolder.txtInvoiceNo.setText(dispatchMaterialMd.getInvoiceNo());
dispatchViewHolder.txtVehicleNo.setText(dispatchMaterialMd.getVehicleNo());
dispatchViewHolder.txtPartyName.setText(dispatchMaterialMd.getPartyName());
dispatchViewHolder.txtNoOfBags.setText(dispatchMaterialMd.getNoOfBags());
dispatchViewHolder.txtMaterialType.setText(dispatchMaterialMd.getMaterialType());
dispatchViewHolder.txtWeight.setText(dispatchMaterialMd.getWeight());
} catch (Exception e) {
e.printStackTrace();
}
}
}
In the DispatchViewHolder:
AppCompatTextView txtInvoiceNo;
AppCompatTextView txtVehicleNo;
AppCompatTextView txtPartyName;
AppCompatTextView txtNoOfBags;
AppCompatTextView txtMaterialType;
AppCompatTextView txtWeight;
DispatchViewHolder(View itemView, ItemClickListener itemClickListener) {
super(itemView, itemClickListener);
//Initiliazation of new view?
}
make change in set adapter code like below.
recycleListDetail.setLayoutManager(new LinearLayoutManager(this));
dispatchMaterialAdapter =
new DispatchMaterialAdapter(itemClickListener, dispatchMaterialMds);
recycleListDetail.setLayoutManager(layoutManager);
recycleListDetail.setAdapter(dispatchMaterialAdapter);
dispatchMaterialAdapter.notifyDataSetChanged();
and also check your adapter in bind view get the arraylist value and check arraylist type in getting and passing to adapter that same or not.
above discussion i am make demo for recycler view and is wroking using simple string array..
Adapter class.
public class RecyclerViewAdpater extends RecyclerView.Adapter {
List mStringList=new ArrayList<>();// hear you can pass any pojo class object.
Context mContext;
public RecyclerViewAdpater(List<String> mStringList, Context mContext) {
this.mStringList = mStringList;
this.mContext = mContext;
}
#Override
public ItemViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view= LayoutInflater.from(parent.getContext()).inflate(R.layout.activity_main,parent,false);
return new ItemViewHolder(view);
}
#Override
public void onBindViewHolder(ItemViewHolder holder, int position) {
String data=mStringList.get(position); // if you pass object of class then create that class object.
holder.textView.setText(data);
}
#Override
public int getItemCount() {
return mStringList.size();
}
public class ItemViewHolder extends RecyclerView.ViewHolder {
TextView textView;
public ItemViewHolder(View itemView) {
super(itemView);
textView=itemView.findViewById(R.id.timeData);
}
}
}
then implement activity like below code put in onCreate view method ...
recyclerViewAdpater=new RecyclerViewAdpater(mStingList,this);
mRvData.setLayoutManager(new LinearLayoutManager(this));
mRvData.setAdapter(recyclerViewAdpater);
chatAdapter.notifyDataSetChanged();
Please try this
#Override
public int getItemCount() {
return objectArrayList.size();
}
This question already has answers here:
butterknife #OnClick inside ViewHolder in CustomAdapter
(4 answers)
Closed 5 years ago.
What would be the proper way of adding Butter Knife's #OnClick to the view in this method?
private List<Foo> foos;
private RecyclerView recyclerView;
private MapActivity mapView;
#Override
public RecyclerView.ViewHolder onCreateViewHolder(final ViewGroup parent, final int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_foo, parent,
false);
view.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
int selectedPosition = recyclerView.getChildAdapterPosition(v);
mapView.onFooSelected(foos.get(selectedPosition));
}
});
return new FooInfoViewHolder(view);
}
The way this question is different is that I want to bind #OnClick to the FooInfoViewHolder itself.
Use ViewHolder class and inside of that class we can handle clicks for butter knife.
Below is the just example for understanding.
public class YourAdapter extends RecyclerView.Adapter<YourAdapter.ViewHolder> {
private List<Data> list;
public YourAdapter(List<Data> list) {
this.list = list;
}
#Override
public YourAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
LayoutInflater inflater = LayoutInflater.from(parent.getContext());
return new ViewHolder(inflater.inflate(R.layout.list_item_layout, parent, false));
}
#Override
public void onBindViewHolder(YourAdapter.ViewHolder holder, int position) {
Data item = list.get(position);
holder.tvTitle1.setText(item.getTitle1());
}
#Override
public int getItemCount() {
int count = 0;
if (null != list) {
count = list.size();
}
return count;
}
public class ViewHolder extends RecyclerView.ViewHolder {
#BindView(R.id.tvVRValue1)
TextView tvTitle1;
public ViewHolder(View itemView) {
super(itemView);
ButterKnife.bind(this, itemView);
}
#OnClick(R.id.tvVRValue1)
public void onClickYourFunction(){
//Here your onclick method goes
}
}
}
Hope it will help you. Cheers !!!
I figured out what was wrong. Actually I think it's a slightly different answer than the possible duplicates.
The key was to implement the #OnClick on the ViewHolder ITSELF (not, like in the other question, on children of the view):
private List<Foo> foos;
private RecyclerView recyclerView;
private MapActivity mapView;
#Override
public RecyclerView.ViewHolder onCreateViewHolder(final ViewGroup parent, final int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_foo, parent,
false);
return new FooInfoViewHolder(view);
}
And then the FooInfoViewHolder:
public class FooInfoViewHolder extends RecyclerView.ViewHolder {
#OnClick
public void onClick(View v) {
int selectedPosition = recyclerView.getChildAdapterPosition(v);
mapView.onFooSelected(foos.get(selectedPosition));
}
public FooInfoViewHolder(View itemView) {
super(itemView);
ButterKnife.bind(this, itemView);
}
}