要將數據庫中的數據列表顯示在屏幕上,我們要使用ListView這個控件,當用戶從數據庫中取出數據時,要將數據綁定到顯示控件上,如何綁定呢,我們需要創建適配器進行綁定,創建適配器有兩種方式:
第一種是用SimpleAdapter創建(要求綁定的數據是List<HashMap<String, Object>>數據類型)
第二種是用SimpleCursorAdapter創建(要求綁定的數據是Cursor數據類型)
顯示效果如圖所示:
界面布局:
item.xml
- <?xml version="1.0" encoding="utf-8"?>
- <!--item -->
- <LinearLayout
- xmlns:Android="http://schemas.android.com/apk/res/android"
- android:orientation="horizontal"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent">
- <!-- 名稱 -->
- <TextView
- android:layout_width="130dp"
- android:layout_height="wrap_content"
- android:id="@+id/name"
- />
- <!-- 電話 -->
- <TextView
- android:layout_width="150dp"
- android:layout_height="wrap_content"
- android:id="@+id/phone"
- />
- <!-- 存款 -->
- <TextView
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:id="@+id/amount"
- />
- </LinearLayout>
main.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"
- >
- <!-- 標題 -->
- <LinearLayout
- android:orientation="horizontal"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content">
-
- <TextView
- android:layout_width="130dp"
- android:layout_height="wrap_content"
- android:text="姓名"
- />
-
- <TextView
- android:layout_width="150dp"
- android:layout_height="wrap_content"
- android:text="電話"
- />
-
- <TextView
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:text="存款"
- />
-
- </LinearLayout>
- <!-- ListView控件 -->
- <ListView
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- android:id="@+id/listView"
- />
- </LinearLayout>
使用SimpleAdapter進行數據綁定
- public class MainActivity extends Activity {
- private PersonService service;
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- service = new PersonService(this);
- ListView listView = (ListView) this.findViewById(R.id.listView);
-
- //獲取到集合數據
- List<Person> persons = service.getScrollData(0, 10);
- List<HashMap<String, Object>> data = new ArrayList<HashMap<String,Object>>();
- for(Person person : persons){
- HashMap<String, Object> item = new HashMap<String, Object>();
- item.put("id", person.getId());
- item.put("name", person.getName());
- item.put("phone", person.getPhone());
- item.put("amount", person.getAmount());
- data.add(item);
- }
- //創建SimpleAdapter適配器將數據綁定到item顯示控件上
- SimpleAdapter adapter = new SimpleAdapter(this, data, R.layout.item,
- new String[]{"name", "phone", "amount"}, new int[]{R.id.name, R.id.phone, R.id.amount});
- //實現列表的顯示
- listView.setAdapter(adapter);
- //條目點擊事件
- listView.setOnItemClickListener(new ItemClickListener());
- }
- //獲取點擊事件
- private final class ItemClickListener implements OnItemClickListener{
-
- public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
- ListView listView = (ListView) parent;
- HashMap<String, Object> data = (HashMap<String, Object>) listView.getItemAtPosition(position);
- String personid = data.get("id").toString();
- Toast.makeText(getApplicationContext(), personid, 1).show();
- }
- }
- }
使用SimpleCursorAdapter進行數據綁定
- public class MainActivity extends Activity {
- private PersonService service;
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- service = new PersonService(this);
- ListView listView = (ListView) this.findViewById(R.id.listView);
- //獲取游標
- Cursor cursor = service.getCursorScrollData(0, 10);
- //創建SimpleCursorAdapter適配器將數據綁定到item顯示控件上
- SimpleCursorAdapter adapter = new SimpleCursorAdapter(this, R.layout.item, cursor,
- new String[]{"name", "phone", "amount"}, new int[]{R.id.name, R.id.phone, R.id.amount});
- listView.setAdapter(adapter);
- //條目點擊事件
- listView.setOnItemClickListener(new ItemClickListener());
- }
-
- private final class ItemClickListener implements OnItemClickListener{
-
- public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
- ListView listView = (ListView) parent;
- Cursor cursor = (Cursor) listView.getItemAtPosition(position);
- String personid = String.valueOf(cursor.getInt(cursor.getColumnIndex("_id")));
- Toast.makeText(getApplicationContext(), personid, 1).show();
- }
- }
- }
注意:使用第二種方式在獲取數據集合時必須指定主鍵"_id"