[Android] Adapter:SimpleAdapter SimpleCursorAdapter ArrayAdapter 与ListView的用法

 

AdapterView: ListView GridView Gallery Spinner

Adapter: SimpleAdapter SimpleCursorAdapter ArrayAdapter

 

[功能]

* AdapterView: 由界面决定用哪一种

* Adapter : 由数据形式决定用哪一种

 

AdapterView 没什么可说的 界面是人各有志 看自己的需要吧 所以今天主要介绍一下 Adapter 的使用

 

 

[前提]

因为与界面无关 所以为方便 界面统一使用 ListView 且:

Java代码   
  1. ListView  lv = (ListView) findViewById(R.id.list);  
ListView  lv = (ListView) findViewById(R.id.list);

 

Java代码   
  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. <ListView     
  8.     android:id="@+id/list"  
  9.     android:layout_width="fill_parent"    
  10.     android:layout_height="wrap_content"    
  11.     />   
  12. </LinearLayout>  
<?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"
    >
<ListView  
	android:id="@+id/list"
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    />
</LinearLayout>

 

* 使用所有Adapter

Java代码   
  1. lv.setAdapter(adapter);  
lv.setAdapter(adapter);

 

 

以下逐一举例:

 

[SimpleAdapter ]

 

* source code:

Java代码   
  1. public SimpleAdapter(Context context, List<? extends Map<String, ?>> data,   
  2.             int resource, String[] from, int[] to)  
public SimpleAdapter(Context context, List<? extends Map<String, ?>> data,
            int resource, String[] from, int[] to)

 

 

* sample

Java代码   
  1. public final static String COLUMN_1 = "name";   
  2. public final static String COLUMN_2 = "phone";   
  3.   
  4. List<Map<String,String>> display;   
  5.   
  6. String[] from = {COLUMN_1,COLUMN_2};   
  7.         int[] to = {android.R.id.text1,android.R.id.text2};   
  8.         SimpleAdapter adapter = new SimpleAdapter(this, display,android.R.layout.simple_list_item_2, from,to);  
public final static String COLUMN_1 = "name";
public final static String COLUMN_2 = "phone";

List<Map<String,String>> display;

String[] from = {COLUMN_1,COLUMN_2};
        int[] to = {android.R.id.text1,android.R.id.text2};
        SimpleAdapter adapter = new SimpleAdapter(this, display,android.R.layout.simple_list_item_2, from,to);

 

 

* 补充:

1. 数据源 display

Java代码   
  1. 1. 定义: List<Map<String,String>> display;   
  2.   
  3.   
  4. 2. 初始化: display = new ArrayList<Map<String,String>>();   
  5.   
  6.   
  7. 3. 使用: display = addValue();   
  8.   
  9. public List<Map<String,String>> addValue(){   
  10.         List<Map<String,String>> value = new ArrayList<Map<String,String>>();   
  11.            
  12.         Map<String,String> item1 = new HashMap<String,String>();   
  13.         item1.put(COLUMN_1, "griffin");   
  14.         item1.put(COLUMN_2, "132123");   
  15.         value.add(item1);   
  16.            
  17.         Map<String,String> item2 = new HashMap<String,String>();   
  18.         item2.put(COLUMN_1, "eoe.android");   
  19.         item2.put(COLUMN_2, "132");   
  20.         value.add(item2);   
  21.            
  22.         Map<String,String> item3 = new HashMap<String,String>();   
  23.         item3.put(COLUMN_1, "gryphone");   
  24.         item3.put(COLUMN_2, "132342");   
  25.         value.add(item3);   
  26.            
  27.         return value;   
  28.     }  
1. 定义: List<Map<String,String>> display;


2. 初始化: display = new ArrayList<Map<String,String>>();


3. 使用: display = addValue();

public List<Map<String,String>> addValue(){
    	List<Map<String,String>> value = new ArrayList<Map<String,String>>();
    	
    	Map<String,String> item1 = new HashMap<String,String>();
    	item1.put(COLUMN_1, "griffin");
    	item1.put(COLUMN_2, "132123");
    	value.add(item1);
    	
    	Map<String,String> item2 = new HashMap<String,String>();
    	item2.put(COLUMN_1, "eoe.android");
    	item2.put(COLUMN_2, "132");
    	value.add(item2);
    	
    	Map<String,String> item3 = new HashMap<String,String>();
    	item3.put(COLUMN_1, "gryphone");
    	item3.put(COLUMN_2, "132342");
    	value.add(item3);
    	
    	return value;
    }

 

 

 

 

[SimpleCursorAdapter]

 

* source code

Java代码   
  1. public SimpleCursorAdapter(Context context, int layout, Cursor c, String[] from, int[] to)  
public SimpleCursorAdapter(Context context, int layout, Cursor c, String[] from, int[] to)

 

* sample

Java代码   
  1. Cursor c = getContentResolver().query(People.CONTENT_URI,    
  2.                 nullnullnullnull);   
  3.            
  4.         String[] from ={People.NAME};   
  5.         int[] to = {android.R.id.text1};   
  6.         SimpleCursorAdapter adapter = new SimpleCursorAdapter(this,   
  7.                 android.R.layout.simple_list_item_1,c, from,to);   
Cursor c = getContentResolver().query(People.CONTENT_URI, 
                null, null, null, null);
        
        String[] from ={People.NAME};
        int[] to = {android.R.id.text1};
        SimpleCursorAdapter adapter = new SimpleCursorAdapter(this,
                android.R.layout.simple_list_item_1,c, from,to); 

 

 

 

[ArrayAdapter]

 

* source code

Java代码   
  1. public ArrayAdapter(Context context, int resource, int textViewResourceId, T[] objects)  
public ArrayAdapter(Context context, int resource, int textViewResourceId, T[] objects)

 

 

* sample

Java代码   
  1. String[] value = {   
  2.             "JAN","FEB","MAR","APR",   
  3.             "MAY","JUN","JUL","AUG","SEP","OCT","NOV","DEC "  
  4.     };   
  5.   
  6. ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,value)  
String[] value = {
			"JAN","FEB","MAR","APR",
            "MAY","JUN","JUL","AUG","SEP","OCT","NOV","DEC "
	};

ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,value)

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值