Android Course---optimization listview list view (2)

Source: Internet
Author: User

Layout_simple.xml

<?XML version= "1.0" encoding= "Utf-8"?><LinearLayoutxmlns:android= "Http://schemas.android.com/apk/res/android"Android:layout_width= "Match_parent"Android:layout_height= "Wrap_content">    <ImageViewAndroid:layout_width= "70DP"Android:layout_height= "70DP"android:src= "@drawable/yuantu"Android:id= "@+id/lv2"/>    <LinearLayoutAndroid:layout_width= "Match_parent"Android:layout_height= "Wrap_content"android:orientation= "vertical"android:layout_gravity= "Center_vertical"Android:layout_marginleft= "20DP">        <TextViewAndroid:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"Android:text= "Food Pictures"Android:id= "@+id/tv1"/>        <TextViewAndroid:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"Android:text= "very tasty."Android:id= "@+id/tv2"/>    </LinearLayout></LinearLayout>

Activity_ui5.xml

<? XML version= "1.0" encoding= "Utf-8" ?> <  xmlns:android= "http://schemas.android.com/apk/res/android"    Android:layout_ Width= "Match_parent"    android:layout_height= "Match_parent"     Android:id= "@+id/lv_2"></ListView>

Uiactity.java

 PackageCom.hanqi.test5;ImportAndroid.os.Bundle;Importandroid.support.v7.app.AppCompatActivity;ImportAndroid.util.Log;ImportAndroid.view.LayoutInflater;ImportAndroid.view.View;ImportAndroid.view.ViewGroup;ImportAndroid.widget.BaseAdapter;ImportAndroid.widget.ImageView;ImportAndroid.widget.ListView;ImportAndroid.widget.TextView;Importjava.util.ArrayList;Importjava.util.List; Public classUIActivity5extendsappcompatactivity{//Data Source//list<map<string,object>> LM;List<food>LF; @Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate);        Setcontentview (R.LAYOUT.ACTIVITY_UI5); //Get ListViewListView lv_2 =(ListView) Findviewbyid (r.id.lv_2); //preparing the data sourceLF =NewArraylist<food>(); Lf.add (NewFood (R.drawable.yuantu, "Content 1", "Gourmet 1")); Lf.add (NewFood (R.drawable.yuantu, "Content 2", "Gourmet 2")); Lf.add (NewFood (R.drawable.yuantu, "Content 3", "Gourmet 3")); Lf.add (NewFood (R.drawable.yuantu, "Content 4", "Gourmet 4")); /*lm =new arraylist<map<string,object>> ();        map<string,object> map = new hashtable<string, object> ();        Map.put ("image", R.drawable.yuantu);        Map.put ("name", "Gourmet picture 01");        Map.put ("Content", "very tasty ah 01");        Lm.add (map);        Map = new hashtable<string, object> ();        Map.put ("image", R.drawable.yuantu);        Map.put ("name", "Gourmet Picture 02");        Map.put ("Content", "very tasty ah 02");        Lm.add (map);        Map = new hashtable<string, object> ();        Map.put ("image", R.drawable.yuantu);        Map.put ("name", "Gourmet picture 03");        Map.put ("Content", "very tasty ah 03");        Lm.add (map);        Map = new hashtable<string, object> ();        Map.put ("image", R.drawable.yuantu);        Map.put ("name", "Gourmet picture 04");        Map.put ("Content", "very tasty ah 04"); Lm.add (map);*/Lv_2.setadapter (NewMybaseadapter ()); }    classMybaseadapterextendsBaseadapter {//get the number of rows to display@Override Public intGetCount () {returnlf.size (); }        //gets the row that corresponds to the subscript@Override PublicObject GetItem (intposition) {            returnLf.get (position); }        //gets the ID of the corresponding row@Override Public LongGetitemid (intposition) {            returnposition; }        //fitting data to a view, returning a view//3 parameters: 1.position (subscript of data)//2.convertView (Reusable view)//3.parent (parent view)@Override PublicView GetView (intposition, View Convertview, ViewGroup parent) {            //if Convertview is empty, the layout file is loaded            if(Convertview = =NULL) {LOG.E ("TAG", "position =" +position); //1. Get the style file (layout file)Layoutinflater Layoutinflater =Getlayoutinflater (); Convertview= Layoutinflater.inflate (R.layout.layout_simple,NULL); }            //2. Binding The view component of the data and layout files//1) Get a row of data from the data source//map<string,object> Map = lm.get (position); Food and food=Lf.get (position); //2) Get the view component of the layout fileImageView iv_2 =(ImageView) Convertview.findviewbyid (R.ID.LV2); TextView TV1=(TextView) Convertview.findviewbyid (R.ID.TV1); TextView TV2=(TextView) Convertview.findviewbyid (R.ID.TV2); //3) binding (data adapter)//Iv_2.setimageresource ((int) map.get ("image"));//Tv1.settext (Map.get ("name"). toString ());//Tv2.settext (Map.get ("content"). toString ());Iv_2.setimageresource ((int) Food.getimage ());            Tv1.settext (Food.getname ());            Tv2.settext (Food.getcontent ()); //3. Return to view            returnConvertview; }    }}

Android Course---optimization listview list view (2)

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.