码迷,mamicode.com
首页 > 移动开发 > 详细

熟悉AndroidAPI系列15——ListView

时间:2015-01-16 16:33:11      阅读:134      评论:0      收藏:0      [点我收藏+]

标签:

三个关键点

  • xml布局设置

  • 数据和简单适配器

  • 自定义适配器

 

XML布局

  1. 主Activity布局

  2. ListView条目的XML布局

主Activity布局,只需要加入一个ListView控件,特别要注意各个控件的layout_width和layout_height的设定

 1 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 2     xmlns:tools="http://schemas.android.com/tools"
 3     android:layout_width="match_parent"
 4     android:layout_height="match_parent"
 5     android:orientation="vertical">
 6 
 7     <LinearLayout 
 8         android:layout_width="fill_parent"
 9         android:layout_height="wrap_content"
10         android:orientation="horizontal">
11         <TextView 
12             android:text="@string/name"
13             android:layout_width="1dp"
14             android:layout_height="wrap_content"
15             android:layout_weight="3"/>
16            <TextView 
17             android:text="@string/phone"
18             android:layout_width="1dp"
19             android:layout_height="wrap_content"
20             android:layout_weight="5"/>
21            <TextView 
22             android:text="@string/account"
23             android:layout_width="1dp"
24             android:layout_height="wrap_content"
25             android:layout_weight="2"/>
26     </LinearLayout>
27     
28     <ListView 
29         android:id="@+id/listView"
30         android:layout_width="fill_parent"
31         android:layout_height="fill_parent">
32         
33     </ListView>
34 
35 </LinearLayout>

示意图

技术分享

 

条目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="wrap_content"
    android:orientation="horizontal" >

        
        <TextView 
            android:id="@+id/name"
            android:layout_width="1dp"
            android:layout_height="wrap_content"
            android:layout_weight="3"/>
        
           <TextView 
            android:id="@+id/phone"
            android:layout_width="1dp"
            android:layout_height="wrap_content"
            android:layout_weight="5"/>
           
           <TextView 
            android:id="@+id/account"
            android:layout_width="1dp"
            android:layout_height="wrap_content"
            android:layout_weight="2"/>
    

</LinearLayout>

 

 

数据和简单数据适配器

  1. 数据形式为Cursor 结果集形式及相应的SimpleCursorAdapter

  2. 数据形式为List< ?extendsMap< String,? > >及相应的SimpleAdapter

Cursor和SimpleCursorAdapter

    private void showListView(Uri uri) {
        ContentResolver resolver = MainActivity.this.getContentResolver();
        cursor = resolver.query(uri, null, null, null, "_id desc limit 0,20");
        //给listView绑定适配器
        SimpleCursorAdapter adapter = new SimpleCursorAdapter(this, R.layout.item, cursor,
                new String[]{"name", "phone", "account"},
                new int[]{R.id.name, R.id.phone, R.id.account}, 1);
        listView.setAdapter(adapter);
    }

List< ?extendsMap< String,? > >及SimpleAdapter

    private void otherShowListView(Uri uri) {
        ContentResolver resolver = MainActivity.this.getContentResolver();
        cursor = resolver.query(uri, null, null, null, "_id desc limit 0,50");
        //数据存储形式为List< ?extendsMap< String,? > > data;
        List< HashMap<String,Object>  > data = new ArrayList<HashMap<String,Object> >();
        while(cursor.moveToNext()){
            HashMap<String, Object> item = new HashMap<String, Object>();
            item.put("name", cursor.getString(cursor.getColumnIndex("name")));
            item.put("phone", cursor.getString(cursor.getColumnIndex("phone")));
            item.put("account", cursor.getString(cursor.getColumnIndex("account")));
            data.add(item);
        }
        SimpleAdapter adapter = new SimpleAdapter(this, data, R.layout.item,
                new String[]{"name", "phone", "account"},
                new int[]{R.id.name, R.id.phone, R.id.account});
        listView.setAdapter(adapter);
    }

 

自定义适配器

  • ListView视图的创建过程

  1. 首先调用适配器的getCount得到共有n个条目
  2. 然后根据每个条目的高度计算出页面可以容纳perPage个条目
  3. 然后为这perPage个条目通过调用getView对象创建出perPage个View控件
  4. ListView是具有缓存功能的,即只会创建perPage个View,当上下移动时,新出现的View使用暂时看不到的View控件(改变其中的数据)

 

 1 private void showListView3(){
 2         final List<Person> dataList = pService.getScrollDate1(0, 30);
 3         listView.setAdapter(new BaseAdapter() {
 4             @Override
 5             public View getView(int position, View convertView, ViewGroup parent) {
 6                 ViewCache vCache;
 7                 //如果该View控件为空,初始化
 8                 if(convertView == null){
 9                     convertView = MainActivity.this.getLayoutInflater().inflate(R.layout.item, null);
10                     vCache = new ViewCache();
11                     /**
12                      * 每次findView会降低性能,所以希望只在第一次创建View对象时执行find操作
13                      */
14                     vCache.name = (TextView) convertView.findViewById(R.id.name);
15                     vCache.phone = (TextView) convertView.findViewById(R.id.phone);
16                     vCache.account = (TextView) convertView.findViewById(R.id.account);
17                     convertView.setTag(vCache);
18                 }
19                 vCache = (ViewCache) convertView.getTag();
20                 vCache.name.setText(dataList.get(position).getName());
21                 vCache.phone.setText(dataList.get(position).getPhone());
22                 vCache.account.setText(dataList.get(position).getAccount()+"");
23                 
24                 return convertView;
25             }
26             @Override
27             public long getItemId(int position) {
28                 return position;
29             }
30             @Override
31             public Object getItem(int position) {
32                 return dataList.get(position);
33             }
34             
35             @Override
36             public int getCount() {
37                 return dataList.size();
38             }
39         });
40     }

 

熟悉AndroidAPI系列15——ListView

标签:

原文地址:http://www.cnblogs.com/lya-nju/p/4228955.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!