RadioButton implementation of single selection in ListView

Source: Internet
Author: User

 Public classFragmentHack5extendsFragment {view view;    ListView lvcountries;    Button btnshow;    Countrylistadapter adapter; List<String>list; @Override Public voidOnattach (activity activity) {Super. Onattach (activity); List=NewArraylist<string>(); List.add (China); List.add (Russia); List.add (United States); List.add (Germany); List.add (British); List.add (Spain); List.add (France); List.add (Brazil); List.add (India); } @Override PublicView Oncreateview (layoutinflater inflater, ViewGroup container, Bundle savedinstancestate) {View= Inflater.inflate (R.layout.fragment_hack5,container,false); Btnshow=(Button) View.findviewbyid (; Lvcountries=(ListView) View.findviewbyid (; Adapter=NewCountrylistadapter (Getactivity (), r.layout.list_country_item,list);        Lvcountries.setadapter (adapter); Btnshow.setonclicklistener (NewView.onclicklistener () {@Override Public voidOnClick (view view) {Toast.maketext (Getactivity (), Adapter.getchoicecountry (), Toast.length_short). Show (            );;        }        }); returnview; }}

 Public classCountrylistadapterextendsArrayadapter<string>{    intresourceId; intChoiceid =-1;  PublicCountrylistadapter (Context context,intResourceId, list<string>objects) {        Super(context,resourceid,objects);  This. ResourceId =resourceId; } @Override PublicView GetView (Final intposition, View Convertview, ViewGroup parent) {String Country=GetItem (position); FinalViewholder Holder; if(convertview==NULL) {Holder=NewViewholder (); Convertview= Layoutinflater.from (GetContext ()). Inflate (ResourceId,NULL); Holder.rbcountry=(RadioButton) Convertview.findviewbyid (;        Convertview.settag (holder); }Else{Holder=(Viewholder) Convertview.gettag ();        } holder.rbCountry.setText (country); if(choiceid==position) {holder.rbCountry.setChecked (true); }Else{holder.rbCountry.setChecked (false); } holder.rbCountry.setOnClickListener (NewView.onclicklistener () {@Override Public voidOnClick (view view) {if(holder.rbCountry.isChecked ()) {Choiceid= position;//remember the currently selected subscriptCountrylistadapter. This. notifydatasetchanged ();        }            }        }); returnConvertview; }    Static classviewholder{ PublicRadioButton Rbcountry; }     PublicString getchoicecountry () {returnGetItem (Choiceid); }}

3: Run Results

RadioButton implementation of single selection in ListView

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.