Android-listview controls

Source: Internet
Author: User

1: First create a ListView view file. Listview.xml

<?xml version= "1.0" encoding= "Utf-8"? ><linearlayout xmlns:android= "http://schemas.android.com/apk/res/ Android "    android:layout_width=" match_parent "    android:layout_height=" match_parent "    android:o rientation= "Vertical" >        <!--    The ListView is a more commonly used component in Android development,    it presents the content as a list, and can be displayed adaptively based on the length of the data. --        <listview           android:id= "@+id/listview1"           android:layout_width= "Match_parent"           android:layout_height= "Wrap_content"           android:layout_margin= "5DP"            android:divider= "#dddddd"           android:dividerheight= "1DP" >    </ListView></LinearLayout>

  

2: Defines a view file for the display interface. Item.xml

<?xml version= "1.0" encoding= "Utf-8"? ><relativelayout xmlns:android= "http://schemas.android.com/apk/res/ Android "Android:layout_width=" Match_parent "android:layout_height=" match_parent "> <imageview Andro Id:id= "@+id/pic" android:layout_width= "50DP" android:layout_height= "50DP" Android:layout_alignparentl Eft= "true" android:layout_alignparenttop= "true" android:layout_marginleft= "10DP" Android:layout_margi ntop= "5DP" android:src= "@drawable/xms" android:layout_marginbottom= "5DP"/> <textview androi D:id= "@+id/text1" android:layout_width= "wrap_content" android:layout_height= "Wrap_content" Android:la yout_marginleft= "10DP" android:layout_torightof= "@+id/pic" android:textsize= "16SP" Android:te Xtcolor= "#333" android:layout_margintop= "10DP"/> <textview android:id= "@+id/text2" android:l       Ayout_width= "Wrap_content" android:layout_height= "Wrap_content" android:layout_alignleft= "@+id/text1" android:layout_below= "@+id/text1 "Android:textcolor=" #666 "android:textsize=" 14sp "/> <textview android:id=" @+id/tex T3 "android:layout_width=" wrap_content "android:layout_height=" wrap_content "android:layout_above=" @+ Id/text2 "android:layout_alignparentright=" true "android:textcolor=" #666 "android:textsize=" 12SP "android:layout_marginright=" 5DP "/></relativelayout>

 

3: Write code in source file: First way (Simpleadapter adapter)

Package Com.huanglinbin.weixintext;import Java.util.arraylist;import Java.util.hashmap;import java.util.List; Import Java.util.map;import android.app.activity;import Android.os.bundle;import Android.widget.listview;import Android.widget.radiogroup;import Android.widget.simpleadapter;public class Mainactivity extends Activity {// Privatize the ListView control. Private ListView listview;//Privatization of the ListView adapter. Private Simpleadapter simpleadapter;//Privatisation list collection. Private list<map<string,object>> List = new arraylist<map<string,object>> ();// Privatization of Radiogub controls.    Private Radiogroup Radiogroup;        @Override protected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);                Setcontentview (R.layout.activity_main);        Find the two controls first.        ListView = (ListView) Findviewbyid (R.id.listview1);                Radiogroup = (radiogroup) Findviewbyid (r.id.radiogroup1);        Assigns the data source to the collection.        List = GetDate (); Define an adapter Simpleadapter = new SimpleAdapter (this,//to ask the object.        list,//data source (that is, the data from the database is stored here, and then loop output) r.layout.item,//page output template.        New string[]{"pic", "Text1", "Text2", "Text3"},//This is equivalent to the attribute in the entity class, and these attributes are stored in the array.        New INT[]{R.ID.PIC,R.ID.TEXT1,R.ID.TEXT2,R.ID.TEXT3}//This relative to the handle, through the handle to find the data source in the template.        );        Add the adapter to the ListView control.           Listview.setadapter (Simpleadapter);    }//define the data source.    Private List<map<string,object>> GetDate () {//define a loop to output the data loop.    for (int i=0;i<5;i++) {//define a map collection and add the collection to the Map collection.    map<string,object> map = new hashmap<string, object> ();    Map.put ("Pic", R.DRAWABLE.XMS);    Map.put ("Text1", "Little Demon God");    Map.put ("Text2", "Android class teacher");    Map.put ("Text3", "Yesterday");    Add the Map collection to the list collection.    List.add (map);    } return list; }           }

  

Second way: (Baseadapter adapter)

Simple understanding for VC tied together Baseadapter, store length recommended 5 star Lv.setadapter (new Baseadapter () {//return how many records @overridepublic int GetCount () {//TODO auto-generated method Stubreturn messagelist.size ();} Each item item returns an interface @overridepublic view GetView (int position, view Convertview, ViewGroup parent) {View view = null;//layout unchanged, Data Change//If the cache is empty, we generate a new layout as 1 itemif (convertview==null) {log.i ("info:", "no cache, regenerate" +position); Layoutinflater inflater = MainActivity.this.getLayoutInflater ();//Because the object returned by GetView (), adapter is automatically assigned to Listviewview = Inflater.inflate (r.layout.listview_item_layout, null);} ELSE{LOG.I ("Info:", "has cache, does not need to regenerate" +position); view = Convertview;} Message m = messagelist.get (position); TextView tv_username = (TextView) View.findviewbyid (r.id.tv_username); Tv_username.settext (M.getusername ()); tv_ Username.settextsize (15);  TextView tv_lastmessage = (TextView) View.findviewbyid (r.id.tv_lastmessage); Tv_lastmessage.settext ( M.getlastmessage ()); Tv_lastmessage.settextsize (12); TextView tv_datetime = (TextView) vIew.findviewbyid (R.id.tv_datetime); Tv_datetime.settext (M.getdatetime ()); Tv_datetime.settextsize (n); return view ;} @Overridepublic Object getItem (int position) {//TODO auto-generated method Stubreturn null;}                @Overridepublic long Getitemid (int position) {//TODO auto-generated method Stubreturn 0;} } );

  

Android-listview controls

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.