How to get value from position adapter RecyclerView - java

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

Related

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 change text of perticular TextView in RecyclerView

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

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

update recycleview row from item onclick

I have a recycleview with 3 texts, is there a way to update specific item onclick that position, (i have quantity of each item, and i want to rise the quantity of that item onclick that position, 0,1,2,3...)
here is my receycleview:
public class RecyclerAdapter extends RecyclerView.Adapter<RecyclerAdapter.RecyclerViewHolder> {
String [] Item_names,Price_items,Item_quantity;
public RecyclerAdapter(String [] Item_names,String [] Price_items,String [] Item_quantity)
{
this.Item_names=Item_names;
this.Price_items=Price_items;
this.Item_quantity=Item_quantity;
}
#Override
public RecyclerViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.row_layout,parent,false);
RecyclerViewHolder recyclerViewHolder=new RecyclerViewHolder(view);
return recyclerViewHolder;
}
#Override
public void onBindViewHolder(final RecyclerViewHolder holder, final int position) {
holder.ItemNameRecycler.setText(Item_names[position]);
holder.ItemPriceRecycler.setText(Price_items[position]);
holder.ItemQuantityRecycler.setText(Item_quantity[position]);
}
#Override
public int getItemCount() {
return Item_names.length;
}
public static class RecyclerViewHolder extends RecyclerView.ViewHolder{
TextView ItemPriceRecycler,ItemNameRecycler,ItemQuantityRecycler;
public RecyclerViewHolder(View view)
{
super(view);
ItemNameRecycler=(TextView)view.findViewById(R.id.RowItemName);
ItemPriceRecycler=(TextView)view.findViewById(R.id.RowItemPrice);
ItemQuantityRecycler=(TextView)view.findViewById(R.id.RowItemQuantity);
ItemQuantityRecycler.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
}
});
}
}
}
You can use getAdapterPosition
From Documentation:
Returns the Adapter position of the item represented by this
ViewHolder.
in your code:
public static class RecyclerViewHolder extends RecyclerView.ViewHolder{
TextView ItemPriceRecycler,ItemNameRecycler,ItemQuantityRecycler;
public RecyclerViewHolder(View view)
{
super(view);
ItemNameRecycler=(TextView)view.findViewById(R.id.RowItemName);
ItemPriceRecycler=(TextView)view.findViewById(R.id.RowItemPrice);
ItemQuantityRecycler=(TextView)view.findViewById(R.id.RowItemQuantity);
ItemQuantityRecycler.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// get position
int pos = getAdapterPosition(); //get position here
}
});
}
EDIT
for updating the recylerview Item Textview in onClick()
public static class RecyclerViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
TextView ItemPriceRecycler, ItemNameRecycler, ItemQuantityRecycler;
public RecyclerViewHolder(final View itemView) {
super(itemView);
ItemNameRecycler = (TextView) itemView.findViewById(R.id.RowItemName);
ItemPriceRecycler = (TextView) itemView.findViewById(R.id.RowItemPrice);
ItemQuantityRecycler = (TextView) itemView.findViewById(R.id.RowItemQuantity);
ItemQuantityRecycler.setOnClickListener(this);
}
// onClick Listener for view
#Override
public void onClick(View v) {
if (v.getId() == ItemQuantityRecycler.getId()) {
int pos = getAdapterPosition();
int value = pos + 1;
Toast.makeText(v.getContext(), "ITEM PRESSED = " + String.valueOf(getAdapterPosition()), Toast.LENGTH_SHORT).show();
ItemQuantityRecycler.setText(String.valueOf(value));
} else {
}
}
}

How to add onClick event on Android recycler view items?

Android Recycler View OnClick event is not working in the code. I can see the Recycler view grid but when I am clicking on one item nothing is happening.
Here is my code:
public class StaggeredGridAdapter extends RecyclerView.Adapter<StaggeredGridAdapter.StaggeredGridView> {
private Context context;
private List<Warehouse> warehouses = new ArrayList<Warehouse>();
int size;
public StaggeredGridAdapter(Context context) {
this.context = context;
}
public void addItems(List<Warehouse> response) {
size = response.size();
warehouses = response;
}
#Override
public StaggeredGridView onCreateViewHolder(ViewGroup parent, int viewType) {
View layoutView = LayoutInflater.from(parent.getContext()).inflate(R.layout.grid_item, parent, false);
StaggeredGridView staggeredGridView = new StaggeredGridView(layoutView);
return staggeredGridView;
}
#Override
public void onBindViewHolder(StaggeredGridView holder, int position) {
holder.textView.setText(warehouses.get(position).getFace());
}
#Override
public int getItemCount() {
return size;
}
class StaggeredGridView extends RecyclerView.ViewHolder implements View.OnClickListener {
TextView textView;
public StaggeredGridView(View itemView) {
super(itemView);
textView = (TextView) itemView.findViewById(R.id.img_name);
}
#Override
public void onClick(View v) {
Toast.makeText(context, "clicked", Toast.LENGTH_SHORT).show();
}
}
}
I inherited inner class i.e. StaggeredGridView with Views.OnClickListner and debug point is not reaching to StaggeredGridView.OnClick method. There is no exception in ADB logs.
What I am doing wrong here?
You need to do this simple thing and it will start working with you
itemView.setOnClickListener(this)
//add this
public StaggeredGridView(View itemView) {
super(itemView);
textView = (TextView) itemView.findViewById(R.id.img_name);
staggeredGridView.setOnClickListener(this)
}

Categories