九、android的ListView实现数据列表展示

来源:互联网 发布:java构造方法例子 编辑:程序博客网 时间:2024/06/05 09:05

基于上一篇第八节的数据库操作为基础,对数据库中的内容在android界面上进行列表展示

1、工程结构:


列表显示示意图:


列表显示效果图:



2、界面的列表展示配置文件

item.xml:

<?xml version="1.0" encoding="utf-8"?><LinearLayout  xmlns:android="http://schemas.android.com/apk/res/android"  android:layout_width="fill_parent"  android:layout_height="fill_parent"    android:orientation="horizontal" ><!-- 使用线性布局,默认不写的话是线性布局 -->    <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/id"  />    <TextView  android:layout_width="fill_parent"   android:textSize="22sp"  android:layout_height="wrap_content"  android:id="@+id/age"  /></LinearLayout>

main.xml:

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:orientation="vertical"    android:layout_width="fill_parent"    android:layout_height="fill_parent"    >    <LinearLayout  android:orientation="horizontal"  android:layout_width="fill_parent"  android:layout_height="wrap_content">       <TextView  android:layout_width="150dp"  android:layout_height="wrap_content"   android:text="@string/id"  />     <TextView  android:layout_width="120dp"  android:layout_height="wrap_content"  android:text="@string/name"  />    <TextView  android:layout_width="fill_parent"  android:layout_height="wrap_content"  android:text="@string/age"  /></LinearLayout>    <ListView      android:layout_width="fill_parent"     android:layout_height="fill_parent"     android:id="@+id/listView"  /></LinearLayout>

3、数值配置文件

<?xml version="1.0" encoding="utf-8"?><resources>    <string name="hello">Hello World, MainActivity!</string>    <string name="app_name">数据库应用</string>    <string name="id">编号</string>    <string name="name">姓名</string>    <string name="age">年龄</string></resources>

4、Activity的界面显示编码

package cn.huangjie.db;import java.util.ArrayList;import java.util.HashMap;import java.util.List;import cn.huangjie.adapter.PersonAdapter;import cn.huangjie.domain.Person;import cn.huangjie.service.PersonService;import android.app.Activity;import android.database.Cursor;import android.os.Bundle;import android.view.View;import android.widget.AdapterView;import android.widget.AdapterView.OnItemClickListener;import android.widget.CursorAdapter;import android.widget.ListView;import android.widget.SimpleAdapter;import android.widget.SimpleCursorAdapter;import android.widget.Toast;public class DbActivity 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{    /**     * view:当前所点击条目的view对象     * position:当前所点击的条目它所绑定的数据在集合中的索引值     */public void onItemClick(AdapterView<?> parent, View view, int position,long id) {ListView lView = (ListView) parent;/*针对show3()的显示Person person = (Person) lView.getItemAtPosition(position);Toast.makeText(getApplicationContext(), person.getName(),Toast.LENGTH_SHORT).show();*///针对show2的显示Cursor cursor = (Cursor) lView.getItemAtPosition(position);Toast.makeText(getApplicationContext(), cursor.getString(cursor.getColumnIndex("name")),Toast.LENGTH_SHORT).show();}        }        /**     * 自定义适配器     */    private void show3() {List<Person> persons = personService.getScrollData(0, 15);PersonAdapter adapter = new PersonAdapter(this, persons, R.layout.item);listView.setAdapter(adapter);}    /**     * 使用带有游标的简单适配器显示条目列表     */private void show2() {Cursor cursor = personService.getCursorScrollData(0, 20);//使用CursorAdapter在查询结果的名称中必须要有一个"_id"的字段,否则没法工作/** * 第一种方案:增加或修改数据的字段使表中有一个"_id"的字段 * 第二种方案:修改查询语句,使用别名方式 */CursorAdapter adapter = new SimpleCursorAdapter(this, R.layout.item, cursor, new String[]{"_id", "name", "age"}, new int[]{R.id.id, R.id.name, R.id.age});listView.setAdapter(adapter);}/** * 使用简单适配器显示条目列表 */private void show(){    List<Person> persons = personService.getScrollData(0, 10);    //需要 用到适配器,适配器的作用是实现数据的绑定,把数据绑定到条目的显示控件上    List<HashMap<String, Object>> data = new ArrayList<HashMap<String,Object>>();        for(Person person : persons){    HashMap<String, Object> item = new HashMap<String, Object>();    //存放数据的key自定义.名字随便取    item.put("id", person.getId());    item.put("name", person.getName());    item.put("age", person.getAge());    data.add(item);    }    //第三个参数指定数据绑定在哪个条目界面上    SimpleAdapter adapter = new SimpleAdapter(this, data, R.layout.item,     new String[]{"id", "name", "age"}, new int[]{R.id.id, R.id.name, R.id.age});    listView.setAdapter(adapter);    //根据手机窗口的高度和条目的高度,可以计算出该窗口展示的条目数量    /*内部的操作代码:    int total = adapter.getCount();    int perpage = 7;//显示每页展示的个数    for(int i=0; i<perpage; i++){    //得到条目的窗口view对象,第二个参数为缓存的view(以前创建的view)    View view = adapter.getView(i, convertView, parent);    //显示条目    }*/    }       }

5、自定义适配器显示

package cn.huangjie.adapter;import java.util.List;import android.content.Context;import android.view.LayoutInflater;import android.view.View;import android.view.ViewGroup;import android.widget.BaseAdapter;import android.widget.TextView;import cn.huangjie.db.R;import cn.huangjie.domain.Person;/** * 自定义适配器 * @author user */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){this.persons = persons;this.resource = resource;//得到系统内置的布局填充服务inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);}public int getCount() {return persons.size();//数据总数}//外面给定一个索引值,就可以得到该索引对应的元素public Object getItem(int position) {// TODO Auto-generated method stubreturn persons.get(position);}//取得条目的idpublic long getItemId(int position) {// TODO Auto-generated method stubreturn position;}/** * listView在显示第一页/屏的时候会把对象new出来,再显示第二页的时候重用 * 第一页的view对象,具有缓存功能 * position表示该条目所要绑定的数据所取得的索引值 */public View getView(int position, View convertView, ViewGroup parent) {TextView idView = null;TextView nameView = null;TextView ageView = null;if(convertView == null){//为第一页显示,创建view对象convertView = inflater.inflate(resource, null);//null没有根元素idView = (TextView) convertView.findViewById(R.id.id);nameView = (TextView) convertView.findViewById(R.id.name);ageView = (TextView) convertView.findViewById(R.id.age);ViewCache cache = new ViewCache();cache.idView = idView;cache.nameView = nameView;cache.ageView = ageView;//存放在标志里当作缓存使用convertView.setTag(cache);}else{ViewCache cache = (ViewCache) convertView.getTag();idView = cache.idView;nameView = cache.nameView;ageView = cache.ageView;}Person person = persons.get(position);idView.setText(person.getId().toString());nameView.setText(person.getName());ageView.setText(person.getAge().toString());return convertView;}/** * 该类用于缓存View对象,第一页的时候进行创建,第二页数据的时候使用缓存 * 这种写法的另一个优点是代码数量少占用内存小,另一种是set/get方法 */private final class ViewCache{public TextView idView;public TextView nameView;public TextView ageView;}}

工程下载地址:http://download.csdn.net/detail/wxwzy738/6316051


原创粉丝点击