Unable to declare variable - java

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

Related

How to get value from position adapter RecyclerView

I have 2 data items where the data has different id. When i click radio button then getting id by position.
Example : I have 2 data items, the first id is 57 and the second id is 59. When I click on the first data it must get a value of 57 or when I click on the second data it must get a value of 59.
private String convertAddressID;
private int previousSelected = -1;
private boolean isRadioChecked;
#Override
public void onBindViewHolder(#NonNull final ViewHolder holder, final int position) {
ModelGetAddress adapterAddress = modelGetAddressList.get(position);
convertAddressID = String.valueOf(adapterAddress.getId());
}
public class ViewHolder extends RecyclerView.ViewHolder {
private RadioButton radioButton;
public ViewHolder(#NonNull View itemView) {
super(itemView);
radioButton = itemView.findViewById(R.id.radioButton);
radioButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
previousSelected = getAdapterPosition();
notifyItemRangeChanged(0, modelGetAddressList.size());
Toast.makeText(getContext, convertAddressID, Toast.LENGTH_SHORT).show();
}
});
}
}
#Override
public void onBindViewHolder(#NonNull final ViewHolder holder, final int position) {
ModelGetAddress adapterAddress = modelGetAddressList.get(position);
convertAddressID = String.valueOf(adapterAddress.getId());
}
public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener{
private RadioButton radioButton;
public ViewHolder(#NonNull View itemView) {
super(itemView);
radioButton = itemView.findViewById(R.id.radioButton);
itemView.setOnClickListener(this);
}
#Override
public void onClick(View view) {
int position=getAdapterPosition();
Toast.makeText(conext, ""+modelGetAddressList.get(position).getId(), Toast.LENGTH_SHORT).show();
}
}
You can set the onClickListener in the onBindViewHolder where you always have the position. Call the findViewById in the ViewHolder class.
Try something like this my (untested) code:
#Override
public void onBindViewHolder(#NonNull final ViewHolder holder, final int position) {
((ViewHolder)holder).button.setOnClickListener(...)
}
public class ViewHolder extends RecyclerView.ViewHolder {
Button button;
ViewHolderContent(#NonNull View itemView) {
super(itemView);
button= itemView.findViewById(R.id.button);
}
}

Not able to use ViewHolder right in RecyclerView

After a long time i am trying to use a recylcerview and when i am generating the onBindViewHolder methode, it is using RecyclerView.ViewHolder as a holder. but to use the holder on the Textview mFach, i need to use my ViewHolder, that i created below.
private ArrayList<DatenItem> mDatenList;
#NonNull
#Override
public RecyclerView.ViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.daten_item, parent, false);
return new ViewHolder(v);
}
#Override
public void onBindViewHolder(#NonNull RecyclerView.ViewHolder holder, int position) {
DatenItem datenItem = mDatenList.get(position);
holder.mFach.setText(datenItem.getmFach()); //both not possible because mfach and mDatum doesnt exist (it is red)
holder.mDatum.setText(datenItem.getmDate());
}
#Override
public int getItemCount() {
return mDatenList.size();
}
public static class ViewHolder extends RecyclerView.ViewHolder {
private final TextView mFach;
private final TextView mDatum;
public ViewHolder(#NonNull View itemView) {
super(itemView);
mFach = itemView.findViewById(R.id.fachView);
mDatum = itemView.findViewById(R.id.datumView);
}
}
}```
btw i deleted the unnecessary parts of the code in the code above
The problem is that you use incorrect generic type. You need to change your adapter declaration and corresponding functions to use your ViewHolder instead of base one:
class YourAdapter() : RecyclerView.Adapter<YourAdapter.ViewHolder>() {
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): YourAdapter.ViewHolder
}
override fun onBindViewHolder(holder: MyViewHolder, position: Int) {
}
}
i think because 'mFach' and 'mDatum' are private you can't do that.
also you can use this sample code in onBindViewHolder :
public void onBindViewHolder(#NonNull RecyclerView.ViewHolder holder, int position)
{
DatenItem datenItem = mDatenList.get(position);
holder.fill(datenItem);
}
and change you'r view holder like this :
public static class ViewHolder extends RecyclerView.ViewHolder {
private final TextView mFach;
private final TextView mDatum;
public ViewHolder(#NonNull View itemView)
{
super(itemView);
mFach = itemView.findViewById(R.id.fachView);
mDatum = itemView.findViewById(R.id.datumView);
}
public void fill(DatenItem datenItem)
{
holder.mFach.setText(datenItem.getmFach());
holder.mDatum.setText(datenItem.getmDate());
}}

RecyclerView items not visible

RecyclerView items not visible
I am using a string array name item test and pass it to String array list, I used the same idea before and was working correctly
listItemsTest = new ArrayList<>(Arrays.asList(itemsTest));
recyclerView = findViewById(R.id.RecycleViewTest);
recycleViewAdapterTest = new RecycleViewAdapterTest(this,listItemsTest);
recyclerView.setAdapter(recycleViewAdapterTest);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
My Adapter
public class RecycleViewAdapterTest extends
RecyclerView.Adapter<RecycleViewAdapterTest.MyViewHolder> {
private ArrayList<String> ListTest;
Context context;
public RecycleViewAdapterTest(Context context, ArrayList<String> listTest) {
this.ListTest = listTest;
this.context = context;
}
#NonNull
#Override
public RecycleViewAdapterTest.MyViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.test_list,parent,false);
MyViewHolder holder = new MyViewHolder(view);
return holder;
}
#Override
public void onBindViewHolder(#NonNull RecycleViewAdapterTest.MyViewHolder holder, int position) {
}
#Override
public int getItemCount() {
return ListTest.size();
}
public static class MyViewHolder extends RecyclerView.ViewHolder {
LinearLayout linearLayout;
TextView textView;
public MyViewHolder(#NonNull View itemView) {
super(itemView);
linearLayout = itemView.findViewById(R.id.LLTest);
textView = itemView.findViewById(R.id.textViewTest);
}
}
#Override
public void onBindViewHolder(#NonNull RecycleViewAdapterTest.MyViewHolder holder, int position) {
String value = this.ListTest.get(position);
holder.textView.setText(value);// Try to make the textView public
}
because you need to set list value to viewholder's textview in OnBindViewHolder. :(

onbindViewHolder is not accepting view holder variables

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

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