• ListView的使用(一)


    在android开发中ListView是比较常用的组件,它以列表的形式展示具体内容,并且能够根据数据的长度自适应显示。

     列表的显示需要三个元素:

    1.ListVeiw 用来展示列表的View。

    2.适配器 用来把数据映射到ListView上的中介。

    3.数据    具体的将被映射的字符串,图片,或者基本组件。

    现在就来实现上述样式的listview

    首先,程序中需要定义两个XML文件,一个定义主Activity的UI界面(Screen Layout)

    ,另一个对Listview每一行的样式进行定义(Row Layout):

    activity_mian.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:id="@+id/listLinearLayout"
            android:layout_width="fill_parent" android:layout_height="wrap_content"
            android:orientation="vertical">
            <ListView android:id="@id/android:list" android:layout_width="fill_parent"
                android:layout_height="wrap_content" android:drawSelectorOnTop="true"
                android:scrollbars="vertical" />
        </LinearLayout>
    </LinearLayout>

    list.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="horizontal" >
        <ImageView android:id="@+id/img"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_margin="5dp"/>
        <LinearLayout 
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="vertical" >
            <TextView android:id="@+id/title"
                 android:layout_width="wrap_content"
                 android:layout_height="wrap_content"/>
            <TextView android:id="@+id/inf"
                 android:layout_width="wrap_content"
                 android:layout_height="wrap_content"/>          
        </LinearLayout>  
    </LinearLayout>

    为了更好的理解程序,先说说适配器。

    以simpleAdapter为例,它的扩展性最好,可以映射各种各样的布局。

    参数:
          1,context:上下文。
          2,data:基于Map的list。Data里边的每一项都和 ListView里边的每一行对应。Data里边的每一项都是一个Map类型,这个Map类里边包含了ListView每一行需要的数据。
          3,resource :就是一个布局layout,需要包含to参数所指定的条目。
          4,from:这是个名字数组,每个名字是为了在 ArrayList数组的每一个item索引Map<String,Object>的Object用的。即 map 中得key值
          5,to:里面是一个TextView数组。这些 TextView是以id的形式来表示的。例如:Android.R.id.text1,这个text1在layout当中是可以索引的。

    反应到程序中对应代码就是:

    SimpleAdapter adapter = new SimpleAdapter(this, getData(), R.layout.list, 
                    new String[]{"title","info","img"},new int[]{R.id.title,R.id.inf,R.id.img});
            setListAdapter(adapter);

    其中geyData()函数返回List类型的打包数据,代码如下:

    private List<Map<String, Object>> getData() {
            List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
    
            Map<String, Object> map = new HashMap<String, Object>();
            map.put("title", "G1");
            map.put("info", "google 1");
            map.put("img", R.drawable.i1);
            list.add(map);
    
            map = new HashMap<String, Object>();
            map.put("title", "G2");
            map.put("info", "google 2");
            map.put("img", R.drawable.i2);
            list.add(map);
    
            map = new HashMap<String, Object>();
            map.put("title", "G3");
            map.put("info", "google 3");
            map.put("img", R.drawable.i3);
            list.add(map);
            
            return list;
        }

    最终实现效果:

    下一篇将实现listview添加按钮,并添加控制效果。

    附完整代码:

    package com.hixin.mylistview;
    
    import java.util.ArrayList;
    import java.util.HashMap;
    import java.util.List;
    import java.util.Map;
    
    import android.app.ListActivity;
    import android.os.Bundle;
    import android.widget.SimpleAdapter;
    
    
    
    
    public class MainActivity extends ListActivity {
        
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            SimpleAdapter adapter = new SimpleAdapter(this, getData(), R.layout.list, 
                    new String[]{"title","info","img"},new int[]{R.id.title,R.id.inf,R.id.img});
            setListAdapter(adapter);
            
    
        }    
        private List<Map<String, Object>> getData() {
            List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
    
            Map<String, Object> map = new HashMap<String, Object>();
            map.put("title", "G1");
            map.put("info", "google 1");
            map.put("img", R.drawable.i1);
            list.add(map);
    
            map = new HashMap<String, Object>();
            map.put("title", "G2");
            map.put("info", "google 2");
            map.put("img", R.drawable.i2);
            list.add(map);
    
            map = new HashMap<String, Object>();
            map.put("title", "G3");
            map.put("info", "google 3");
            map.put("img", R.drawable.i3);
            list.add(map);
            
            return list;
        }
        
    
    }
  • 相关阅读:
    dos命令大全
    死亡之ping(Ping of Death)
    硬盘安装系统
    DataGrid实现逻辑分页
    DropDownList另一种写法
    DataGrid3
    DataGrid2
    hidden(隐藏域)
    sql合并列
    未找到与约束contractname Microsoft.VisualStudio.Utilities.IContentTypeRegistryService...匹配的导出
  • 原文地址:https://www.cnblogs.com/hixin/p/4018715.html
Copyright © 2020-2023  润新知