Android--ListView (simpleadapter) custom Adapter

Source: Internet
Author: User

2. Code



Package Com.himi;import Java.util.arraylist;import java.util.hashmap;import java.util.list;import java.util.Map; Import;import android.os.bundle;import android.view.view;import Android.widget.AdapterView; Import Android.widget.adapterview.onitemclicklistener;import Android.widget.listview;import Android.widget.simpleadapter;import Android.widget.toast;public class Mainactivity extends Activity {private Simpleadapter adapter;//declaration Adapter Object Private ListView ListView; Declaring list View objects private List < Map < String, object >> list;//Declaration list container public static mainactivity ma;@ Overridepubli c void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); ma = this;//instantiation list container, List = new Arrayl Ist < Map < string, Object >> (); listview = new ListView (this);//Instantiate list view//instance a list data container Map < String, O Bject > Map = new HashMap < String, Object > ();//Add Data map.put to the list container ("Item1_imageivew", R.drawable.icon); ma P.put ("Item1_bigtv", "BIGTV "), Map.put (" Item1_smalltv "," SMALLTV ");//Add list data to the list container list.add (map);//use Android to provide the Simpleadapter adapter, can not implement component monitoring; /adapter = new Simpleadapter (this, list, r.layout.main,//new string[] {"Item1_imageivew", "Item1_bigtv", "ITEM1_SMALLTV "},//new int[] {, R.ID.BIGTV, R.ID.SMALLTV});//--use a custom adapter to listen for event monitoring of each item in its listview mysimpleadapter adapter = new My Simpleadapter (this, list, R.lay                                                            Out.main, New String [] {                                                            "Item1_imageivew", "ITEM1_BIGTV",                              "Item1_smalltv"},                          new int [] {R.ID.IV,                                   R.ID.BIGTV, R.ID.SMALLTV  });//set Adapter for List view (map data to List view) Listview.setadapter (adapter); Listview.setonitemclicklistener (                              New Onitemclicklistener () {@ overridepublic void Onitemclick (Adapterview <? > Parent, View view, int position, long ID) {//TODO Auto-generat                                                            Ed Method Stubtoast.maketext (Mainactivity.this, "You clicked on the first:"                              + Position + "items", Toast.length_short). Show ();}} )////Display list view This.setcontentview (ListView);}}


Package Com.himi;import Java.util.list;import;import;import Android.content.context;import Android.view.layoutinflater;import Android.view.view;import Android.view.ViewGroup ; Import Android.widget.baseadapter;import Android.widget.button;import Android.widget.checkbox;import Android.widget.compoundbutton;import Android.widget.imageview;import Android.widget.textview;import Android.widget.compoundbutton.oncheckedchangelistener;public class Mysimpleadapter extends BaseAdapter {// Declares a Layoutinflater object (which is used to instantiate the layout) private Layoutinflater minflater;private List < Map < String, Object >> lis t;//Declaration list container Object private int layoutid; Declare layout idprivate String flag[];//Declare all components in a ListView item map index private INT itemids[];//Declaration ListView Item All component ID array public               Mysimpleadapter (Context context, List < Map < String, Object >> list, int LayoutID, String flag[], int itemids[]) {//Use construct to instantiate member variable object this.minflater = LayoutinflaTer.from (context); this.list = List;this.layoutid = Layoutid;this.flag = Flag;this. Itemids = Itemids;} @ overridepublic int GetCount () {return list.size ();//Returns the length of a ListView item}@ overridepublic Object getItem (int arg0) {RET Urn 0;} @ overridepublic long getitemid (int arg0) {return 0;} Instantiate layout and components and set component data//getview (int position, View Convertview, ViewGroup Parent)//First parameter: Number of lines plotted//                              Second argument: The drawing view here refers to the layout of each item in the ListView//Third parameter: View collection, which does not require @ overridepublic view GetView (int position, view Convertview,  ViewGroup parent) {//to instantiate the layout through the Minflater object as a Viewconvertview = Minflater.inflate (LayoutID, NULL  ); for (int i = 0; i < flag.length; i + +) {//traverse all components of each item//each component to make a matching decision to get the correct type of the component if (Convertview.findviewbyid (itemids [i]) instanceof ImageView) {//findviewbyid () function is instantiating a component in a layout//when the component is a ImageView type, it instantiates a ImageView object ImageView IV = (Image View) Convertview. Findviewbyid (Itemids [i]);//Set data Iv.setbackgroundresource for its components (Int Eger) LisT. Get (position). Get (flag [i]));}  else if (Convertview.findviewbyid (itemids [i]) instanceof TextView) {//When the component is of type TextView, instantiate a TextView object for it TextView TV = (TextView) Convertview. Findviewbyid (Itemids [i]);//Set data for its components Tv.settext ((String) L Ist.get (position). Get (flag [i]));}} Sets the listener for the button (button) Convertview.findviewbyid (R.ID.BTN)). Setonclicklistener (new View). Onclicklistener () {@ Override public void OnClick (View v)                                                            {//Here a dialog box pops up, followed by a detailed narration of new Alertdialog.builder ( MAINACTIVITY.MA). s                                  Ettitle ("Custom Simpleadapter")                          The. Setmessage ("button successfully triggers the Listener event!                              "). Show ();                              });//Set the Listener ((checkbox) Convertview.findviewbyid (R.ID.CB)) for the check box                              . Setoncheckedchangelistener (New Oncheckedchangelistener () {@ Override public void OnCheckedChanged (Compoundbutton                              Buttonview, Boolean isChecked) {                                                            A dialog box pops up, followed by a detailed narration of new Alertdialog.builder (                                                    Settitle ("Custom Simpleadapter") . Setmessage ("checkbox successfully triggered status Change listener event!              " )                                              . Show (); }}); return convertview;}}


<?xml version= "1.0" encoding= "Utf-8"? ><linearlayout xmlns:android= " Android "Android:layout_width=" Fill_parent "android:layout_height=" fill_parent "android:orientation=" Horizontal " > <imageview android:id= "@+id/iv" android:layout_width= "Wrap_content" android:layout_height= "Wrap_content"/> <linearlayout android:layout_width= "wrap_content" android:layout_height= "Wrap_con Tent "android:orientation=" vertical "> <textview android:id=" @+id/bigtv "Android        : layout_width= "wrap_content" android:layout_height= "wrap_content" android:textsize= "20sp"/> <textview android:id= "@+id/smalltv" android:layout_width= "Wrap_content" Android:lay out_height= "Wrap_content" android:textsize= "10sp"/> </LinearLayout> <button android:i D= "@+id/btn" Android:layout_Width= "Wrap_content" android:layout_height= "Wrap_content" android:focusable= "false" android:text= "but Ton "/> <checkbox android:id=" @+id/cb "android:layout_width=" Wrap_content "android:layout_he ight= "Wrap_content" android:focusable= "false"/></linearlayout>

Android--ListView (simpleadapter) custom Adapter

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: 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.