Implement listview with images and checkbox in Android

Source: Internet
Author: User

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 >}

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.