ListView的例子

1. 概述

ListView用于以列表的形式显示数据,使用中,需要通过适配器来显示数据。

2. 总的思想

使用ListView时,涉及到如下几个方面的工作:

  • ListView控件:在某个布局中定义一个ListView控件,用于以列表方式显示一些数据;——对应MVC模式中的View
  • 待显示的数据:所有要显示的数据,通常是Map对象的一个列表;——对应MVC模式中的Model(领域模型)
  • 列表中每一行数据的显示布局:也是View的一部分,对应ListView控件中要显示的每一行数据的具体布局;
  • 适配器:用于把待显示的每一项数据映射(适配)到列表框中的每一行。——对应MVC模式中的Control。


3. SimpleAdapter

SimpleAdapter是最简单的一种适配器,只需要给SimpleAdapter设置数据即可。

3.1 代码

3.1.1 Java代码

package com.example.hellolistview;

import java.util.ArrayList;
import java.util.HashMap;

import android.app.Activity;
import android.os.Bundle;
import android.widget.ListView;
import android.widget.SimpleAdapter;

public class MainActivity extends Activity {
	
    @Override
    protected void onCreate(Bundle savedInstanceState) {
    	
        super.onCreate(savedInstanceState);
        setContentView(R.layout.my_listview);

        ListView listView = (ListView) this.findViewById(R.id.list_view);
        setData(listView);
        
    }

	private void setData(ListView listView) {
		// each item's layout resource id
		int student_item_id = R.layout.student_item;

		// columns names
		String[] columnNames = new String[] { "name", "score" };

		// resource ids
		int[] ids = new int[] { R.id.student_name, R.id.student_score };

		// the data to be displayed
		ArrayList<HashMap<String, Object>> students = new ArrayList<HashMap<String, Object>>();
		HashMap<String, Object> map = null;
		for (int i = 1; i <= 10; i++) {
			map = new HashMap<String, Object>();
			map.put(columnNames[0], "student-" + i);
			map.put(columnNames[1], String.valueOf(i * 10));
			students.add(map);
		}
		
		listView.setAdapter(new SimpleAdapter(this, students, student_item_id,
				columnNames, ids));

	}
    
}


3.1.2 my_listview.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="vertical" >
    
    <ListView
        android:id="@+id/list_view"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"/>

</LinearLayout>

3.1.3 student_item.xml

<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:id="@+id/student_name"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="2"/>
    
    <TextView
        android:id="@+id/student_score"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"/>

</LinearLayout>

3.2 代码解读

my_listview.xml资源文件比较简单,它用作整个Activity的界面,其中包括一个ListView控件。

而这个控件中的每一行的UI布局,则对应student_item.xml的定义。

student_item的数据填充,则是由适配器来完成的。——适配器的输入就是领域模型的具体数据。

因为SimpleAdapter帮助我们完成了所有的工作,因此上述代码比较简单。——至于其输入数据及其含义,直接看SimpleAdapter的源码即可。我们在setData()方法中,特意定义了几个变量,它和SimpleAdapter构造函数传入的参数是一致的。

3.3 效果图


4. BaseAdapter

对于上述需求,我们再用BaseAdapter进行改写。通过这种比较,可以进一步理解各种适配器的使用方法、功能差异点。

4.1 代码

只需要Java代码重构如下:

package com.example.hellolistview;

import java.util.ArrayList;
import java.util.HashMap;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;

public class MainActivity extends Activity {
	// each item's layout resource id
	//private int student_item_id = R.layout.student_item;

	// columns names
	private String[] columnNames = new String[] { "name", "score" };

	// resource ids
	//private int[] ids = new int[] { R.id.student_name, R.id.student_score };
	
	private ArrayList<HashMap<String, Object>> students = new ArrayList<HashMap<String, Object>>();
	
	private Context context = null;
	
    @Override
    protected void onCreate(Bundle savedInstanceState) {
    	
        super.onCreate(savedInstanceState);
        setContentView(R.layout.my_listview);

        context = this;
        
        ListView listView = (ListView) this.findViewById(R.id.list_view);
        createData();
        //setSimpleAdapter(listView);
        listView.setAdapter(new AnotherAdapter());
        
    }

    private void createData() {
    	// the data to be displayed
		HashMap<String, Object> map = null;
		for (int i = 1; i <= 10; i++) {
			map = new HashMap<String, Object>();
			map.put(columnNames[0], "student-" + i);
			map.put(columnNames[1], String.valueOf(i * 10));
			students.add(map);
		}
    }
    
	/*private void setSimpleAdapter(ListView listView) {
		listView.setAdapter(new SimpleAdapter(this, students, student_item_id,
				columnNames, ids));
	}*/
    
	public class AnotherAdapter extends BaseAdapter {

		@Override
		public int getCount() {
			return students.size();
		}

		@Override
		public Object getItem(int position) {
			return students.get(position);
		}

		@Override
		public long getItemId(int position) {
			return position;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			if (convertView == null) {
				convertView = LayoutInflater.from(context).inflate(R.layout.student_item, null);
			}
			
			TextView studentName = (TextView) convertView.findViewById(R.id.student_name);
			TextView studentScore = (TextView) convertView.findViewById(R.id.student_score);
			
			studentName.setText((CharSequence) students.get(position).get(columnNames[0]/*"name"*/));
			studentScore.setText((CharSequence) students.get(position).get("score"));
			
			return convertView;
		}
		
	}
}

通过查看SimpleAdapter的源代码,可以发现这里的AnotherAdapter的代码和SimpleAdapter是一致的。——只是SimpleAdapter对getView()做了各种支持。


事实上,SimpleAdapter继承自BaseAdapter,并实现了Filterable接口。——后面会讲述如何过滤数据。




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值