0

I have lisview it contain radioButton and 6 textView. I want to allow check only one item radio button at a time. Default, first item radio button checked when page open. If i choose any item automatically uncheck previous checked item.. Please anyone help me!

Code below :

AdapterClass:

public class AddressCustomAdapter extends BaseAdapter implements ListAdapter {
    private final Context context;
    private final ArrayList<AddressResult> itemsArrayList;

    public AddressCustomAdapter(Context context, ArrayList<AddressResult> itemsArrayList) {
        this.context = context;
        this.itemsArrayList = itemsArrayList;

    }

    @Override
    public int getCount() {
        return itemsArrayList.size();
    }

    @Override
    public Object getItem(int position) {
        return itemsArrayList.get(position);
    }

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

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        View view = convertView;
        final ViewHolder holder;
        if (convertView == null) {
            LayoutInflater inflater = (LayoutInflater) ((Activity)context).getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            view = inflater.inflate(R.layout.address_list,null);
            holder = new ViewHolder();

            holder.addrTypeTxt = (TextView)view.findViewById(R.id.addr);
            holder.line1Txt = (TextView)view.findViewById(R.id.addrLine1Txt);
            holder.line2Txt = (TextView)view.findViewById(R.id.addrLine2Txt);
            holder.areaTxt = (TextView)view.findViewById(R.id.addrAreaTxt);
            holder.cityTxt = (TextView)view.findViewById(R.id.addrCityTxt);
            holder.pincodeTxt = (TextView)view.findViewById(R.id.addrPincodeTxt);
           holder.radioButton = (RadioButton) view.findViewById(R.id.radioBtn);
            view.setTag(holder);
        }else{
            holder=(ViewHolder)view.getTag();
        }
        if(Utility.isNotNull(itemsArrayList.get(position).type)){
            holder.addrTypeTxt.setText(itemsArrayList.get(position).type);
        }else{
            Utility.textViewDisable(holder.addrTypeTxt);
        }

        if(Utility.isNotNull(itemsArrayList.get(position).line1)){
            holder.line1Txt.setText(itemsArrayList.get(position).line1);
        }else{
            Utility.textViewDisable(holder.line1Txt);
        }

        if(Utility.isNotNull(itemsArrayList.get(position).line2)){
            holder.line2Txt.setText(itemsArrayList.get(position).line2);
        }else{
            Utility.textViewDisable(holder.line2Txt);
        }

        if(Utility.isNotNull(itemsArrayList.get(position).area)){
            holder.areaTxt.setText(itemsArrayList.get(position).area);
        }else{
            Utility.textViewDisable(holder.areaTxt);
        }

        if(Utility.isNotNull(itemsArrayList.get(position).city)){
            holder.cityTxt.setText(itemsArrayList.get(position).city+"-");
        }else{
            Utility.textViewDisable(holder.cityTxt);
        }

        if(Utility.isNotNull(itemsArrayList.get(position).pincode)){
            holder.pincodeTxt.setText(itemsArrayList.get(position).pincode);
        }else{
            Utility.textViewDisable(holder.pincodeTxt);
        }

       return view;
    }

    public static class ViewHolder{
        public TextView addrTypeTxt;
        public TextView line1Txt;
        public TextView line2Txt;
        public TextView areaTxt;
        public TextView cityTxt;
        public TextView pincodeTxt;
        public RadioButton radioButton;
    }
}

Thanks in advance!!!

flavio.donze
  • 7,432
  • 9
  • 58
  • 91
hikoo
  • 517
  • 4
  • 10
  • 20
  • Do you want to check multiple radio buttons at the same time? – kagkar Apr 18 '16 at 13:16
  • Aren't all your radiobuttons part of the same radioGroup? You haven't given your activity.xml but to me it seems you have one radiobutton per List item view and thus each belong to a different viewgroup? Is that the case? – Abhishek Sinha Apr 18 '16 at 13:27
  • In my xml contains only one radioButton.. Its applied for each item – hikoo Apr 18 '16 at 13:54
  • refer this http://stackoverflow.com/questions/8337180/custom-single-choice-listview, you will get proper solution. – Tulsiram Rathod Apr 19 '16 at 12:03

1 Answers1

0

Since your radiobuttons do not belong to one radiogroup, they are not working as radiobutton but just as a selector. It defeats the purpose of radiobutto, so I would suggest you to relook the design once. However, as a workaround to achieve what you want, you would want some way to keep track of currently selected radiobutton and once any other is selected toggle the currently selected button

So define a variable in your activity or fragment called currentradioselected

private RadioButton currentRadioButtonSelected

In your radiobuttonclick listener

public void onRadioButtonClicked(View view) {
   RadioButton current = (RadioButton) view;
   if (current.isChecked() && currentRadioSelected != null ) {
        currentRadioButtonSelected.toggle();
        currentRadioButtonSelected = current; 
   } else if (current.isChecked()) {
        currentRadioButtonSelected = current;
   } else {
        //already selected radiobutton unchecked
         currentRadioButtonSelected = null;
   }
 }

Hope this helps

Abhishek Sinha
  • 435
  • 4
  • 12