public class MainActivity 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);
show();
}
private void show3() {//自定义适配器
List<Person> persons=personService.getScrollData(0,20);
PersonAdapter adapter=new PersonAdapter(this,persons,R.layout.item);
listView.setAdapter(adapter);
}
private void show2() {
// TODO Auto-generated method stub
Cursor cursor=personService.getCursorData(0, 20);//这个适配器一定要将查询处理的别名改为 _id
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);
}
private void show(){
List<Person> persons=personService.getScrollData(0,20);
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);
}
}
public class PersonAdapter extends BaseAdapter {
private List<Person> persons;// 要绑定的数据
private int resource;// 绑定的条目界面
private LayoutInflater inflater;// 布局填充服务
public PersonAdapter(Context context, List<Person> persons, int resource) {
inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);//取得布局填充服务
this.persons = persons;
this.resource = resource;
}
public int getCount() {// 条目的数量
return persons.size();
}
public Object getItem(int position) {//得到条目的数据
return persons.get(position);
}
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
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;
}
}