Reputation: 451
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();
}
});
}
}
Upvotes: 0
Views: 670
Reputation: 36
@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();
}
}
Upvotes: 1
Reputation: 1619
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);
}
}
Upvotes: 0