Haven't written a blog for a long time, recently in the development of Android, suddenly think of here, you can post some code here, do some records, and share the exchange with you. Android development, often used in the mix of RadioButton and ListView, the user click on an item, and then record the selected state, the most important thing is to record the user click on the selected item location.
The layout file is simple:
<linearlayout xmlns:android= "Http://schemas.android.com/apk/res/android" xmlns: tools= "Http://schemas.android.com/tools" android:layout_width= "Match_parent" android:layout_height= "Match_parent" android:layout_marginleft= "10DP" android:layout_marginright= "10DP" android:orientation= "vertical " > <textview android:layout_ Width= "Match_parent" android:layout_height= "Wrap_content" android:gravity= "Center" android:text= "@string/select_authors" android:textsize= " 25SP " /> <listview android:id = "@+id/list" android:layout_width= "Match_parent" android:layout_height= "Match_parent" android: Scrollbars= "None" /></LinearLayout>
is not complex:
<relativelayout xmlns:android= "Http://schemas.android.com/apk/res/android" xmlns:tools= "Http://schemas.android.com/tools" android:layout_width= "Match_parent" android:layout_height= "Match_parent" > <TextView android:id= "@+id/author" android:layout_width= "Wrap_content" android:layout_height = "Wrap_content" android:layout_alignparentleft= "true" android:layout_centervertical= "true" android:text= "@string/author_name" android: Textsize= "20SP"/> <radiobutton Android:id= "@+id/radio" &Nbsp; android:layout_width= "Wrap_content" android:layout_height= "Wrap_content" android: Layout_alignparentright= "true" android:layout_centervertical= " True "/></relativelayout>
radiobuttonlist
package com.example.radiobuttonlisttest;import android.app.activity;import android.os.bundle; import android.widget.listview;public class radiobuttonlist extends activity { private listview radiobuttonlist;private radioadapter adapter;// simulates several data, As List entries private string[] authors = { "Mustard Ryunosuke", "Mishima Yukio", "Kawabata", "Village Spring Tree" , "East Wild GUI Wu", "Zhang ailing", "Jin Yong", "Qian Zhongshu", "Lao She", "Liang Shiqiu", "Henry Miller", "Hemingway", "Fitzgerald", "Kerouac", "Jack London", "Alexandre Dumas", "Duras", "Fu Lou Worship", "Hugo", "Balzac", "Shakespeare", "Lawrence", "Maugham", "Conan Doyle", "Defoe" }; @Overrideprotected void oncreate (bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (r.layout.activity_radio_button_list); radioButtonList = (ListView) findviewbyid (r.id.list); Adapter = new radioadapter (this, authors); Radiobuttonlist.setadapter (adapter);}}
adapters are the most critical, Mark the selected position, the selected state will not be because listview
package com.example.radiobuttonlisttest;import android.content.context;import android.view.layoutinflater;import android.view.view;import android.view.viewgroup;import android.widget.baseadapter;import android.widget.compoundbutton;import android.widget.compoundbutton.oncheckedchangelistener;import android.widget.radiobutton;import Android.widget.textview;import android.widget.toast;public class radioadapter extends baseadapter {private layoutinflater inflater;private string[] authors;private viewHolder holder;// mark the author that the user is currently selecting private int index = -1;private Context c;public radioadapter (context c, string[] authors) {super ();this.c = c;this.authors = authors;inflater = layoutinflater.from (c);} @Overridepublic int getcount () {return authors.length;} @Overridepublic objEct getitem (int position) {return null;} @Overridepublic long getitemid (int position) {return 0;} @Overridepublic view getview (final int position, view convertview, viewgroup parent) {holder = new viewholder ();if (convertView == null) {convertview = inflater.inflate (r.layout.item_list, null);holder.nameTxt = ( TextView) convertview.findviewbyid (r.id.author);holder.selectbtn = (RadioButton) Convertview.findviewbyid (R.id.radio); Convertview.settag (holder);} else {holder = (Viewholder) convertview.gettag ();} Holder.nameTxt.setText (Authors[position]); Holder.selectBtn.setOnCheckedChangeListener (new Oncheckedchangelistener () {@Overridepublic void oncheckedchanged (compoundbutton buttonview , boolean ischecked) {if (isChecked) {toast.maketext (c, "Your chosen writer is:" &nbsP;+ authors[position],toast.length_long). Show (); Index = position;notifydatasetchanged ();}}); if (index == position) {// whether the selected entry and the current entry are equal holder.selectBtn.setChecked (true);} else {holder.selectbtn.setchecked (false);} Return convertview;} public class viewholder {public textview nametxt;public radiobutton SELECTBTN;}}
Finally look at:
650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M02/4D/9A/wKioL1RUmYHzGp8wAAHUsdUIf08328.jpg "style=" float: none; "title=" 001v7ylagy6neaqrzzzb9&690.jpg "alt=" Wkiol1rumyhzgp8waahusduif08328.jpg "/>
650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M02/4D/9A/wKiom1RUmSXi98iQAAIRDufO4-I942.jpg "style=" float: none; "title=" 001v7ylagy6neaqwizy98&690.jpg "alt=" Wkiom1rumsxi98iqaairdufo4-i942.jpg "/>
Hybrid use of Android RadioButton with ListView