熟悉AndroidAPI系列15——ListView

三个关键点

  • 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     }
时间: 2024-08-28 20:13:58

熟悉AndroidAPI系列15——ListView的相关文章

熟悉AndroidAPI系列14——SharedPreferences和保存用户设置

点击保存参数保存 重启应用,会显示上一次保存的数据 类似于工作用户登录界面 SharedPreferences类 如何创建这个类的实例 它和Context的练习 业务类的代码 1 public void save(String name, String age){ 2 SharedPreferences preference = mcontext.getSharedPreferences("app_preference", Context.MODE_PRIVATE); 3 Editor

熟悉AndroidAPI系列16—-ProgressBar

设置风格style="?android:attr/progressBarStyleSmall" style="@android:style/Widget.ProgressBar.Horizontal" android中的进度条 各种进度条关系 上图圆圈为ProgressBar,风格为垂直 上图右上为水平风格的ProgressBar 上图坐下为SeekBar 上图星星为RatingBar 控制进度条 max属性: progress属性:当前进度 secondaryPr

熟悉AndroidAPI系列8——RelativeLayout综合练习

组件布局最好通过一个兄弟组件或者父组件确定其位置坐标. gravity属性,能容在组件内部的位置 EditText中的提示属性hint,以及如何设置密码的输入格式 1 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 2 xmlns:tools="http://schemas.android.com/tools" 3 android:layout_width=&

熟悉AndroidAPI系列4——ImageView和scaleType属性

图像比ImageView的尺寸小,但不想改变图像大小优先考虑center选项系列 图像比ImageView的尺寸大,可以有多种选择,可特别考虑centerCrop选项 scaleType属性的center选项 scaleType属性的centerCrop选项 scaleType属性的centerInside选项 scaleType属性的fitCenter选项 scaleType属性的fitStart选项 scaleType属性的fitEnd选项

熟悉AndroidAPI系列12——AlertDialog和Toast

如何建立上面的对话框和提示信息 1 @Override 2 protected void onCreate(Bundle savedInstanceState) { 3 super.onCreate(savedInstanceState); 4 setContentView(R.layout.activity_main); 5 6 btn = (Button)findViewById(R.id.btn); 7 builder = new AlertDialog.Builder(this); 8

熟悉AndroidAPI系列2——CheckBox和OnCheckedChangeListener

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBott

熟悉AndroidAPI系列9——ProgressBar

熟悉AndroidAPI系列1——LinearLayout

<?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:ori

熟悉AndroidAPI系列13——LayoutInflater

如何把一个xml文件渲染成一个View控件 1 //如何把一个xml文件渲染为一个View控件 2 //得到一个对这个Activity的渲染器inflater 3 LayoutInflater inflater = this.getLayoutInflater(); 4 //将image_switch.xml渲染成View控件 5 View view = inflater.inflate(R.layout.image_switch, null); 6 builder.setView(view);