How to change text of perticular TextView in RecyclerView - java

I have a text and Button in RecyclerView. Am trying to change the text of TextView onClick. I tried in a way by setting text inside OnBindViewHolder but it didn't work that way and wherever I click the button the text changes of last item of recyclerView instead of perticular position I wanted.
Here is my Adapter class-
TextView textView;
Button button;
#NonNull
#Override
public viewHl onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
View view= LayoutInflater.from(parent.getContext()).inflate(R.layout.un, parent, false);
return new viewHl(view);
}
#Override
public void onBindViewHolder(#NonNull viewHl holder, int position) {
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
holder.geTextView().setText("hellow");
}
});
}
#Override
public int getItemCount() {
return 4;
}
public class viewHl extends RecyclerView.ViewHolder {
public viewHl(#NonNull View itemView) {
super(itemView);
textView=itemView.findViewById(R.id.textView);
button=itemView.findViewById(R.id.button);
}
public TextView geTextView()
{
return textView;
}
}
}
[![enter image description here][1]][1]
[1]: https://i.stack.imgur.com/hlU1M.png

Problem is that you are holding a single reference to Button and TextView in the adapter. Instead you need to keep it in view holder, so each item will keep their button and text.
public class viewHl extends RecyclerView.ViewHolder {
private TextView textView;
private Button button;
public viewHl(#NonNull View itemView) {
super(itemView);
textView=itemView.findViewById(R.id.textView);
button=itemView.findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
textView.setText("hellow");
}
});
}
}

Related

Moving a recyclerviewer to different tab

I have 3 fragment(Todo, Ongoing , Finished)
A recyclerviewer populates the first fragment (ToDO). I want to move the recyclerview to the next tab (ongoing). Could not find appropriate documentation on this. Below is a screenshot of the UI
Code for adapter
public class recyclerKanbanAdapter extends RecyclerView.Adapter<recyclerKanbanAdapter.MyViewHolder> {
ArrayList<kanbanItems> kanlist;
Context context;
public recyclerKanbanAdapter(Context context,ArrayList<kanbanItems>kanbanList)
{
this.context=context;
this.kanlist=kanbanList;
}
public recyclerKanbanAdapter.MyViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
View v = LayoutInflater.from(context).inflate(R.layout.fragment_itemkanban,parent,false);
return new recyclerKanbanAdapter.MyViewHolder(v);
}
#Override
public void onBindViewHolder(#NonNull recyclerKanbanAdapter.MyViewHolder holder, int position) {
kanbanItems kanban = kanlist.get(position);
holder.contractorName.setText(kanban.getContractorName());
holder.Duration.setText(kanban.getDuration());
holder.taskName.setText(kanban.getTaskName());
holder.btn_Delete.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
kanlist.remove(holder.getAdapterPosition());
notifyItemRemoved(holder.getAdapterPosition());
notifyItemRangeChanged(holder.getAdapterPosition(), kanlist.size());
}
});
holder.btn_Move.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//move to next tab
}
});
}
#Override
public int getItemCount() {
return kanlist.size();
}
public static class MyViewHolder extends RecyclerView.ViewHolder{
EditText taskName,Duration,contractorName;
ImageButton btn_Delete;
ImageButton btn_Move;
public MyViewHolder(#NonNull View itemView) {
super(itemView);
btn_Move=itemView.findViewById(R.id.btn_move);
btn_Delete=itemView.findViewById(R.id.btn_delete);
taskName= itemView.findViewById(R.id.txtTaskName);
Duration=itemView.findViewById(R.id.txtDuration);
contractorName=itemView.findViewById(R.id.txtContractor);
}
}
}

Move a list to the next tab on click a button using tab layout

I have a recycler view on one tab. I am trying to move the recyclerview to the next tab when the user clicks on the next button as shown below. Im having to add the code move on the adapter class. Hence, below is the code for the adapter class. Any idea on how to move the recyclerview to the next tabs (ongoing + finished)
code
public class recyclerKanbanAdapter extends RecyclerView.Adapter<recyclerKanbanAdapter.MyViewHolder> {
ArrayList<kanbanItems> kanlist;
Context context;
public recyclerKanbanAdapter(Context context,ArrayList<kanbanItems>kanbanList)
{
this.context=context;
this.kanlist=kanbanList;
}
public recyclerKanbanAdapter.MyViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
View v = LayoutInflater.from(context).inflate(R.layout.fragment_itemkanban,parent,false);
return new recyclerKanbanAdapter.MyViewHolder(v);
}
#Override
public void onBindViewHolder(#NonNull recyclerKanbanAdapter.MyViewHolder holder, int position) {
kanbanItems kanban = kanlist.get(position);
holder.contractorName.setText(kanban.getContractorName());
holder.Duration.setText(kanban.getDuration());
holder.taskName.setText(kanban.getTaskName());
holder.btn_Delete.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
kanlist.remove(holder.getAdapterPosition());
notifyItemRemoved(holder.getAdapterPosition());
notifyItemRangeChanged(holder.getAdapterPosition(), kanlist.size());
}
});
holder.btn_Move.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//struggling to implemene the move button
}
});
}
#Override
public int getItemCount() {
return kanlist.size();
}
public static class MyViewHolder extends RecyclerView.ViewHolder{
EditText taskName,Duration,contractorName;
ImageButton btn_Delete;
ImageButton btn_Move;
public MyViewHolder(#NonNull View itemView) {
super(itemView);
btn_Move=itemView.findViewById(R.id.btn_move);
btn_Delete=itemView.findViewById(R.id.btn_delete);
taskName= itemView.findViewById(R.id.txtTaskName);
Duration=itemView.findViewById(R.id.txtDuration);
contractorName=itemView.findViewById(R.id.txtContractor);
}
}
}

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

How to change image of imageView from drawable inside an adapter of RecyclerView after an item has been clicked?

I fetched data from SERVER to populate RECYCLER view
I have an imageview in each recyclerview row
When I click that imageview, I want to load my drawable into the imageview
holder.imageView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Drawable placeholder = holder.imageView.getContext().getResources().getDrawable(R.drawable.app);
holder.imageView.setImageDrawable(placeholder);
}
});
The above is what I tried but it says "holder" needs to be declared final but in reality, I haven't even mentioned it in a way I could write "final holder" or whatever.
My ADAPTER code :
public class ProductsAdapter extends
RecyclerView.Adapter<ProductsAdapter.ProductViewHolder> {
private Context mCtx;
private List<Product> productList;
public ProductsAdapter(Context mCtx, List<Product> productList) {
this.mCtx = mCtx;
this.productList = productList;
}
#Override
public ProductViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
LayoutInflater inflater = LayoutInflater.from(mCtx);
View view = inflater.inflate(R.layout.product_list, null);
return new ProductViewHolder(view);
}
#Override
public void onBindViewHolder(ProductViewHolder holder, int position) {
final Product product = productList.get(position);
//loading the image
Glide.with(mCtx)
.load(product.getImage())
.into(holder.imageView);
holder.textViewTitle.setText(product.getTitle());
holder.textViewShortDesc.setText(product.getShortdesc());
holder.textViewRating.setText(String.valueOf(product.getRating()));
holder.textViewPrice.setText(String.valueOf(product.getPrice()));
holder.imageView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Drawable placeholder = holder.imageView.getContext().getResources().getDrawable(R.drawable.app);
holder.imageView.setImageDrawable(placeholder);
}
});
}
#Override
public int getItemCount() {
return productList.size();
}
class ProductViewHolder extends RecyclerView.ViewHolder {
TextView textViewTitle, textViewShortDesc, textViewRating, textViewPrice;
ImageView imageView;
public ProductViewHolder(View itemView) {
super(itemView);
textViewTitle = itemView.findViewById(R.id.textViewTitle);
textViewShortDesc = itemView.findViewById(R.id.textViewShortDesc);
textViewRating = itemView.findViewById(R.id.textViewRating);
textViewPrice = itemView.findViewById(R.id.textViewPrice);
imageView = itemView.findViewById(R.id.imageView);
}
}
}
You can declare the holder final in the method header
public void onBindViewHolder(final ProductViewHolder holder, int position) {
Your code here
}
There are several solutions:
1) Make your holder to be final doing
#Override
public void onBindViewHolder(final ProductViewHolder holder, int position)
or using additional variable
final Holder finalHolder = holder;
holder.imageView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Drawable placeholder = finalHolder.imageView.getContext().getResources().getDrawable(R.drawable.app);
holder.imageView.setImageDrawable(placeholder);
}
});
2) When you set ClickListener on particular view, you are sure that only that view will trigger action, so you can use that knowledge
holder.imageView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
ImageView imgView = (ImageView) view;
Drawable placeholder = imgView.getContext().getResources().getDrawable(R.drawable.app);
imgView.setImageDrawable(placeholder);
}
});
If you're using View Binding in kotlin then it will work,
holder.binding.imageView.setImageDrawable(ContextCompat.getDrawable(holder.binding.imageView.context,R.drawable.medal_2)
without view binding it will be
holder.imageView.setImageDrawable(ContextCompat.getDrawable(holder.imageView.context,R.drawable.medal_2)

How to set onClickListener on RecyclerView

I have a simple problem that I can't set onClickListener on recyclerview , i don't want to set onClickListener on items of recyclerview , just the recyclerview itself , I tried the ordinary way and it didn't work
( i tried to set an onClickListener on the parent Layout but the strange things it doesn't work because of the LinearLayoutManager that i set on recyclerview :| )
here's my code :
mRecyclerView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Log.d("onClick","Clicked");
}
});
Adapter :
public class RecyclerAdapter extends RecyclerView.Adapter<RecyclerAdapter.ViewHolder> {
private List<Message> mMessage;
public static class ViewHolder extends RecyclerView.ViewHolder{
private TextView txtSender;
private TextView txtMessage;
public ViewHolder(View v) {
super(v);
txtSender = (TextView) v.findViewById(R.id.txtSender);
txtMessage = (TextView) v.findViewById(R.id.txtText);
}
}
#Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View inflatedView = LayoutInflater.from(parent.getContext())
.inflate(R.layout.recyclerview_item_row, parent, false);
return new ViewHolder(inflatedView);
}
public RecyclerAdapter(List<Message> messages) {
mMessage = messages;
}
#Override
public void onBindViewHolder(ViewHolder holder, int position) {
Message msg = mMessage.get(position);
holder.txtSender.setText(msg.getSender());
holder.txtMessage.setText(msg.getmessage());
}
#Override
public int getItemCount() {
return mMessage.size();
}
As per your need, you need to modify R.layout.recyclerview_item_row. Use a android LinearLayout put both of TextView(R.id.txtSender and R.id.txtText) inside that.
Modify your ViewHolder as below:
public static class ViewHolder extends RecyclerView.ViewHolder
{
private LinearLayout lvContainer;
private TextView txtSender;
private TextView txtMessage;
public ViewHolder(View v) {
super(v);
lvContainer = (LinearLayout) v.findViewById(R.id.lv_container); // R.id.lv_container this should be in your R.layout.recyclerview_item_row
txtSender = (TextView) v.findViewById(R.id.txtSender);
txtMessage = (TextView) v.findViewById(R.id.txtText);
}
}
Modify your onBindViewHolder as below
#Override
public void onBindViewHolder(ViewHolder holder, int position) {
Message msg = mMessage.get(position);
holder.txtSender.setText(msg.getSender());
holder.txtMessage.setText(msg.getmessage());
holder.lvContainer.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//Handle your click here
}
});
}

Categories