onbindViewHolder is not accepting view holder variables - java

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

Related

Cannot recognize a method I created in RecyclerView.Adapter

I created my own class called RecyclerviewAdapter.java that extends RecyclerView.Adapter<>, and I created a method in that class
public void setItemCount(int i){
size=i;
}
My RecyclerView works, and I want user to be able to set how many items to be displayed in RecyclerView. In MainActivity, when I call adapter.setItemCount(2), it says Cannot resolve method 'setItemCount' in 'Adapter', also Method 'setItemCount(int)' is never used. I don't know how to solve it.Here are my files.MainActivity.java
RecyclerView recycler;
RecyclerView.Adapter adapter;
RecyclerView.LayoutManager manager;
ArrayList<RecyclerviewItem> list=new ArrayList<>();
list.add(new RecyclerviewItem(R.drawable.hoversfw,"Title","Description"));
manager=new LinearLayoutManager(this);
adapter=new RecyclerviewAdapter(list);
recycler=findViewById(R.id.recycler);
recycler.setHasFixedSize(true);
recycler.setLayoutManager(manager);
recycler.setAdapter(adapter);
if(!count.getText().toString().equals("")) {
int c= Integer.parseInt(count.getText().toString());
adapter.setItemCount(c);
}
RecyclerviewAdapter.java
public class RecyclerviewAdapter extends RecyclerView.Adapter<RecyclerviewAdapter.RecyclerviewHolder> {
private ArrayList<RecyclerviewItem> list;
private int size;
#NonNull
#Override
public RecyclerviewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
View v= LayoutInflater.from(parent.getContext()).inflate(R.layout.recyclerview_item,parent,false);
RecyclerviewHolder holder=new RecyclerviewHolder(v);
return holder;
}
public RecyclerviewAdapter(ArrayList<RecyclerviewItem>mlist){
list=mlist;
size=1;
}
public void setItemCount(int i){
size=i;
}
#Override
public void onBindViewHolder(#NonNull RecyclerviewHolder holder, int position) {
RecyclerviewItem item=list.get(position);
holder.image.setImageResource(item.getImageResource());
holder.title.setText(item.getTitle());
holder.description.setText(item.getDescription());
}
#Override
public int getItemCount() {
return size;
}
public static class RecyclerviewHolder extends RecyclerView.ViewHolder{
public ImageView image;
public TextView title;
public TextView description;
public RecyclerviewHolder(#NonNull View itemView) {
super(itemView);
image=itemView.findViewById(R.id.img);
title=itemView.findViewById(R.id.title);
description=itemView.findViewById(R.id.description);
}
}
}
RecyclerviewItem.java
public class RecyclerviewItem {
private int imageResource;
private String title;
private String description;
public RecyclerviewItem(int mimageResource, String mtitle, String mdescription){
imageResource=mimageResource;
title=mtitle;
description=mdescription;
}
public int getImageResource(){
return imageResource;
}
public String getTitle(){
return title;
}
public String getDescription(){
return description;
}
}
You are declaring RecyclerView Adapter with RecyclerView.Adapter although it should be RecyclerviewAdapter
RecyclerView.Adapter is an SDK class, but RecyclerviewAdapter is yours.

Unable to declare variable

Why I'm unable to declare variable in MessageViewHolder class? I want to findviewById for my xml file. BTW I'm running latest version of Android Studio in latest 64 bit Ubuntu. But, surely there is no problem with version. I tried with some other variable. Same problem!
#NonNull
#Override
public MessageViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(
R.layout.message_single_layout, parent, false);
return new MessageViewHolder(view);
}
#Override
public void onBindViewHolder(#NonNull MessageViewHolder holder, int position) {
Messages messages = messagesList.get(position);
holder.chatText.setText(messages.getMessage());
}
#Override
public int getItemCount() {
return messagesList.size();
}
public class MessageViewHolder extends RecyclerView.ViewHolder {
public TextView chatText;
public CircleImageView chatImage;
public MessageViewHolder(View itemView) {
super(itemView);
}
chatText =
chatImage
}
}
You need to do findViewById inside MessageViewHolder() Using itemView
Try this
public class MessageViewHolder extends RecyclerView.ViewHolder {
public TextView chatText;
public CircleImageView chatImage;
public MessageViewHolder(View itemView) {
super(itemView);
chatText =itemView.findViewById(R.id.chatText);
chatImage=itemView.findViewById(R.id.chatImage);
}
}
In view holder you take id's from view
So it is -
public MessageViewHolder(View itemView) {
super(itemView);
chatText = itemView.findViewById(R.id.*);
chatImage = itemView.findViewById(R.id.*);
}

method does not override method from its superclass : onBindViewHolder

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 ?

Recyclerview is not displaying data when using generic adapter & 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();
}

ButterKnife onClick listener inside ViewHolder method [duplicate]

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);
}
}

Categories