http://blog.youkuaiyun.com/chenzheng_java/article/details/6200536
最终效果图
目录结构
main.xml主布局文件,代码
- <?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组件布局文件代码
- <?xml version="1.0" encoding="utf-8"?>
- <!-- 创建存放一行数据的组件 -->
- <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>
主Activity,listView.java代码
- 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);
- setContentView(R.layout.main);
- ListView listView = (ListView) findViewById(R.id.listView);
- /* 参数一多,有些人就头晕了。这里解说下,各个参数的意思。
- * 第一个参数 this 代表的是当前上下文,可以理解为你当前所处的activity
- * 第二个参数 getData() 一个包含了数据的List,注意这个List里存放的必须是map对象。simpleAdapter中的限制是这样的List<? extends Map<String, ?>> data
- * 第三个参数 R.layout.user 展示信息的组件
- * 第四个参数 一个string数组,数组内存放的是你存放数据的map里面的key。
- * 第五个参数:一个int数组,数组内存放的是你展示信息组件中,每个数据的具体展示位置,与第四个参数一一对应
- * */
- 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 准备一些测试数据
- * @return 一个包含了数据信息的hashMap集合
- */
- private ArrayList<HashMap<String, Object>> getData(){
- ArrayList<HashMap<String, Object>> arrayList = new ArrayList<HashMap<String,Object>>();
- for(int i=0;i<10;i++){
- HashMap<String, Object> tempHashMap = new HashMap<String, Object>();
- tempHashMap.put("image", R.drawable.icon);
- tempHashMap.put("userName", "用户"+i);
- tempHashMap.put("userAge", 30-i);
- arrayList.add(tempHashMap);
- }
- return arrayList;
- }
- }
strings.xml代码
- <?xml version="1.0" encoding="utf-8"?>
- <resources>
- <string name="hello">布局列表展示</string>
- <string name="app_name">列表布局</string>
- <string name="name">姓名</string>
- <string name="age">年龄</string>
- </resources>
废话连绵:
我们一起看看结构,一个主布局文件,一个组件布局文件,一个Activity类。
依旧分为三步:
第一步:定义布局文件,设计UI,包括寻找合适的图片了等等……
第二步:获取数据。这里用的是simpleAdapter,所以要求数据必须固定格式的
第三步:绑定数据源
然后,我们就可以看到我们想要的结果了。