【Android】以SimpleAdapter做适配器的ListView和GridView

SimpleAdapter介绍

 

SimpleAdapter是一个简单的适配器,可以将静态数据映射到XML文件中定义好的视图。

构造函数

[java] view plaincopy
 
  1. public SimpleAdapter (Context context, List<? extends Map<String, ?>> data, int resource, String[] from, int[] to)  

参数

  context  SimpleAdapter关联的View的运行环境

  data    一个Map组成的List。在列表中的每个条目对应列表中的一行,每一个map中应该包含所有在from参数中指定的键

  resource   一个定义列表项的布局文件的资源ID。布局文件将至少应包含那些在to中定义了的ID

  from         一个将被添加到Map映射上的键名

  to     将绑定数据的视图的ID,跟from参数对应,这些应该全是TextView

ListView

Java类

[java] view plaincopy
 
  1. package com.app.test01;  
  2.   
  3. import java.util.ArrayList;  
  4. import java.util.HashMap;  
  5. import java.util.List;  
  6.   
  7. import android.app.Activity;  
  8. import android.os.Bundle;  
  9. import android.widget.ListView;  
  10. import android.widget.SimpleAdapter;  
  11.   
  12. public class ListViewSimple extends Activity{  
  13.     ListView listView1;  
  14.     @Override  
  15.     protected void onCreate(Bundle savedInstanceState) {  
  16.         // TODO Auto-generated method stub  
  17.         super.onCreate(savedInstanceState);  
  18.         setContentView(R.layout.activity_weixin);  
  19.           
  20.         listView1 = (ListView) findViewById(R.id.listView1);  
  21.         String[] strings = {"img","title","info","time"};//Map的key集合数组  
  22.         int[] ids = {R.id.img,R.id.title,R.id.info,R.id.time};//对应布局文件的id  
  23.         SimpleAdapter simpleAdapter = new SimpleAdapter(this,   
  24.                 getData(), R.layout.activity_weixin_item, strings, ids);  
  25.         listView1.setAdapter(simpleAdapter);//绑定适配器  
  26.     }  
  27.     // 初始化一个List  
  28.     private List<HashMap<String, Object>> getData() {  
  29.         // 新建一个集合类,用于存放多条数据  
  30.         ArrayList<HashMap<String, Object>> list = new ArrayList<HashMap<String, Object>>();  
  31.         HashMap<String, Object> map = null;  
  32.         for (int i = 1; i <= 40; i++) {  
  33.             map = new HashMap<String, Object>();  
  34.             map.put("title""人物" + i);  
  35.             map.put("time""9月20日");  
  36.             map.put("info""我通过了你的好友验证请求,现在我们可以开始对话啦");  
  37.             map.put("img", R.drawable.special_spring_head2);  
  38.             list.add(map);  
  39.         }  
  40.         return list;  
  41.     }  
  42. }  

主视图布局

[html] view plaincopy
 
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <!-- 这是范例ListView的布局文件,出了ListView,还可以放置其他控件 -->  
  3. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  4.     android:layout_width="match_parent"  
  5.     android:layout_height="fill_parent"  
  6.     android:background="#fff"  
  7.     android:orientation="vertical" >  
  8.     <RelativeLayout  
  9.         android:id="@+id/relativeLayout1"  
  10.         android:layout_width="match_parent"  
  11.         android:layout_height="50dp"  
  12.         android:layout_alignParentLeft="true"  
  13.         android:layout_alignParentTop="true"  
  14.         android:background="#2B3439" >  
  15.   
  16.         <TextView  
  17.             android:id="@+id/textView1"  
  18.             android:layout_width="wrap_content"  
  19.             android:layout_height="wrap_content"  
  20.             android:layout_centerHorizontal="true"  
  21.             android:layout_centerVertical="true"  
  22.             android:text="微信"  
  23.             android:textColor="#fff"  
  24.             android:textSize="22sp" />  
  25.     </RelativeLayout>  
  26.       
  27.     <ListView  
  28.         android:id="@+id/listView1"  
  29.         android:layout_width="match_parent"  
  30.         android:paddingTop="60dp"  
  31.         android:paddingBottom="50dp"  
  32.         android:cacheColorHint="#00000000"    
  33.         android:layout_height="match_parent"  
  34.         android:stackFromBottom="true"   
  35.         android:transcriptMode="alwaysScroll"  >  
  36.     </ListView>  
  37.   
  38. </RelativeLayout>  

子视图布局

[html] view plaincopy
 
  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="60dp"  
  6.     android:layout_gravity="center_vertical"  
  7.     android:orientation="horizontal"  
  8.     android:padding="5dp" >  
  9.   
  10.     <ImageView  
  11.         android:id="@+id/img"  
  12.         android:layout_width="wrap_content"  
  13.         android:layout_height="wrap_content"  
  14.         android:layout_alignParentLeft="true"  
  15.         android:layout_alignParentTop="true"  
  16.         android:layout_centerVertical="true"  
  17.         android:padding="5dp"  
  18.         android:src="@drawable/gong1" />  
  19.   
  20.     <RelativeLayout  
  21.         android:id="@+id/relativeLayout1"  
  22.         android:layout_width="match_parent"  
  23.         android:layout_height="match_parent"  
  24.         android:layout_centerVertical="true"  
  25.         android:layout_marginLeft="5dp"  
  26.         android:layout_marginRight="70dp"  
  27.         android:layout_toRightOf="@+id/img" >  
  28.   
  29.         <TextView  
  30.             android:id="@+id/title"  
  31.             android:layout_width="wrap_content"  
  32.             android:layout_height="wrap_content"  
  33.             android:layout_alignParentLeft="true"  
  34.             android:layout_alignParentTop="true"  
  35.             android:text="TextView"   
  36.             android:textColor="#000"  
  37.             android:textSize="18sp"/>  
  38.   
  39.         <TextView  
  40.             android:id="@+id/info"  
  41.             android:layout_width="match_parent"  
  42.             android:layout_height="wrap_content"  
  43.             android:layout_alignParentBottom="true"  
  44.             android:layout_alignParentLeft="true"  
  45.             android:text="TextView"  
  46.             android:singleLine="true"  
  47.             android:ellipsize="end"  
  48.             android:textColor="#ccc"  
  49.             android:textSize="15sp"  />  
  50.     </RelativeLayout>  
  51.   
  52.     <TextView  
  53.         android:id="@+id/time"  
  54.         android:layout_width="wrap_content"  
  55.         android:layout_height="wrap_content"  
  56.         android:layout_alignTop="@+id/relativeLayout1"  
  57.         android:layout_alignParentRight="true"  
  58.         android:layout_marginRight="10dp"  
  59.         android:text="TextView"   
  60.         android:textColor="#ccc"  
  61.         android:textSize="15sp"/>  
  62.   
  63. </RelativeLayout>  

效果图

 

GridView

Java类

[java] view plaincopy
 
  1. package com.app.test01;  
  2.   
  3. import java.util.ArrayList;  
  4. import java.util.HashMap;  
  5. import java.util.Map;  
  6.   
  7. import android.app.Activity;  
  8. import android.os.Bundle;  
  9. import android.widget.GridView;  
  10. import android.widget.SimpleAdapter;  
  11.   
  12. public class GridViewSimple extends Activity {  
  13.     private GridView gridView1;  
  14.     private int[] ids = { R.drawable.gong1, R.drawable.gong2, R.drawable.gong3,  
  15.             R.drawable.gong4, R.drawable.gong5, R.drawable.gong6,  
  16.             R.drawable.gong7, R.drawable.gong8, R.drawable.gong9,  
  17.             R.drawable.gong1, R.drawable.gong2, R.drawable.gong3,  
  18.             R.drawable.gong4, R.drawable.gong5, R.drawable.gong6,  
  19.             R.drawable.gong7, R.drawable.gong8, R.drawable.gong9 };  
  20.   
  21.     @Override  
  22.     protected void onCreate(Bundle savedInstanceState) {  
  23.         // TODO Auto-generated method stub  
  24.         super.onCreate(savedInstanceState);  
  25.         setContentView(R.layout.gridview);  
  26.         gridView1 = (GridView) findViewById(R.id.gridView1);  
  27.   
  28.         ArrayList<Map<String, Object>> arrayList = new ArrayList<Map<String, Object>>();  
  29.         for (int i = 0; i < ids.length; i++) {  
  30.             Map<String, Object> map = new HashMap<String, Object>();  
  31.             map.put("image", ids[i]);  
  32.             arrayList.add(map);  
  33.         }  
  34.   
  35.         SimpleAdapter simpleAdapter = new SimpleAdapter(this, arrayList,  
  36.                 R.layout.gridview_item, new java.lang.String[] { "image" },  
  37.                 new int[] { R.id.imageView1 });  
  38.         gridView1.setAdapter(simpleAdapter);  
  39.     }  
  40. }  

主视图布局

[html] view plaincopy
 
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:layout_width="match_parent"  
  4.     android:layout_height="match_parent"  
  5.     android:orientation="vertical" >  
  6.   
  7.     <GridView  
  8.         android:id="@+id/gridView1"  
  9.         android:layout_width="match_parent"  
  10.         android:layout_height="wrap_content"  
  11.         android:numColumns="3" >  
  12.     </GridView>  
  13.   
  14. </LinearLayout>  

子视图布局

[html] view plaincopy
 
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:layout_width="match_parent"  
  4.     android:layout_height="match_parent"  
  5.     android:orientation="vertical"  
  6.     android:gravity="center"   
  7.     android:paddingTop="10dp"  
  8.     android:paddingBottom="10dp">  
  9.   
  10.     <ImageView  
  11.         android:id="@+id/imageView1"  
  12.         android:layout_width="wrap_content"  
  13.         android:layout_height="wrap_content"  
  14.         android:src="@drawable/gong1" />  
  15.   
  16.     <TextView  
  17.         android:id="@+id/textView1"  
  18.         android:layout_width="wrap_content"  
  19.         android:layout_height="wrap_content"  
  20.         android:text="TextView" />  
  21.   
  22. </LinearLayout>  

效果图

posted on 2014-01-07 09:58  duanxz  阅读(1420)  评论(0编辑  收藏  举报