Main. xml main layout file, code
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" > <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/hello" /> <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" > <TextView android:text="@string/name" android:gravity="center" android:layout_width="150px" android:layout_height="wrap_content" /> <TextView android:text="@string/age" android:gravity="center" android:layout_width="170px" android:layout_height="wrap_content" /> </LinearLayout> <ListView android:id="@+id/listView" android:layout_width="wrap_content" android:layout_height="wrap_content" /> </LinearLayout>
User. xml component layout File Code
<? XML version = "1.0" encoding = "UTF-8"?> <! -- Create a component to store a row of data --> <tablelayout xmlns: Android = "http://schemas.android.com/apk/res/android" Android: layout_width = "fill_parent" Android: layout_height = "wrap_content"> <tablerow> <imageview Android: id = "@ + ID/image" Android: layout_width = "50px" Android: layout_height = "50px"> </imageview> <textview Android: id = "@ + ID/username" Android: gravity = "center" Android: layout_height = "wrap_content" Android: layout_width = "150px"> </textview> <textview Android: id = "@ + ID/userage" Android: gravity = "center" Android: layout_height = "wrap_content" Android: layout_width = "170px"> </textview> </tablerow> </tablelayout>
Main activity, listview. Java code
Package cn.com. android. listview; 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. simpleadapter; public class listview extends activity {@ override public void oncreate (bundle savedinstancestate) {super. oncreate (savedinstancestate); setconte Ntview (R. layout. Main); listview = (listview) findviewbyid (R. Id. listview);/* If there are multiple parameters, some people will be dizzy. The meaning of each parameter is described here. * The first parameter This represents the current context, which can be understood as your current activity * The second parameter getdata () is a list containing data, note that the list must contain map objects. The restriction in simpleadapter is this list <? Extends Map <string,?> Data * third parameter R. layout. User component that displays information * Fourth parameter is a string array. The Array Memory contains the key in the map where you store data. * Fifth parameter: an int array. The Array Memory is the position where each data is displayed in your display information component, one-to-one correspondence with the fourth parameter **/simpleadapter adapter = new simpleadapter (this, getdata (), R. layout. user, new string [] {"image", "username", "userage"}, new int [] {R. id. image, R. id. username, R. id. userage}); listview. setadapter (adapter);}/*** @ author chenzheng_java * @ description prepare some test data * @ return a hashmap set containing data information */private arraylist
Strings. XML Code
<? XML version = "1.0" encoding = "UTF-8"?> <Resources> <string name = "hello"> layout list display </string> <string name = "app_name"> List layout </string> <string name = "name"> name </string> <string name = "Age"> age </string> </resources>
Continuous nonsense:
Let's take a look at the structure, a main layout file, a component layout file, and an activity class.
There are three steps:
Step 1: Define the layout file and design the UI, including searching for suitable images ......
Step 2: obtain data. Simpleadapter is used here, so the data must be in a fixed format
Step 3: bind a data source
Then we can see what we want.