android ListView布局之二(是用simpleAdapter绑定数据)

http://blog.youkuaiyun.com/chenzheng_java/article/details/6200536

最终效果图

 

 

目录结构

 

main.xml主布局文件,代码

[xhtml]  view plain copy
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:orientation="vertical"  
  4.     android:layout_width="fill_parent"  
  5.     android:layout_height="fill_parent"  
  6.     >  
  7. <TextView    
  8.     android:layout_width="fill_parent"   
  9.     android:layout_height="wrap_content"   
  10.     android:text="@string/hello"  
  11. />  
  12.     <LinearLayout  
  13.     android:layout_width="wrap_content"  
  14.     android:layout_height="wrap_content" >  
  15.     <TextView  
  16.         android:text="@string/name"  
  17.         android:gravity="center"  
  18.          android:layout_width="150px"  
  19.         android:layout_height="wrap_content"  
  20.     />  
  21.       
  22.     <TextView  
  23.         android:text="@string/age"  
  24.         android:gravity="center"  
  25.          android:layout_width="170px"  
  26.         android:layout_height="wrap_content"  
  27.     />  
  28.       
  29.     </LinearLayout>  
  30.     <ListView  
  31.     android:id="@+id/listView"  
  32.     android:layout_width="wrap_content"  
  33.     android:layout_height="wrap_content"  
  34.     />  
  35.       
  36.       
  37. </LinearLayout>  

 

user.xml组件布局文件代码

[xhtml]  view plain copy
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <!-- 创建存放一行数据的组件 -->  
  3. <TableLayout  
  4.   xmlns:android="http://schemas.android.com/apk/res/android"  
  5.   android:layout_width="fill_parent"  
  6.   android:layout_height="wrap_content">  
  7.  <TableRow>  
  8.    
  9.   <ImageView  
  10.     android:id="@+id/image"  
  11.     android:layout_width="50px"      
  12.     android:layout_height="50px"   
  13.   ></ImageView>  
  14.     
  15.   <TextView  
  16.     android:id="@+id/userName"  
  17.     android:gravity="center"  
  18.     android:layout_height="wrap_content"  
  19.     android:layout_width="150px"  
  20.   ></TextView>  
  21.     
  22.   <TextView  
  23.     android:id="@+id/userAge"  
  24.     android:gravity="center"  
  25.     android:layout_height="wrap_content"  
  26.     android:layout_width="170px"  
  27.   ></TextView>  
  28.     
  29.   </TableRow>  
  30. </TableLayout>  

 

 

主Activity,listView.java代码

[java]  view plain copy
  1. package cn.com.android.listView;  
  2.   
  3. import java.util.ArrayList;  
  4. import java.util.HashMap;  
  5. import java.util.List;  
  6. import java.util.Map;  
  7.   
  8. import android.app.Activity;  
  9. import android.os.Bundle;  
  10. import android.widget.ListView;  
  11. import android.widget.SimpleAdapter;  
  12.   
  13. public class listView extends Activity {  
  14.     @Override  
  15.     public void onCreate(Bundle savedInstanceState) {  
  16.         super.onCreate(savedInstanceState);  
  17.         setContentView(R.layout.main);  
  18.         ListView listView = (ListView) findViewById(R.id.listView);  
  19.         /* 参数一多,有些人就头晕了。这里解说下,各个参数的意思。 
  20.          * 第一个参数 this 代表的是当前上下文,可以理解为你当前所处的activity 
  21.          * 第二个参数 getData() 一个包含了数据的List,注意这个List里存放的必须是map对象。simpleAdapter中的限制是这样的List<? extends Map<String, ?>> data 
  22.          * 第三个参数 R.layout.user 展示信息的组件 
  23.          * 第四个参数 一个string数组,数组内存放的是你存放数据的map里面的key。 
  24.          * 第五个参数:一个int数组,数组内存放的是你展示信息组件中,每个数据的具体展示位置,与第四个参数一一对应 
  25.          * */  
  26.         SimpleAdapter adapter = new SimpleAdapter(this, getData(), R.layout.user,  
  27.                 new String[]{"image","userName","userAge"}, new int[]{R.id.image,R.id.userName,R.id.userAge});  
  28.         listView.setAdapter(adapter);  
  29.           
  30.     }  
  31.       
  32.       
  33.       
  34.     /** 
  35.      * @author chenzheng_java 
  36.      * @description 准备一些测试数据 
  37.      * @return 一个包含了数据信息的hashMap集合 
  38.      */  
  39.     private ArrayList<HashMap<String, Object>> getData(){  
  40.         ArrayList<HashMap<String, Object>> arrayList = new ArrayList<HashMap<String,Object>>();  
  41.         for(int i=0;i<10;i++){  
  42.             HashMap<String, Object> tempHashMap = new HashMap<String, Object>();  
  43.             tempHashMap.put("image", R.drawable.icon);  
  44.             tempHashMap.put("userName""用户"+i);  
  45.             tempHashMap.put("userAge"30-i);  
  46.             arrayList.add(tempHashMap);  
  47.               
  48.         }  
  49.           
  50.           
  51.         return arrayList;  
  52.           
  53.     }  
  54.       
  55.       
  56. }  

strings.xml代码

[xhtml]  view plain copy
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <resources>  
  3.     <string name="hello">布局列表展示</string>  
  4.     <string name="app_name">列表布局</string>  
  5.     <string name="name">姓名</string>  
  6.     <string name="age">年龄</string>  
  7. </resources>  

 

废话连绵:

我们一起看看结构,一个主布局文件,一个组件布局文件,一个Activity类。

依旧分为三步:

第一步:定义布局文件,设计UI,包括寻找合适的图片了等等……

第二步:获取数据。这里用的是simpleAdapter,所以要求数据必须固定格式的

第三步:绑定数据源

然后,我们就可以看到我们想要的结果了。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值