Android: layout_weight = "1" can increase the weight of the textview in the middle, so that the checkbok can be displayed on the right.
Android: focusable = "false"
Android: focusableintouchmode = "false"
Android: clickable = "false"
These three statements are very important. If they are not added, an error will occur. Paris Hilton ballet waterfront hairstyle
Because the checkbox click event has a higher priority than the listview, you must add Android: focusable = "false" to the checkbox so that the checkbox does not obtain the focus at the beginning.
Next, add the listview component in Main. xml.
Package com. zwq. umeng;
Import java. util. arraylist;
Import java. util. hashmap;
Import java. util. List;
Import java. util. Map;
Import Android. content. context;
Import Android. View. layoutinflater;
Import Android. View. view;
Import Android. View. viewgroup;
Import Android. widget. baseadapter;
Import Android. widget. checkbox;
Import Android. widget. imageview;
Import Android. widget. textview;
Public class myadapter extends baseadapter {
Private layoutinflater minflater;
Private list <Map <string, Object> mdata;
Public static Map <integer, Boolean> isselected;
Public myadapter (context ){
Minflater = layoutinflater. From (context );
Init ();
}
// Initialization
Private void Init (){
Mdata = new arraylist <Map <string, Object> ();
For (INT I = 0; I <5; I ++ ){
Map <string, Object> map = new hashmap <string, Object> ();
Map. Put ("IMG", R. drawable. Icon );
Map. Put ("title", "Number" + (I + 1) + "Row title ");
Mdata. Add (MAP );
}
// Here, the map isselected is defined to record the status of each listitem, and the initial status is all false.
Isselected = new hashmap <integer, Boolean> ();
For (INT I = 0; I <mdata. Size (); I ++ ){
Isselected. Put (I, false );
}
}
@ Override
Public int getcount (){
Return mdata. Size ();
}
@ Override
Public object getitem (INT position ){
Return NULL;
}
@ Override
Public long getitemid (INT position ){
Return 0;
}
@ Override
Public View getview (INT position, view convertview, viewgroup parent ){
Viewholder holder = NULL;
// Initialize convertview when convertview is null.
If (convertview = NULL ){
Holder = new viewholder ();
Convertview = minflater. Inflate (R. layout. vlist, null );
Holder. IMG = (imageview) convertview. findviewbyid (R. Id. IMG );
Holder. Title = (textview) convertview. findviewbyid (R. Id. Title );
Holder. cbox = (checkbox) convertview. findviewbyid (R. Id. CB );
Convertview. settag (holder );
} Else {
Holder = (viewholder) convertview. gettag ();
}
Holder. IMG. setbackgroundresource (integer) mdata. Get (position). Get (
"IMG "));
Holder. Title. settext (mdata. Get (position). Get ("title"). tostring ());
Holder. cbox. setchecked (isselected. Get (position ));
Return convertview;
}
Public final class viewholder {
Public imageview IMG;
Public textview title;
Public checkbox cbox;
}< BR >}