item.xml
<?xml version="1.0" encoding="utf-8"?> <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="120dp" android:textSize="22sp" android:layout_height="wrap_content" android:id="@+id/name" /> <TextView android:layout_width="150dp" android:textSize="22sp" android:layout_height="wrap_content" android:id="@+id/phone" /> <TextView android:layout_width="fill_parent" android:textSize="22sp" android:layout_height="wrap_content" android:id="@+id/amount" /> </LinearLayout>
MainActivity.java
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); listView.setOnItemClickListener(new ItemClickListener()); show2(); } private final class ItemClickListener implements OnItemClickListener{ public void onItemClick(AdapterView<?> parent, View view, int position, long id) { ListView lView = (ListView)parent; /* 自定义适配器 Person person = (Person) lView.getItemAtPosition(position); Toast.makeText(getApplicationContext(), person.getId().toString(), 1).show();*/ Cursor cursor = (Cursor) lView.getItemAtPosition(position); int personid = cursor.getInt(cursor.getColumnIndex("_id")); Toast.makeText(getApplicationContext(), personid+ "", 1).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() { //此适配器要求有一个字段为_id,不然会报错,解决方法两种,1、增加_id的字段 2、为字段指定别名 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); } 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;//绑定的条目界面,item.xml private LayoutInflater inflater; //布局填充器,通过上下文得到 public PersonAdapter(Context context, List<Person> persons, int resource) { this.persons = persons; this.resource = resource; inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); } @Override public int getCount() { return persons.size();//数据总数 } @Override public Object getItem(int position) { return persons.get(position); } @Override public long getItemId(int position) { return position; } @Override public View getView(int position, View convertView, ViewGroup parent) { TextView nameView = null; TextView phoneView = null; TextView amountView = null; //利用View的缓存 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; } }