The ListView in Android----Custom Radio list with RadioButton

Source: Internet
Author: User

Sometimes we need to make a custom radio list, but will encounter some problems, such as multi-choice, false selection problem, so look for information on the Internet, organize a demo out, paste the code:


<listview        android:id= "@+id/listview1"        android:layout_width= "match_parent"        android:layout_height = "Wrap_content"        android:layout_centerhorizontal= "true"        android:layout_centervertical= "true" >    </ListView>

The contents of each row in the list

<?xml version= "1.0" encoding= "Utf-8"? ><linearlayout xmlns:android= "http://schemas.android.com/apk/res/ Android "Android:layout_width=" Match_parent "android:layout_height=" match_parent "android:orientation=" vertical "        > <relativelayout android:id= "@+id/outpatient_check_hospital" android:layout_width= "Fill_parent" android:layout_height= "Wrap_content" android:layout_marginbottom= "5.0dip" android:layout_marginleft= "12 .599976dip "android:layout_marginright=" 12.599976dip "android:layout_margintop=" 5.0dip "Android:gravi Ty= "center_vertical" android:background= "#AAAAAA" > <linearlayout android:id= "@+id/linear_l Ayout_up "android:layout_width=" fill_parent "android:layout_height=" Wrap_content "Android : layout_margin= "10.0dip" android:gravity= "center" android:orientation= "Horizontal" > & Lt;imageview Android: layout_width= "10dip" android:layout_height= "10dip" android:adjustviewbounds= "false"/>                <textview android:id= "@+id/tv_device_name" android:layout_width= "Wrap_content" android:layout_height= "Wrap_content" android:layout_weight= "2.0" Android:text = "Name" android:textcolor= "#ff323232" android:textsize= "16.0SP" Android:typefac E= "monospace"/> <radiobutton android:id= "@+id/rb_light" Android:layout_wid                Th= "Wrap_content" android:layout_height= "Wrap_content" android:focusable= "false" Android:text= ""/> </LinearLayout> </RelativeLayout></LinearLayout>


Use a list of pages

public class Mainactivity extends Activity {private ListView listview;private listviewadapter adapter;private string[] Be ans = new string[] {"1", "2", "3", "4", "5", "6", "7", "8", "9", "Ten", "one", "one", "one"}; @Overrideprotected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (R.layout.activity_main); Initview ();} private void Initview () {//TODO auto-generated method stublog.i ("HTP", "beans.size:" + beans.length); listview = (ListView ) Findviewbyid (r.id.listview1); adapter = new Listviewadapter (mainactivity.this, beans); Listview.setadapter (adapter) ; Listview.setchoicemode (listview.choice_mode_single);}

Adapter content, where to deal with single-selection issues


Package Com.example.listviewdemo;import Java.util.hashmap;import Java.util.list;import android.content.Context; Import Android.view.layoutinflater;import Android.view.view;import Android.view.viewgroup;import Android.widget.baseadapter;import Android.widget.imageview;import Android.widget.linearlayout;import Android.widget.radiobutton;import Android.widget.textview;public class Listviewadapter extends BaseAdapter {private Context context;private string[] beans;//is used to record the status of each RadioButton and to ensure that only one hashmap<string is selected, boolean> states = new Hashmap<string, boolean> (); class Viewholder {TextView tvname; RadioButton Rb_state;} Public Listviewadapter (context context, string[] beans) {//TODO auto-generated constructor Stubthis.beans = beans;this.c Ontext = context;} @Overridepublic int GetCount () {//TODO auto-generated method Stubreturn beans.length;} @Overridepublic Object getItem (int position) {//TODO auto-generated method Stubreturn beans[position];} @Overridepublic long getitemid (int positiON) {//TODO auto-generated method Stubreturn position;}  @Overridepublic View GetView (final int position, view Convertview, ViewGroup parent) {//TODO auto-generated method stub// Page Viewholder holder; String bean = beans[position]; Layoutinflater Inflater = layoutinflater.from (context), if (Convertview = = null) {Convertview = Inflater.inflate ( R.layout.assist_device_binding_list_item, null); holder = new Viewholder ();//holder.rb_state = (RadioButton) Convertview//.findviewbyid (r.id.rb_light); holder.tvname = (TextView) Convertview.findviewbyid (R.id.tv_device_name ); Convertview.settag (holder);} else {holder = (Viewholder) Convertview.gettag ();}      Holder.tvName.setText (Bean), Final RadioButton radio= (RadioButton) Convertview.findviewbyid (r.id.rb_light);  Holder.rb_state = Radio; Holder.rb_state.setOnClickListener (New View.onclicklistener () {public void OnClick (View v) {//reset ensures that only one item is selected for ( String Key:states.keySet ()) {States.put (key, false);} States.put (string.valueof (position), Radio.ischeckEd ()); ListViewAdapter.this.notifyDataSetChanged ();}}); Boolean res = False;if (States.get (string.valueof (position)) = = null| | States.get (string.valueof (position)) = = False) { res = False;states.put (string.valueof (position), false);} Elseres = true;holder.rb_state.setchecked (res); return convertview;}}


The ListView in Android----Custom Radio list with RadioButton

Related Article

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: info-contact@alibabacloud.com 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.