listview实现数据列表显示

一、将要显示的界面中增加一个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){  //构造方法引入context
		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;
	}


	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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值