'How to call setChecked for RadioButton of specific position in RecyclerView (Adapter) java?
I hope you are fine.
i need to use setChecked for RadioButton in RecyclerView Adapter, from another class, i could not do that. i hope you help me.
this is my Adapter (in this adapter all is working fine I just need to set Checked the radio button of specific position from another class, how can i do that ?):
public class Adapter extends RecyclerView.Adapter<Adapter.ViewHolder> {
public int mSelectedItem = -1;
public ArrayList<HashMap<String, String>> mItems;
private Context mContext;
public static ClickListener clickListener;
public Adapter(Context context, ArrayList<HashMap<String, String>> items) {
mContext = context;
mItems = items;
}
@Override
public void onBindViewHolder(Adapter.ViewHolder viewHolder, final int i) {
View v = viewHolder.itemView;
RadioButton radiobutton1 = v.findViewById(R.id.radiobutton1);
radiobutton1.setChecked(i == mSelectedItem);
RecyclerView.LayoutParams _lp = new RecyclerView.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
v.setLayoutParams(_lp);
}
@Override
public int getItemCount() {
return mItems.size();
}
@Override
public ViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
LayoutInflater inflater = LayoutInflater.from(mContext);
final View view = inflater.inflate(R.layout.recycler_adapter, viewGroup, false);
return new ViewHolder(view);
}
class ViewHolder extends RecyclerView.ViewHolder {
public RadioButton mRadio;
public TextView mText;
public ViewHolder(final View inflate) {
super(inflate);
mText = (TextView) inflate.findViewById(R.id.title);
mRadio = (RadioButton) inflate.findViewById(R.id.radiobutton1);
View.OnClickListener clickListener = new View.OnClickListener() {
@Override
public void onClick(View v) {
Adapter.clickListener.onItemClick(getAdapterPosition(), v);
mSelectedItem = getAdapterPosition();
notifyDataSetChanged();
}
};
itemView.setOnClickListener(clickListener);
mRadio.setOnClickListener(clickListener);
}
}
public void setOnItemClickListener(Adapter.ClickListener clickListener) {
Adapter.clickListener = clickListener;
}
public interface ClickListener {
void onItemClick(int position, View v);
}
}
Solution 1:[1]
Here is how this is done using Java Swing (as requested by the OP)
public class RadioButtonDemo {
JPanel radioPanel = null;
public RadioButtonDemo() {
RadioButtonListener listener = new RadioButtonListener();
JRadioButton btnOne = new JRadioButton("One");
JRadioButton btnTwo = new JRadioButton("Two");
JRadioButton btnThree = new JRadioButton("Three");
btnOne.addActionListener(listener);
btnTwo.addActionListener(listener);
btnThree.addActionListener(listener);
btnOne.setSelected(true);
ButtonGroup btnGp = new ButtonGroup();
btnGp.add(btnOne);
btnGp.add(btnTwo);
btnGp.add(btnThree);
radioPanel = new JPanel();
radioPanel.add(btnOne);
radioPanel.add(btnTwo);
radioPanel.add(btnThree);
}
/**
* @param args
*/
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
createAndShowGui();
}
});
}
private static void createAndShowGui() {
RadioButtonDemo demo = new RadioButtonDemo();
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setContentPane(demo.radioPanel);
frame.pack();
frame.setVisible(true);
}
private static class RadioButtonListener implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
JRadioButton btn = (JRadioButton)e.getSource();
System.out.println("Selected button: " + btn.getText());
}
}
}
As you can see, I don't need to explicitly select or deselect buttons in a radio button group.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | hfontanez |

