Android SimpleAdapter显示ListView、GridView

IT程序员开发必备-各类资源下载清单,史上最全IT资源,个人收藏总结!


SimpleAdapter作为一个数据集,主要向ListView、GridView容器填充数据,总结了几种填充方式,原理很简单,直接看代码和效果图


1、 ListView单行显示(simple_list_item_1)

代码:

  1. public class myListItem1 extends Activity{  
  2.     @Override  
  3.     public void onCreate(Bundle savedInstanceState){  
  4.         super.onCreate(savedInstanceState);  
  5.           
  6.         ListView listView = new ListView(this);  
  7.           
  8.         List<Map<String, String>> mList = new ArrayList<Map<String, String>>();  
  9.         for(int i=0; i<10; i++){  
  10.             Map<String, String> map = new HashMap<String, String>();  
  11.             map.put("TITLE""Test Title");  
  12.             map.put("CONTENT""Test Content");  
  13.             mList.add(map);  
  14.         }  
  15.           
  16.         SimpleAdapter adapter = new SimpleAdapter(this,  
  17.                                         mList,  
  18.                                         android.R.layout.simple_list_item_1,        // List 显示一行item1  
  19.                                         new String[]{ "CONTENT" },          // "TITLE",   
  20.                                         new int[]{ android.R.id.text1 }  
  21.                                         );  
  22.           
  23.         listView.setAdapter(adapter);  
  24.         setContentView(listView);  
  25.     }  
  26. }  

效果:



2、 ListView双行显示(simple_list_item_2)

代码:

  1. public class myListItem2 extends Activity{  
  2.     @Override  
  3.     public void onCreate(Bundle savedInstanceState){  
  4.         super.onCreate(savedInstanceState);  
  5.           
  6.         ListView listView = new ListView(this);  
  7.           
  8.         List<Map<String, String>> mList = new ArrayList<Map<String, String>>();  
  9.         for(int i=0; i<10; i++){  
  10.             Map<String, String> map = new HashMap<String, String>();  
  11.             map.put("TITLE""Test Title");  
  12.             map.put("CONTENT""Test Content");  
  13.             mList.add(map);  
  14.         }  
  15.           
  16.         SimpleAdapter adapter = new SimpleAdapter(this,  
  17.                                         mList,  
  18.                                         android.R.layout.simple_list_item_2,        // List 显示两行item1、item2  
  19.                                         new String[]{ "TITLE""CONTENT" },  
  20.                                         new int[]{ android.R.id.text1, android.R.id.text2 }  
  21.                                         );  
  22.           
  23.         listView.setAdapter(adapter);  
  24.         setContentView(listView);  
  25.     }  
  26. }  

效果:


3、 ListView自定义显示

代码:

  1. public class MyList extends Activity {  
  2.     @Override  
  3.     public void onCreate(Bundle savedInstanceState) {  
  4.         super.onCreate(savedInstanceState);  
  5.         setContentView(R.layout.mylist);  
  6.   
  7.         List<Map<String, Object>> mList = new ArrayList<Map<String, Object>>();  
  8.         for (int i = 0; i < 10; i++) {  
  9.             Map<String, Object> map = new HashMap<String, Object>();  
  10.             map.put("PIC", R.drawable.pic);     // 加载图片资源  
  11.             map.put("TITLE""Test Title");  
  12.             map.put("CONTENT""Test Content");  
  13.             mList.add(map);  
  14.         }  
  15.           
  16.         SimpleAdapter adapter = new SimpleAdapter(this,  
  17.                                         mList,  
  18.                                         R.layout.listitem,      // 自定义布局格式  
  19.                                         new String[] { "PIC""TITLE""CONTENT" },   
  20.                                         new int[] { R.id.listitem_pic, R.id.listitem_title, R.id.listitem_content }  
  21.                                         );  
  22.   
  23.         ListView listView = (ListView) findViewById(R.id.list);  
  24.         listView.setAdapter(adapter);  
  25.     }  
  26. }  
自定义的 listitem.xml
  1. <?xml version="1.0" encoding="utf-8"?>  
  2.   
  3. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  4.         android:layout_width="fill_parent"   
  5.         android:layout_height="?android:attr/listPreferredItemHeight">  
  6.           
  7.         <ImageView android:id="@+id/listitem_pic"  
  8.                 android:layout_width="wrap_content"   
  9.                 android:layout_height="fill_parent"  
  10.                 android:layout_alignParentTop="true"   
  11.                 android:layout_alignParentBottom="true"  
  12.                 android:adjustViewBounds="true"  
  13.                 android:padding="10dip" />  
  14.           
  15.         <TextView android:id="@+id/listitem_title"  
  16.                 android:layout_width="wrap_content"   
  17.                 android:layout_height="wrap_content"  
  18.                 android:layout_toRightOf="@id/listitem_pic"  
  19.                 android:layout_alignParentRight="true"   
  20.                 android:layout_alignParentTop="true"  
  21.                 android:layout_alignWithParentIfMissing="true"   
  22.                 android:gravity="center_vertical"  
  23.                 android:textSize="22sp" />  
  24.           
  25.         <TextView android:id="@+id/listitem_content"  
  26.                 android:layout_width="fill_parent"   
  27.                 android:layout_height="wrap_content"  
  28.                 android:layout_toRightOf="@id/listitem_pic"  
  29.                 android:layout_alignParentBottom="true"  
  30.                 android:layout_alignParentRight="true"   
  31.                 android:layout_below="@id/listitem_title"  
  32.                 android:singleLine="true"  
  33.                 android:ellipsize="marquee"   
  34.                 android:textSize="14sp" />  
  35. </RelativeLayout>  

效果:



4、 GridView自定义显示

代码:

  1. public class MyGrid extends Activity {  
  2.     @Override  
  3.     public void onCreate(Bundle savedInstanceState) {  
  4.         super.onCreate(savedInstanceState);  
  5.         setContentView(R.layout.mygrid);  
  6.   
  7.         List<Map<String, Object>> mList = new ArrayList<Map<String, Object>>();  
  8.         for (int i = 0; i < 10; i++) {  
  9.             Map<String, Object> map = new HashMap<String, Object>();  
  10.             map.put("PIC", R.drawable.pic);     // 加载图片资源  
  11.             map.put("TITLE""Test Title");  
  12.             mList.add(map);  
  13.         }  
  14.         SimpleAdapter adapter = new SimpleAdapter(this,  
  15.                                         mList, R.layout.griditem,       // 自定义布局格式  
  16.                                         new String[] { "PIC""TITLE" },   
  17.                                         new int[] { R.id.griditem_pic, R.id.griditem_title, }  
  18.                                         );  
  19.   
  20.         GridView gridView = (GridView) findViewById(R.id.grid);  
  21.         gridView.setAdapter(adapter);  
  22.     }  
  23. }  
自定义的 gridview.xml
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:layout_height="fill_parent"   
  4.     android:layout_width="fill_parent"  
  5.     android:orientation="vertical">  
  6.       
  7.     <ImageView android:id="@+id/griditem_pic"  
  8.         android:layout_width="wrap_content"   
  9.         android:layout_height="wrap_content"  
  10.         android:layout_gravity="center_horizontal">  
  11.     </ImageView>  
  12.       
  13.     <TextView android:id="@+id/griditem_title"  
  14.         android:layout_width="wrap_content"   
  15.         android:layout_height="wrap_content"  
  16.         android:layout_gravity="center_horizontal">  
  17.     </TextView>  
  18. </LinearLayout>  

效果:




源码下载


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值