Only select only one radio button from listview
Asked Answered
T

5

6

I have searched the question over and over, there are many questions I have found in stackoverflow but I did not come up with a solution. Here is my problem. I have a list of item in a linearlayout like this: enter image description here

When the screen comes first, the last radion button is selected automatically. But if I select another radio button, the last radio button is already in selected state. I want to select only one radion button but at the same time when the screen comes first the last radio button will be auto selected and other automatically will not be selected. How can I achieve this. My adapter class is :

public class AdapterPaymentMethod extends ArrayAdapter<PaymentData> {
    private Context context;
    private boolean userSelected = false;
    ViewHolder holder;

    public AdapterPaymentMethod(Context context, int resource,
            List<PaymentData> items) {
        super(context, resource, items);
        this.context = context;
    }

    /* private view holder class */
    private class ViewHolder {
        RadioButton radioBtn;
        Button btPaymentMethod;
        // ImageView ivLine;
    }

    public View getView(final int position, View convertView, ViewGroup parent) {
        holder = null;
        PaymentData rowItem = getItem(position);
        LayoutInflater mInflater = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        if (convertView == null) {
            convertView = mInflater.inflate(
                    com.android.paypal.homeactivity.R.layout.list_item, null);
            holder = new ViewHolder();
            holder.radioBtn = (RadioButton) convertView
                    .findViewById(com.android.paypal.homeactivity.R.id.rdb_payment_method);
            holder.btPaymentMethod = (Button) convertView
                    .findViewById(com.android.paypal.homeactivity.R.id.bt_item_event);
            convertView.setTag(holder);
        } else
            holder = (ViewHolder) convertView.getTag();

        if(position==getCount()-1 && userSelected==false){
            holder.radioBtn.setChecked(true);
        Log.d("if position", ""+position);
        }
        else{
            holder.radioBtn.setChecked(false);
            Log.d("else position", ""+position);
        }

        holder.radioBtn.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                userSelected=true;
                if(getCount()-1==position&&userSelected==true){         
                }
                else if(getCount()-2==position&&userSelected==true)
                {
                    holder.radioBtn.setChecked(true);
                }
            }
        });



        holder.radioBtn.setText(rowItem.getRdbText());
        holder.btPaymentMethod.setText(rowItem.getBtText());
        return convertView;
    }
}
Tyler answered 18/4, 2013 at 21:1 Comment(1)
i m also stuck on same issues can u help me , but in check boxFrancefrancene
H
13

Keep the currently checked RadioButton in a variable in your Activity. In the OnClickListener of your RadioButtons do this:

// checks if we already have a checked radiobutton. If we don't, we can assume that 
// the user clicked the current one to check it, so we can remember it.
if(mCurrentlyCheckedRB == null) 
    mCurrentlyCheckedRB = (RadioButton) v;
    mCurrentlyCheckedRB.setChecked(true);
}

// If the user clicks on a RadioButton that we've already stored as being checked, we 
// don't want to do anything.       
if(mCurrentlyCheckedRB == v)
    return;

// Otherwise, uncheck the currently checked RadioButton, check the newly checked 
// RadioButton, and store it for future reference.
mCurrentlyCheckedRB.setChecked(false);
((RadioButton)v).setChecked(true);
mCurrentlyCheckedRB = (RadioButton)v;   
Halvorsen answered 18/4, 2013 at 21:19 Comment(7)
You solution works fine. Can you explain your code. I did not understand your code. I am newbie. Thanks.Tyler
I've added some comments to explain it. If this works for you, please set the answer as accepted!Halvorsen
@catherine can u explain ,i am stuck in same issue but in check box how can i use this conditionFrancefrancene
What do you need explained that isn't explained above?Halvorsen
Pls how does one get the currently checked RadioButton in a variable. Have being trying since morning with no success. I know this is an old post but for reference sakes and for those that will get stuck like me.Hagi
@Hagi I don't recall exactly, but you should be able to get it in the button's on click listener.Halvorsen
It is a great effort. However, if you have more items than the screen size, the new items's radio button is auto-selectedSansculotte
B
9

Here's my code:

ListAdapter.java

public class ListAdapter extends BaseAdapter {

    private Context context;
    private String[] version;
    private LayoutInflater inflater;
    private RadioButton selected = null;

    public ListAdapter(Context context, String[] version) {
        this.context = context;
        this.version = version;
        inflater = LayoutInflater.from(context);
    }

    @Override
    public int getCount() {
        return version.length;
    }

    @Override
    public Object getItem(int position) {
        return version;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View view, ViewGroup viewGroup) {

        final Holder holder;
        if (view == null) {
            holder = new Holder();
            view = inflater.inflate(R.layout.row_list, null);

            holder.radioButton = (RadioButton) view.findViewById(R.id.radio1);
            holder.textViewVersion = (TextView) view.findViewById(R.id.textView1);
            holder.btnMore = (Button) view.findViewById(R.id.button1);

            view.setTag(holder);
        } else {
            holder = (Holder) view.getTag();
        }

        holder.textViewVersion.setText(version[position]);

        //by default last radio button selected
        if (position == getCount() - 1) {
            if (selected == null) {
                holder.radioButton.setChecked(true);
                selected = holder.radioButton;
            }
        }

        holder.radioButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                if (selected != null) {
                    selected.setChecked(false);
                }
                holder.radioButton.setChecked(true);
                selected = holder.radioButton;
            }
        });       
    }

    private class Holder {
        private RadioButton radioButton;
        private TextView textViewVersion;
        private Button btnMore;
    }
}

Hope this helps you :)

Burke answered 1/12, 2016 at 10:33 Comment(0)
U
1

Try this hack.

In layout make this changes.

<RadioButton
  android:id="@+id/radio"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:layout_gravity="center_vertical"
  **android:clickable="false"
  android:focusable="false"**
/>

In your Adapter just add these lines:

 private int selectedPosition = -1;

 viewHolder.choiceButton = (RadioButton)convertView.findViewById(R.id.radio);

 if(selectedPosition == -1) {
    viewHolder.choiceButton.setChecked(position==getCount()-1);
 }
 else{
    viewHolder.choiceButton.setChecked(selectedPosition == position);
 }

In getView() assign selectedPosition:

viewHolder.clickLayout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                selectedPosition = (int) view.getTag();
                notifyDataSetChanged();
            }
        });
Unwitnessed answered 1/3, 2017 at 6:35 Comment(2)
This code does not work. Multiple radio buttons get selected.Heelpost
Have you assigned selectedPosition on click layout using getTag() ?Unwitnessed
G
0

In your Adapter class, take a global variable and initialize it as shown below.

private int lastSelectedPosition = -1;

public class ViewHolder extends RecyclerView.ViewHolder {
    public RadioButton check;
    public ViewHolder(View itemView) {
        super(itemView);
        check=itemView.findViewById(R.id.check_box4);//Your radio button id
        check.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                lastSelectedPosition = getAdapterPosition();
                notifyDataSetChanged();
            }
        });
    }

public void onBindViewHolder(ViewHolder holder, int position) {
    holder.check.setChecked(lastSelectedPosition == position);
}
Gerthagerti answered 4/2, 2019 at 11:21 Comment(0)
B
0

This looks simple version to me

global variable in ArrayAdapter

private RadioButton selected = null;

in side getView

public View getView(final int position, View convertView, ViewGroup parent) {

    LayoutInflater inflater = (LayoutInflater) context
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    View rowView = inflater.inflate(R.layout.welcom_product_choose_list, parent, false);
   

    RadioButton chooseproduct_redio = rowView.findViewById(R.id.chooseproduct_redio);

    if (position == getCount() - 1) {
        if (selected == null) {
            chooseproduct_redio.setChecked(true);
            selected = chooseproduct_redio;
        }
    }

    chooseproduct_redio.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            if (selected != null) {
                selected.setChecked(false);
            }
            chooseproduct_redio.setChecked(true);
            selected = chooseproduct_redio;
        }
    });
    return rowView;
}
Benzaldehyde answered 9/6, 2022 at 16:46 Comment(1)
Formatting is wild and hard to understand what youre sayingKnutson

© 2022 - 2024 — McMap. All rights reserved.