实例教程八:采用ListView实现数据列表显示

不错的帖子:

分享一个Android火焰效果程序
http://www.eoeandroid.com/thread-210395-1-1.html

android 查询工具源代码
http://www.eoeandroid.com/thread-210365-1-1.html

Android在线音乐播放器
http://www.eoeandroid.com/thread-210352-1-1.html

-------------------正文--------------------------

原文链接:http://www.eoeandroid.com/thread-201372-1-1.html

继上一章的数据,继续添加内容
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:id="@+id/txtName"
        android:layout_width="120dp"
        android:layout_height="wrap_content"
        android:text="xxxxx" />

    <TextView
        android:id="@+id/txtPhone"
        android:layout_width="150dp"
        android:layout_height="wrap_content"
        android:text="xxxxx" />

    <TextView
        android:id="@+id/txtAmount"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="2000" />

</LinearLayout>

main.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="vertical" >
    
    <LinearLayout 
        android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal">
        
        <TextView 
            android:layout_width="120dp"
            android:layout_height="wrap_content"
            android:text="@string/name"/>
        
        <TextView 
            android:layout_width="120dp"
            android:layout_height="wrap_content"
            android:text="@string/phone"/>
        
        <TextView 
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text="@string/amount"/>
        
    </LinearLayout>

    <ListView
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:id="@+id/lstShow" />

</LinearLayout>
package cn.itcast.db;

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

import cn.itcast.domain.Person;
import cn.itcast.domain.PersonAdapter;
import cn.itcast.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.ListView;
import android.widget.SimpleAdapter;
import android.widget.SimpleCursorAdapter;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;

public class MainActivity extends Activity {
        //在Android平台上,集成了一个嵌入式关系型数据库——SQLite
        //SQLite3支持NULL、INTEGER、REAL(浮点数字)、TEXT(字符串文本)和BLOB(二进制对象)数据类型
        //虽然它支持的类型只有5种,但实际上sqlite3也接受varchar(n)、char(n)、decimal(p, s)等数据
        //只不过在运行或保存时会转成对应的五种数据类型
        //SQLite最大的特点是你可以把各种类型的数据保存到任何字段中,而不用关心字段声明的数据类型是什么

        private ListView lstShow;
        private PersonService personService;

        @Override
        public void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
                setContentView(R.layout.main);

                personService = new PersonService(this);                
                lstShow = (ListView)this.findViewById(R.id.lstShow);
                lstShow.setOnItemClickListener(new ItemClickListener());
                //show();
                //show2();
                show3();
        }
        
        private final class ItemClickListener implements OnItemClickListener{
                @Override
                public void onItemClick(AdapterView<?> parent, View view, int position,
                                long id) {
                        
                        ListView lView = (ListView)parent;
                        //和show()3配对
                        Person person = (Person)lView.getItemAtPosition(position);
                        Toast.makeText(getApplicationContext(), person.getId().toString(), 1).show();
                        
                        //和show()2配对
                        Cursor cursor = (Cursor)lView.getItemAtPosition(position);
                        int personId = cursor.getInt(cursor.getColumnIndex("_id"));
                        Toast.makeText(getApplicationContext(), personId, 1).show();
                        
                        //和show()配对
                        List<HashMap<String, Object>> hashMap = (List<HashMap<String, Object>>)lView.getItemAtPosition(position);
                        int Id = Integer.valueOf(hashMap.get(personId).get("personId").toString());
                        Toast.makeText(getApplicationContext(), Id, 1).show();
                }                
        }

        //自定义适配器
        private void show3() {
                List<Person> persons = personService.getScrollData(0, 20);
                PersonAdapter adapter = new PersonAdapter(this, persons, R.layout.item);
                lstShow.setAdapter(adapter);
        }

        private void show2() {
                Cursor cursor = personService.getCursorScrollData(0, 20);
                SimpleCursorAdapter adapter = 
                        new SimpleCursorAdapter(this, R.layout.item, cursor,
                                        new String[]{"name", "phone", "amount"}, 
                                        new int[]{R.id.txtName, R.id.txtPhone, R.id.txtAmount});
                lstShow.setAdapter(adapter);
                
                //直接运行会报错,原因是SimpleCursorAdapter要求结果集中必须包含_id字段
                //修复方法:1.把数据库表中的主键名称personId改为_id
                //2.处理查询后的结果集
        }

        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.txtName, R.id.txtPhone, R.id.txtAmount});
                lstShow.setAdapter(adapter);
                //listView内部操作过程
                //{
                //        int total = adapter.getCount(); //用于得到数据总数
                //        int perpage = 7;
                //        for (int i = 0; i < perpage; i++) {
                //                View view = adapter.getView(i, convertView, parent); //用于得到条目对应的View
                //                //显示条目
                //        }
                //}
        }

}
package cn.itcast.domain;

import java.util.List;

import cn.itcast.db.R;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

public class PersonAdapter extends BaseAdapter {
        private List<Person> persons; //绑定数据
        private int resource; //绑定条目界面
        private LayoutInflater inflater;
        
        public PersonAdapter(Context context, List<Person> persons, int resouce){
                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 txtName = null;
                TextView txtPhone = null;
                TextView txtAmount = null;
                //这样提升性能
                if(convertView == null){ //表明现在是第1页
                        inflater.inflate(resource, null); //生成条目界面对象
                        
                        txtName = (TextView)convertView.findViewById(R.id.txtName);
                        txtPhone = (TextView)convertView.findViewById(R.id.txtPhone);
                        txtAmount = (TextView)convertView.findViewById(R.id.txtAmount);
                        
                        ViewCache cache = new ViewCache();
                        cache.txtName = txtName;
                        cache.txtPhone = txtPhone;
                        cache.txtAmount = txtAmount;
                        
                        convertView.setTag(cache);
                }else{
                        ViewCache cache = (ViewCache)convertView.getTag();
                        txtName = cache.txtName;
                        txtPhone = cache.txtPhone;
                        txtAmount = cache.txtAmount;
                }
                                
                Person person = persons.get(position);
                //下面代码实现数据绑定
                txtName.setText(person.getName());
                txtPhone.setText(person.getPhone());
                txtAmount.setText(person.getAmount().toString());
                
                return null;
        }
        
        private final class ViewCache{
                public TextView txtName;
                public TextView txtPhone;
                public TextView txtAmount;
        }
}
原文地址:https://www.cnblogs.com/vus520/p/2745188.html