Mainactivity.java
Package Com.example.listviewdemo4;import Java.util.arraylist;import Java.util.hashmap;import java.util.List;import Java.util.map;import Android.os.bundle;import Android.app.activity;import Android.view.menu;import Android.widget.listview;public class Mainactivity extends Activity {listview listview; list<map<string, object>> list; @Overrideprotected void OnCreate (Bundle savedinstancestate) { Super.oncreate (savedinstancestate); Setcontentview (r.layout.activity_main); listview = (ListView) Findviewbyid ( R.id.listview);//Get Data List = GetData (); Listview.setadapter (new Listviewadapter (List,mainactivity.this));} Private list<map<string, object>> getData () {//TODO auto-generated method Stublist<map<string, object>> list = new arraylist<map<string, object>> (); map<string, object> map = new hashmap<string, object> (), Map.put ("name", "Figure 1"), Map.put ("Pictrue", R.DRAWABLE.TU1); List.add (map); map = new hashmap<string, object> (); Map.put ("Name","Figure 2"); Map.put ("Pictrue", R.DRAWABLE.TU2); List.add (map); map = new hashmap<string, object> (); Map.put ("name", "Figure 3 "); Map.put (" Pictrue ", R.DRAWABLE.TU3); List.add (map); map = new hashmap<string, object> (); Map.put (" name "," Figure 4 "); Map.put ("Pictrue", R.drawable.tu4); List.add (map); return list;}}
Listviewadapter.java
Package Com.example.listviewdemo4;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.imageview;import Android.widget.textview;public Class Listviewadapter extends Baseadapter{list<map<string, object>> List; Context context; Layoutinflater Layoutinflater; Listviewadapter (list<map<string, object>> list,context Context) {this.list=list;this.context=context;/ /use context contexts to get the system Layout_inflater Service layoutinflater= (layoutinflater) context.getsystemservice (context. Layout_inflater_service);} @Overridepublic int GetCount () {//TODO auto-generated method Stubreturn list.size ();} @Overridepublic Object getItem (int arg0) {//TODO auto-generated method Stubreturn List.get (arg0);} @Overridepublic long getitemid (int arg0) {//TODO auto-generated method Stubreturn arg0;} @Overridepublic view GetView (int position, view contentView, ViewGroup arg2) {//layoutinflater incoming list_item generates a Viewview view=layoutinflater.inflate (R.layout.list_item, NULL);
Look for controls in the generated view TextView textview= (TextView) View.findviewbyid (R.id.textview);
Set the control content Textview.settext ((charsequence) list.get (position). Get ("name")); ImageView imageview= (ImageView) View.findviewbyid (R.id.imageview);
Set the picture's Resource Imageview.setimageresource ((Integer) list.get (position). Get ("Pictrue")); return view;}}
Configuration file:
Activity_main.xml:
<relativelayout xmlns:android= "http://schemas.android.com/apk/res/android" xmlns:tools= "http// Schemas.android.com/tools " android:layout_width=" match_parent " android:layout_height=" Match_parent " android:paddingbottom= "@dimen/activity_vertical_margin" android:paddingleft= "@dimen/activity_ Horizontal_margin " android:paddingright=" @dimen/activity_horizontal_margin " android:paddingtop=" @dimen /activity_vertical_margin " tools:context=". Mainactivity "> <textview android:layout_width=" wrap_content " android:layout_height=" Wrap_ Content " android:text=" @string/hello_world "/> <listview android:layout_width=" Wrap_ Content " android:layout_height=" wrap_content " android:id=" @+id/listview " ></ListView> </RelativeLayout>
Listview_item.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" > <textview android:layout_width= "wrap_content" android:layout_height= " Wrap_content " android:id=" @+id/textview " /> <imageview android:layout_width=" Wrap_ Content " android:layout_height=" wrap_content " android:id=" @+id/imageview " /></ Linearlayout>
ListView is not optimized before