[Android] Adapter:SimpleAdapter SimpleCursorAdapter ArrayAdapter 与ListView的用法
2011-11-08 16:34
369 查看
AdapterView: ListView GridView Gallery Spinner
Adapter: SimpleAdapter SimpleCursorAdapter ArrayAdapter
[功能]
* AdapterView: 由界面决定用哪一种
* Adapter : 由数据形式决定用哪一种
AdapterView 没什么可说的 界面是人各有志 看自己的需要吧 所以今天主要介绍一下 Adapter 的使用
[前提]
因为与界面无关 所以为方便 界面统一使用 ListView 且:
Java代码
ListView lv = (ListView) findViewById(R.id.list);
Java代码
<?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代码
lv.setAdapter(adapter);
以下逐一举例:
[SimpleAdapter ]
* source code:
Java代码
public SimpleAdapter(Context context, List<? extends Map<String, ?>> data, int resource, String[] from, int[] to)
* 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)
* 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)
* 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)
Adapter: SimpleAdapter SimpleCursorAdapter ArrayAdapter
[功能]
* AdapterView: 由界面决定用哪一种
* Adapter : 由数据形式决定用哪一种
AdapterView 没什么可说的 界面是人各有志 看自己的需要吧 所以今天主要介绍一下 Adapter 的使用
[前提]
因为与界面无关 所以为方便 界面统一使用 ListView 且:
Java代码
ListView lv = (ListView) findViewById(R.id.list);
ListView lv = (ListView) findViewById(R.id.list);
Java代码
<?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>
<?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代码
lv.setAdapter(adapter);
lv.setAdapter(adapter);
以下逐一举例:
[SimpleAdapter ]
* source code:
Java代码
public SimpleAdapter(Context context, List<? extends Map<String, ?>> data, int resource, String[] from, int[] to)
public SimpleAdapter(Context context, List<? extends Map<String, ?>> data, int resource, String[] from, int[] to)
* 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);
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; }
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)
public SimpleCursorAdapter(Context context, int layout, Cursor c, String[] from, int[] to)
* 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);
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)
public ArrayAdapter(Context context, int resource, int textViewResourceId, T[] objects)
* 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)
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)
相关文章推荐
- Android ListView 最基本的用法,使用SimpleCursorAdapter 。 附例子。
- Android train——ListView绑定ArrayAdapter、SimpleAdapter、SimpleCursorAdapter、BaseAdapter
- Android SimpleAdapter,SimpleCursorAdapter,ArrayAdapter的区别
- Android列表视图(ListView--SimpleCursorAdapter)学习
- android的adapter:ArrayAdapter,SimpleAdapter,SimpleCursorAdapter
- android 数据库存储+listView+SimpleCursorAdapter 范例demo(亲测)
- Android 常用Adapter(ArrayAdapter ,SimpleAdapter ,BaseAdapter )的具体用法
- android中SimpleAdapter、ArrayAdapter的用法以及继承了ListActivity的设置适配器的方法
- Android适配器之ArrayAdapter、SimpleAdapter和BaseAdapter的简单用法与有用代码片段
- Android适配器之ArrayAdapter、SimpleAdapter和BaseAdapter的简单用法与有用代码片段(转)
- Android---UI篇---ListView之SimpleCursorAdapter(列表)---3
- Android适配器之ArrayAdapter、SimpleAdapter和BaseAdapter的简单用法与有用代码片段
- Android新手入门2016(9)--ListView之SimpleAdapter和SimpleCursorAdapter
- androidUI第四部分--1.4.1ListView用法—利用ArrayAdapter生成ListView
- Android 使用SimpleCursorAdapter为listview的item添加按钮
- android ListView使用的三种方法汇总(通过继承BaseAdapter,使用SimpleAdapter和SimpleCursorAdapter)
- 【android】ListView+SimpleCursorAdapter+checkbox实现批量删除
- androidUI第四部分---1.4.2ListView用法—利用SimpleAdapter生成ListView
- android——ListView(使用simpleCursorAdapter)
- Android-ListView详解(SimpleAdapter,ArrayAdapter,BaseAdapter,OnItenClickListener,OnScrollListener)