ListView控件示例:
三种绑定适配器的方法:
1、SimpleCursorAdapter
private void show() {
List<Person> persons = personService.getScrollData(0, 19);
List<HashMap<String,Object>> data = new ArrayList<HashMap<String,Object>>();
for(Person person:persons)
{
HashMap<String,Object> item = new HashMap<String,Object>();
item.put("name", person.getName());
item.put("phone", person.getPhone());
item.put("amount", person.getAmount());
item.put("id", person.getId());
data.add(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);
}
2、SimpleCursorAdapter
private void show2() {
Cursor cursor = personService.getCursorScrollData(0, 20);
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);
}
3、自定义适配器
private void show3() {
List<Person> persons = personService.getScrollData(0, 20);
PersonAdapter adapter = new PersonAdapter(this, persons, R.layout.item);
listView.setAdapter(adapter);
}
自定义类:
package cn.itcast.adapter;
import java.util.List;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;
import cn.itcast.db.R;
import cn.itcast.domain.Person;
public class PersonAdapter extends BaseAdapter {
//创建要绑定的数据
private List<Person> persons;
//绑定条目界面
private int resource;
//布局填充器
private LayoutInflater inflater;
public PersonAdapter(Context context,List<Person> persons,int resource) {
super();
this.persons = persons;
this.resource = resource;
inflater=(LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
//得到数据的总数
public int getCount() {
return persons.size();
}
//根据给定的索引值得到指定数据
public Object getItem(int position) {
return persons.get(position);
}
//获得条目ID
public long getItemId(int position) {
return position;
}
//取得条目界面
public View getView1(int position, View convertView, ViewGroup parent) {
//当显示第二页时重用第一页显示好的对象
if(convertView==null){
convertView = inflater.inflate(resource, null);
}
//找显示控件
TextView nameView = (TextView) convertView.findViewById(R.id.name);
TextView phoneView = (TextView) convertView.findViewById(R.id.phone);
TextView amountView = (TextView) convertView.findViewById(R.id.amount);
Person person = persons.get(position);
// 实现数据绑定
nameView.setText(person.getName());
phoneView.setText(person.getPhone());
amountView.setText(person.getAmount().toString());
return convertView;
}
//对上一段代码进行优化
public View getView(int position, View convertView, ViewGroup parent) {
TextView nameView = null;
TextView phoneView =null;
TextView amountView = null;
//当显示第二页时重用第一页显示好的对象
if(convertView==null){
convertView = inflater.inflate(resource, null);
nameView = (TextView) convertView.findViewById(R.id.name);
phoneView = (TextView) convertView.findViewById(R.id.phone);
amountView = (TextView) convertView.findViewById(R.id.amount);
ViewCache cache = new ViewCache();
cache.nameView = nameView;
cache.phoneView = phoneView;
cache.amountView = amountView;
convertView.setTag(cache);
}
else
{
ViewCache cache = (ViewCache) convertView.getTag();
nameView=cache.nameView;
phoneView=cache.phoneView;
amountView=cache.amountView;
}
//找显示控件
Person person = persons.get(position);
// 实现数据绑定
nameView.setText(person.getName());
phoneView.setText(person.getPhone());
amountView.setText(person.getAmount().toString());
return convertView;
}
private final class ViewCache{
public TextView nameView;
public TextView phoneView;
public TextView amountView;
}
}
SimpleCursorAdapter适配器必须包含_id字段。
解决方法:
select*from person order by personid asc limit ?,?
改为
select personid as _id,name,phone,amount from person order by personid asc limit ?,?
绑定代码:
public class DbActivity extends Activity {
private ListView listView;
private PersonService personService;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
personService = new PersonService(this);
listView = (ListView) this.findViewById(R.id.listView);
//为listView设置条目点击事件
listView.setOnItemClickListener(new ItemClickListener());
//show();
// show2();
show3();
}
private final class ItemClickListener implements OnItemClickListener{
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
ListView lView = (ListView)parent;
/*
Cursor cursor = (Cursor) lView.getItemAtPosition(position);
int personid = cursor.getInt(cursor.getColumnIndex("_id"));
Toast.makeText(getApplicationContext(), personid+"", 1).show();
*/
///*自定义适配器
Person person = (Person) lView.getItemAtPosition(position);
Toast.makeText(getApplicationContext(), person.getId().toString(), 1).show();
//*/
}
}
网格布局和条目布局应用相似。