Learn to implement a ListView list with icons using the ListView component and the Simapleadapter adapter
A total of 3 parts
I.. mainactivity.java file
1 Package xqx;2 3 import java.util.ArrayList;4 import Java.util.HashMap;5 import java.util.List;6 import Java.util.Map;7 8 import COM.EXAMPLE.TEST.R;9 Ten import android.app.Activity; One import Android.content.res.TypedArray; A import Android.os.Bundle; - import Android.view.View; - import Android.view.View.OnClickListener; the import Android.view.ViewGroup; - import Android.widget.AdapterView; - import Android.widget.AdapterView.OnItemClickListener; - import Android.widget.BaseAdapter; + import android.widget.Gallery; - import Android.widget.ImageView; + import Android.widget.ListView; A import Android.widget.SimpleAdapter; at import Android.widget.Toast; - - Public classMainactivity extends activity{ - - PrivateListView ListView; - @Override in protected voidonCreate (Bundle savedinstancestate) { - //TODO auto-generated Method Stub to super.oncreate (savedinstancestate); + - Setcontentview (r.layout.mainactivity); the //Get list View *ListView =(ListView) Findviewbyid (r.id.listview1); $ //defines and initializes an array that holds the image IDPanax Notoginseng int[] ImageId =New int[]{r.drawable.menu_chapu,r.drawable.menu_chapu,r.drawable.menu_chapu,r.drawable.menu_chapu,r.drawable.menu _chapu}; - //defines and initializes an array of saved list item literals theString[] title =Newstring[]{"Tea Taboo","Tea Set","Tea Treatment","Tea Spectrum","more"}; + //Create a list collection AList<map<string,object>> ListItems =NewArraylist<map<string,object>>(); the //Place the picture ID and list item text in a map with a for loop and add it to the list collection + for(intI=0; i<imageid.length;i++) - { $ //instantiating a Map object $map<string,object> map =NewHashmap<string,object>(); -Map.put ("Image", Imageid[i]); -Map.put ("title", Title[i]); the //to add a map object to the list collection - listitems.add (map);Wuyi } theSimpleadapter adapter =NewSimpleadapter ( This, ListItems, R.layout.items,Newstring[]{"title","Image"},New int[]{r.id.title,r.id.image}]; - Listview.setadapter (adapter); Wu } - About}
Two, two layout
(1) Items.xml
1<?xml version="1.0"encoding="Utf-8"?>2<linearlayout xmlns:android="http://schemas.android.com/apk/res/android"3Android:layout_width="match_parent"4android:layout_height="match_parent"5android:orientation="Horizontal">6 7<ImageView8Android:id="@+id/image"9android:paddingright="10px"Tenandroid:paddingtop="20px"//Picture distance from head 20px OneAndroid:paddingbottom="20px"//Picture distance from bottom 20px Aandroid:adjustviewbounds="true" //
Maintain the aspect ratio of the picture when ImageView adjusts the boundary, it needs to be used with maxwidth, maxheight, otherwise it will not work alone. |
-Android:maxwidth="72px" -android:maxheight="72px" theAndroid:layout_width="wrap_content" -android:layout_height="wrap_content" -/> - +<TextView -Android:layout_width="wrap_content" +android:layout_height="wrap_content" Aandroid:padding="10px" atandroid:layout_gravity="Center" -Android:id="@+id/title" -/> -</LinearLayout>
For layout list item content, with horizontal line layout, and add a ImageView component and a TextView component in the layout manager, respectively, for displaying icons and text in list items
(2) Mainactivity.xml
1<?xml version="1.0"encoding="Utf-8"?>2<linearlayout xmlns:android="http://schemas.android.com/apk/res/android"3Android:layout_width="match_parent"4android:layout_height="match_parent"5android:orientation="Vertical">6 7<ListView8Android:id="@+id/listview1"9Android:layout_width="match_parent"Tenandroid:layout_height="wrap_content"> One</ListView> A -</LinearLayout>
Third, Androidmanifest.xml
1<manifest xmlns:android="http://schemas.android.com/apk/res/android"2Package="com.example.test"3Android:versioncode="1"4Android:versionname="1.0">5 6<uses-SDK7android:minsdkversion="8"8android:targetsdkversion=" -"/>9 Ten<Application Oneandroid:allowbackup="true" Aandroid:icon="@drawable/ic_launcher" -Android:label="@string/app_name" -Android:theme="@style/apptheme"> the<activity android:name="XQX. Mainactivity"> --<intent-filter> -<action android:name="Android.intent.action.MAIN"/> -<category android:name="Android.intent.category.LAUNCHER"/> +</intent-filter> -</activity> +</application> A at</manifest>
List of Android development icons