Android ListView Radio CheckBox

Source: Internet
Author: User

Ideas
1 Trigger Event Setonitemclicklistener change checkbox
2 Change the checkbox status in the adapter's GetView () to record the selected checkbox position
3 Adapter provides method change checkbox
4 Start GetView with notifydatasetchanged ()
Implementation code

//Adapter Public  class myadapter extends baseadapter {        //Fill list of data        Privatearraylist<map<string, object>> list;//To control the check status of the checkbox        PrivateHashmap<integer,boolean> isSelected;//Context        PrivateContext context;//To import layouts        PrivateLayoutinflater Inflater =NULL;//Builder         Public Myadapter(arraylist<map<string, object>> list, context context) { This. Context = Context; This. list = list;            Inflater = Layoutinflater.from (context); isSelected =NewHashmap<integer, boolean> ();//Initialize dataInitdate (); }//Initialize the isselected data        Private void initdate(){ for(intI=0; I<list.size (); i++) {getisselected (). put (I,false); }        }@Override         Public int GetCount() {returnList.size (); }@Override         PublicObjectGetItem(intPosition) {returnList.get (position); }@Override         Public Long Getitemid(intPosition) {returnPosition }@Override         PublicViewGetView(intPosition, View Convertview, ViewGroup parent) {//import layouts and assign values to ConvertviewConvertview = Inflater.inflate (r.layout.item_exchange_self,NULL);            TextView tv_name = (TextView) Convertview.findviewbyid (r.id.exchange_self_name);            TextView tv_address = (TextView) Convertview.findviewbyid (r.id.exchange_self_address);            TextView tv_date = (TextView) Convertview.findviewbyid (r.id.exchange_self_date); CheckBox checkbox = (checkbox) Convertview.findviewbyid (r.id.select_address);//Set the display of TextView in listTv_name.settext (List.get (position). Get ("Name"). ToString ()); Tv_address.settext (List.get (position). Get ("Address"). ToString ()); Tv_date.settext (List.get (position). Get ("Workdates"). ToString ());//Set checkbox selection according to isselectedCheckbox.setchecked (getisselected (). get (position));returnConvertview; } PublicHashmap<integer,boolean>getisselected() {returnisSelected; } Public void setisselected(intPostiton) {initdate (); This. Isselected.put (Postiton,true);        Notifydatasetchanged (); }    }
//listview  ListView        Exchangecenters = (ListView) Findviewbyid (r.id.exchange_centers); final  Myadapter myadapter = new  myadapter (GetData (), this        );        Exchangecenters.setadapter (Myadapter);            Exchangecenters.setonitemclicklistener (new  Adapterview.onitemclicklistener () {             @Override  public  void  onitemclick  (adapterview<?> parent, view view, int  position,                 Long  ID) {myadapter.setisselected (position);            Exchangecenter = GetData (). get (position); }        });

Considerations for Layout
1.CheckBox own Click to close, add property android:clickable= "false"
2.item Headquarters Bureau needs to add android:descendantfocusability= "blocksdescendants" to ensure that the ListView click event is OK
3.ListView Property plus android:layout_weight= "1" ensures that the button below is not covered

Android ListView Radio CheckBox

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.