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

[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);

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