listview实现数据列表显示

来源:互联网 发布:low pass filter 算法 编辑:程序博客网 时间:2024/05/16 13:44

一、将要显示的界面中增加一个listview控件

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:orientation="vertical"     >    <LinearLayout    android:layout_width="match_parent"    android:layout_height="match_parent"    android:orientation="horizontal" >    <TextView        android:layout_width="120dp"       android:layout_height="match_parent"        android:text="@string/name"     />    <TextView        android:layout_width="150dp"       android:layout_height="match_parent"        android:text="@string/phone"     />      <TextView        android:layout_width="match_parent"       android:layout_height="match_parent"        android:text="@string/amount"     /></LinearLayout>        <ListView          android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:id="@+id/listview"        ></ListView></LinearLayout>
二、建立item的xml文件

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:orientation="horizontal" >        <TextView        android:layout_width="120dp"       android:layout_height="match_parent"        android:id="@+id/name"     />    <TextView        android:layout_width="150dp"       android:layout_height="match_parent"        android:id="@+id/phone"     />        <TextView        android:layout_width="match_parent"       android:layout_height="match_parent"        android:id="@+id/amount"     /></LinearLayout>
三、继承 BaseAdapter创建适配器

public class PersonAdapter extends BaseAdapter {private List<Person> persons;private int resource; //绑定的条目界面private LayoutInflater inflater;//将xml对象转化为view对象public PersonAdapter(Context context,List<Person> persons,int resource){  //构造方法引入contextthis.persons = persons;this.resource = resource;inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);}@Overridepublic int getCount() {return persons.size();}@Overridepublic Object getItem(int position) {return persons.get(position);}@Overridepublic long getItemId(int position) {return position;}public View getView0(int position, View convertView, ViewGroup parent) {if(convertView ==null){//用于缓存条目对象convertView = inflater.inflate(resource, null);//生成条目界面对象}TextView nameText = (TextView) convertView.findViewById(R.id.name);TextView phoneText = (TextView) convertView.findViewById(R.id.phone);TextView amountText = (TextView) convertView.findViewById(R.id.amount);Person person = persons.get(position);//实现数据绑定nameText.setText(person.getName());phoneText.setText(person.getPhone());amountText.setText(person.getAmount().toString());return convertView;}
//改善性能,查找耗费性能,定义一个内部类用来缓存这三个view对象

public View getView(int position, View convertView, ViewGroup parent) {TextView nameText=null;TextView phoneText = null;TextView amountText = null;if(convertView ==null){//用于缓存条目对象convertView = inflater.inflate(resource, null);//生成条目界面对象 nameText = (TextView) convertView.findViewById(R.id.name); phoneText = (TextView) convertView.findViewById(R.id.phone); amountText = (TextView) convertView.findViewById(R.id.amount);ViewCatch viewCatch = new ViewCatch();viewCatch.nameText = nameText;viewCatch.phoneText = phoneText;viewCatch.amountText = amountText;convertView.setTag(viewCatch); }else{ViewCatch viewCatch = (ViewCatch)convertView.getTag();nameText = viewCatch.nameText; phoneText = viewCatch.phoneText;amountText =viewCatch.amountText;  }Person person = persons.get(position);nameText.setText(person.getName());phoneText.setText(person.getPhone());nameText.setText(person.getAmount().toString());return convertView;}public final class ViewCatch{public TextView nameText; public TextView phoneText;public TextView amountText; }}

其他两种显示方式

private void show() {List<Person> persons = otherPersonService.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);}}
使用cursor作为参数

private void show2() {Cursor cursor = otherPersonService.getCursorScrollData(0, 20);@SuppressWarnings("deprecation")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);}





     


0 0
原创粉丝点击