您的位置:首页 > 移动开发 > Android开发

Android学习之Adapter:SimpleAdapter,SimpleCursorAdapter,ArrayAdapter(数据库 列表)

2010-07-08 17:53 387 查看
AdapterView: ListView GridView Gallery Spinner

Adapter: SimpleAdapter SimpleCursorAdapter ArrayAdapter

[功能]

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

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

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

[前提]

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

Java代码:

ListView  lv = (ListView) findViewById(R.id.list);
[/code]

xml代码:

<?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>
[/code]

* 使用所有Adapter

Java代码:
lv.setAdapter(adapter);


以下逐一举例:

(一) [SimpleAdapter ]

* source code:

Java代码:

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

* sample

Java代码

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. 定义: 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代码

public SimpleCursorAdapter(Context context, int layout, Cursor c, String[] from, int[] to)
[/code]

* sample

Java代码

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代码
public ArrayAdapter(Context context, int resource, int textViewResourceId, T[] objects)
[/code]

* sample

Java代码

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)


done!
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐