现在的位置: 首页 > 综合 > 正文

Android-SimpleAdapter用法学习

2017年12月18日 ⁄ 综合 ⁄ 共 6006字 ⁄ 字号 评论关闭

下面介绍下SimpleAdapter的相关知识

以下是官方网上对SimpleAdapter的介绍:

An easy adapter to map static data to views defined in an XML file. You can specify the data backing the list as an ArrayList of MapsEach entry in the ArrayList corresponds to one row in
the list. The Maps contain the data for each row. You also specify an XML file that defines the views used to display the row, and a mapping from keys in the Map to specific views.

Binding data to views occurs in two phases.

First, if a SimpleAdapter.ViewBinder is available, setViewValue(android.view.View, Object, String) is invoked.

If the returned value is true, binding has occurred.

If the returned value is false, the following views are then tried in order: 
 (1)A view that implements Checkable (e.g. CheckBox). The expected bind value is a boolean. 
 (2)TextView. The expected bind value is a string and setViewText(TextView, String) is invoked. 
 (3)ImageView. The expected bind value is a resource id or a string and setViewImage(ImageView, int) or setViewImage(ImageView, String) is invoked. 
  If no appropriate binding can be found, an IllegalStateException is thrown.

(大概中文解释:适配器,将静态数据映射到在xml文件中定义的视图上,指定数据列表。每个ArrayList中得一项对应在ListView中的一行。你能自定一个Layout布局文件通过关键字映射来显示数据。

绑定数据分为两个阶段:

首先,如果一个SimpleAdapter.ViewBinder是有效的,setViewValue(android.view.View, Object, String)将被调用;

否则,如果返回失败,则会尝试以下的几个步骤。。。。。)


上代码

  1. package com.potato;  
  2.   
  3. import java.util.ArrayList;  
  4. import java.util.HashMap;  
  5. import java.util.Map;  
  6.   
  7. import android.app.ListActivity;  
  8. import android.os.Bundle;  
  9. import android.view.View;  
  10. import android.widget.AdapterView;  
  11. import android.widget.AdapterView.OnItemClickListener;  
  12. import android.widget.ListView;  
  13. import android.widget.SimpleAdapter;  
  14. import android.widget.Toast;  
  15.   
  16. public class ListViewSimpleAdapterDemoActivity extends ListActivity  
  17. {  
  18.     private String[]                mListTitleStr   = { "姓名", "性别", "年龄", "居住地" };  
  19.     private String[]                mListStr        = { "GeXiao", "male","secret", "北京" };  
  20.   
  21.     public ListView                     mListView       = null;  
  22.     ArrayList<Map<String, Object>>  mDataArrList    = new ArrayList<Map<String, Object>>(); ;  
  23.     public SimpleAdapter mAdapter;  
  24.     /** Called when the activity is first created. */  
  25.     @Override  
  26.     protected void onCreate(Bundle savedInstanceState)  
  27.     {  
  28.         super.onCreate(savedInstanceState);  
  29.         // 获取ListView  
  30.         mListView = getListView();// 注1  
  31.   
  32.         // 数据  
  33.         int lengh = mListTitleStr.length;  
  34.         for (int i = 0; i < lengh; i++)  
  35.         {  
  36.             Map<String, Object> item = new HashMap<String, Object>();  
  37.             item.put("user_state", R.drawable.ic_launcher);  
  38.             item.put("title", mListTitleStr[i]);  
  39.             item.put("text", mListStr[i]);  
  40.             item.put("user", R.drawable.ic_launcher);  
  41.             mDataArrList.add(item);  
  42.         }  
  43.           
  44.         // ListView与数据相连接  
  45.         mAdapter = new SimpleAdapter(this, mDataArrList,  
  46.                         R.layout.list_item,   
  47.                         new String[] { "user_state", "title", "text" , "user"},   
  48.                         new int[] { R.id.img_status, R.id.tv_name, R.id.tv_phone_number, R.id.img_user });  // 注2  
  49.         setListAdapter(mAdapter);  
  50.           
  51.         // listview的item的点击事件响应  
  52.         mListView.setOnItemClickListener(new OnItemClickListener()  
  53.         {  
  54.             @Override  
  55.             public void onItemClick(AdapterView<?> adapterView, View view,  
  56.                     int position, long id)  
  57.             {  
  58.                 // 获取点击的View            // 注3  
  59.                 View view1 = ListViewSimpleAdapterDemoActivity.this.mAdapter.getDropDownView(position, null, null);  
  60.                 Toast toast = new Toast(ListViewSimpleAdapterDemoActivity.this);  
  61.                 toast.setView(view1);  
  62.                 toast.show();  
  63.             }  
  64.         });  
  65.     }  
  66.   
  67. }  

注2:

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

参数

context 关联SimpleAdapter运行着的视图的上下文。
data 一个Map的列表。在列表中的每个条目对应列表中的一行,应该包含所有在from中指定的条目
resource 一个定义列表项目的视图布局的资源唯一标识。布局文件将至少应包含哪些在to中定义了的名称。
from 一个将被添加到Map上关联每一个项目的列名称的列表
to 应该在参数from显示列的视图。这些应该全是TextView。在列表中最初的N视图是从参数from中最初的N列获取的值。

注3:

显示数据,通过getDropDownView获取要显示的数据。。。。

  1. View view1 = ListViewSimpleAdapterDemoActivity.this.mAdapter.getDropDownView(position, null, null);  

list_item.xml(ListView每个Item显示布局)

  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="wrap_content"  
  6.     android:background="#FFFFFF">  
  7.     <ImageView   
  8.         android:id="@+id/img_status"  
  9.         android:layout_width="wrap_content"   
  10.         android:layout_height="fill_parent"  
  11.         android:adjustViewBounds="true"  
  12.         android:padding="2dip" />  
  13.     <TextView   
  14.         android:id="@+id/tv_name"  
  15.         android:layout_width="wrap_content"  
  16.         android:layout_height="wrap_content"  
  17.         android:gravity="top"  
  18.         android:textStyle="bold"  
  19.         android:textSize="20px"  
  20.         android:layout_toRightOf="@id/img_status"  
  21.         android:padding="2px"  
  22.         android:singleLine="true"  
  23.     />  
  24.     <TextView   
  25.         android:id="@+id/tv_phone_number"  
  26.         android:layout_width="wrap_content"  
  27.         android:layout_height="wrap_content"  
  28.         android:gravity="top"  
  29.         android:textStyle="bold"  
  30.         android:textSize="20px"  
  31.         android:textColor="#000000"  
  32.         android:layout_toLeftOf="@+id/img_user"  
  33.         android:layout_alignParentBottom="true"  
  34.         android:padding="2px"  
  35.         android:singleLine="true"  
  36.     />  
  37.     <ImageView android:id="@id/img_user"  
  38.         android:layout_width="wrap_content"   
  39.         android:layout_height="fill_parent"  
  40.         android:layout_alignParentTop="true"   
  41.         android:layout_alignParentBottom="true"  
  42.         android:layout_alignParentRight="true"  
  43.         android:adjustViewBounds="true"  
  44.         android:padding="2dip" />  
  45. </RelativeLayout>  

转自:SimpleAdapterhttp://blog.csdn.net/alex0203/article/details/6939225

抱歉!评论已关闭.