Examples of items that are useful to check boxes, ah ah ... Find the information on the Internet, most of them are too cumbersome, so I decided to write a demo of this aspect ...
First give A:
To add a check box to a ListView focus on the following issues:
1. ListView item with the control in item grab focus problem (must set checkbox not clickable and cannot get focus, let ListView get Focus)
2. Setchoicmode (int choicemode) selection mode of the ListView (): Choicemode:choice_mode_none, Choice_mode_single, or Choice_mode _multiple are not selectable, single, multi-choice
3, click the ListView Item, reverse the status of the CheckBox, Mcheckbox.toggle ();
Speaking of which, you can see the logic of the ListView implementation check box is still relatively simple, for the first question, why do you need to let the checkbox lose focus? Because controls such as buttons, checkboxes, ImageButton, or their child controls, such as the system's default clickable control, have a higher priority relative to other controls that are not clickable by default, so they will give priority to focus when used with other controls. So if you don't set the checkbox to lose focus, then the ListView is not clickable, just click the CheckBox control.
OK, just put the code on it:
This method is to display a ListView check box:
private void Showallappinfo () {View view = Layoutinflater.from (this). Inflate (R.layout.show_appinfo,null); ListView Mlistview = (ListView) View.findviewbyid (R.id.show_appinfo_listview); Button Mbtncancel = (button) View.findviewbyid (R.id.show_appinfo_cancel); Button Mbtnsubmit = (button) View.findviewbyid (r.id.show_appinfo_submit); final Myadapter adapter = new Myadapter (); Mlistview.setadapter (adapter); Mlistview.setitemscanfocus (true);//Let Item get focus, "note" The checkbox control in the ListView item layout must be lost focus Mlistview.setchoicemode (listview.choice_mode_multiple);// Set multi-select mode Mlistview.setonitemclicklistener (New Onitemclicklistener () {@Overridepublic void Onitemclick (adapterview< ?> parent, view view,int position, long ID) {Viewholder holder = (viewholder) view.gettag ();//Viewholder object that gets the View that is currently clicked Holder.mCheckBox.toggle ();//When clicked, set the opposite state Adapter.mCheckBoxSelected.put (position,holder.mcheckbox.ischecked ());// Modify the check in the corresponding position checkbox in HashMap});D isplay Display = Getwindowmanager (). Getdefaultdisplay (); Point outsize = new PoInt ();d isplay.getsize (outsize), int width = outsize.x;int height = outsize.y;final Popupwindow pop = new Popupwindow (view, Width * 3/4,height * 4/5, true); Windowmanager.layoutparams params = GetWindow (). GetAttributes ();p Arams.alpha = 0.6f;getwindow (). SetAttributes ( params); Mbtnsubmit.setonclicklistener (new Onclicklistener () {@Overridepublic void OnClick (View v) {StringBuffer sb = New StringBuffer (); for (int i = 0; i < adapter.appInfoList.size (); i++) {//Iterate through the collection to get the information of the Select item if (Adapter.mcheckboxsele Cted.get (i)) {String selectedappname = Adapter.appInfoList.get (i). Getappname ();//drawable Selectedappicon = Adapter.appInfoList.get (i). Getappicon (); Sb.append (selectedappname+ ",");}} Pop.dismiss (); Toast.maketext (Getapplicationcontext (), sb.tostring (), Toast.length_short). Show ();}); Mbtncancel.setonclicklistener (New Onclicklistener () {@Overridepublic void OnClick (View v) {Pop.dismiss ();}}); Pop.setondismisslistener (New Ondismisslistener () {@Overridepublic void Ondismiss () {windowmanager.layouTparams params = GetWindow (). GetAttributes ();p Arams.alpha = 1.0f;getwindow (). SetAttributes (params);}); Pop.setbackgrounddrawable (New Bitmapdrawable ());p op.setoutsidetouchable (True);p Op.setanimationstyle ( r.style.showpopupanimation);p op.showatlocation (view, gravity.center, 0, 0);}
rewrite adapter, Myadapter.java
public class Myadapter extends Baseadapter {public list<appinfo> appinfolist;public Hashmap<integer, Boolean > Mcheckboxselected;public myadapter () {appinfolist = Getsystemappinfo (); mcheckboxselected = new HashMap<Integer , boolean> (); for (int i = 0; i < appinfolist.size (); i++) {Mcheckboxselected.put (I, false);//Initialize, default unchecked}} @Overridepu Blic int GetCount () {return appinfolist.size ();} @Overridepublic Object getItem (int position) {return appinfolist.get (position);} @Overridepublic long Getitemid (int position) {return position;} @Overridepublic view GetView (int position, view Convertview, ViewGroup parent) {Viewholder viewholder;if (Convertview = = N ull) {Viewholder = new Viewholder (); Convertview = Layoutinflater.from (mainactivity.this). Inflate (R.layout.listview_ Item, NULL, TRUE); Viewholder.mimgappicon = (ImageView) Convertview.findviewbyid (r.id.listview_item_img); Viewholder.mtvappname = (TextView) Convertview.findviewbyid (R.ID.LISTVIEW_ITEM_TV); Viewholder.mcheckbox = (ChecKbox) Convertview.findviewbyid (R.ID.LISTVIEW_ITEM_CBX); Convertview.settag (Viewholder);} else {Viewholder = (Viewholder) Convertview.gettag ();} ViewHolder.mImgAppIcon.setImageDrawable (Appinfolist.get (position). Getappicon ()); ViewHolder.mTvAppName.setText ( Appinfolist.get (position). Getappname ()); viewHolder.mCheckBox.setChecked (Mcheckboxselected.get (position)); return Convertview;}}
Viewholder class:
Public final static class Viewholder {ImageView Mimgappicon; TextView Mtvappname; CheckBox Mcheckbox;}
get system-wide non-system apps:
Get the list of non-system applications private list<appinfo> Getsystemappinfo () {list<appinfo> list = new arraylist<appinfo> (); list<packageinfo> packages = This.getpackagemanager (). getinstalledpackages (0); AppInfo AppInfo = null;for (PackageInfo temp:packages) {if (Temp.applicationInfo.flags & Applicationinfo.flag_syst EM) = = 0) {//Non-system application String AppName = (string) Temp.applicationInfo.loadLabel (Getpackagemanager ());D rawable AppIcon = temp. Applicationinfo.loadicon (Getpackagemanager ()); appInfo = new AppInfo (AppIcon, appName); List.add (appInfo); appInfo = null;} else {//System application}}return list;}
Of course there is a JavaBean class: Appinfo.java, the code for this class is not posted.
SOURCE download please poke: http://download.csdn.net/detail/u010687392/8702343
Android Combat ListView check box