ListView是類似于将一個界面分為一行一行,如下圖:
注意:listView.getItemAtPosition(int pos)内部調用了adapter.getItem(int position)方法,而每種擴充卡傳回的類型都是不一樣的:
當SimpleAdapter傳回Map<String,Object>
SimpleCursorAdapter傳回Cursor;
繼承BaseAdapter傳回自己實作的類型;
一般ListView都是用來顯示清單的,一般清單的資料都是來自資料庫的,是以我們這裡假設前面已經實作了一個DBService類,裡面存在pageQuery(int offset,int length);
比如dbservice.pageQuery(3,5);表示跳過3個記錄,插入5條記錄;
main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<LinearLayout
android:layout_width="wrap_content"//注意一定要wrap_content
android:layout_height="wrap_content"//注意一定要wrap_content
android:orientation="horizontal" >
<TextView
android:layout_width="100dp"
android:layout_height="wrap_content"
android:text="ID" />
<TextView
android:layout_width="100dp"
android:layout_height="wrap_content"
android:text="NAME" />
<TextView
android:layout_width="100dp"
android:layout_height="wrap_content"
android:text="AGE" />
</LinearLayout>
<ListView
android:id="@+id/listview"
android:layout_width="wrap_content"
android:layout_height="fill_parent" >
</ListView>
</LinearLayout>
item.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal" >
<TextView
android:id="@+id/id1"
android:layout_width="100dp"
android:layout_height="wrap_content"
/>
<TextView
android:id="@+id/name"
android:layout_width="100dp"
android:layout_height="wrap_content"
/>
<TextView
android:id="@+id/age"
android:layout_width="100dp"
android:layout_height="wrap_content"
/>
</LinearLayout>
MainActivity.java
package org.xiazdong.db;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.xiazdong.db.domain.Person;
import org.xiazdong.db.service.DBService;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.widget.ListView;
import android.widget.SimpleAdapter;
public class MainActivity extends Activity {
private ListView listView;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
listView = (ListView)this.findViewById(R.id.listview);
DBService service = new DBService(this);
List<Person> persons = service.pageQuery(0, 10);
List<HashMap<String,Object>> data = new ArrayList<HashMap<String,Object>>();
for(Person person:persons){
HashMap<String,Object>map = new HashMap<String,Object>();
map.put("id", person.getId());
map.put("name", person.getName());
map.put("age", person.getAge());
data.add(map);
}
System.out.println(data);
SimpleAdapter adapter = new SimpleAdapter(this, data, R.layout.item, new String[]{"id","name","age"}, new int[]{R.id.id1,R.id.name,R.id.age});
//data表示顯示的資料,一個Map為一行,List<Map>表示多行
//R.layout.item表示一個item的布局
//new String[]{"id","name","age"}表示将key="id"的值映射到R.id.id1上
listView.setAdapter(adapter);
}
}
總結:SimpleAdapter不需要内部實作Adapter,隻能實作每個item布局都一樣的清單;
二、設定每個Item的監聽器
SimpleAdapter:
private OnItemClickListener listener = new OnItemClickListener(){
@Override
public void onItemClick(AdapterView<?> parent, View view, int position,//parent就是ListView,view表示Item視圖,position表示資料索引
long id) {
ListView lv = (ListView)parent;
HashMap<String,Object> person = (HashMap<String,Object>)lv.getItemAtPosition(position);//SimpleAdapter傳回Map
Toast.makeText(getApplicationContext(), person.toString(), Toast.LENGTH_SHORT).show();
}
};
listView.setOnItemClickListener(listener);
SimpleCursorAdapter:
Cursor cursor = (Cursor)lv.getItemAtPosition(position);