zoukankan      html  css  js  c++  java
  • Android-第二天(2)

    程序3

    SimpleAdapter是扩展性最好的适配器,可以定义各种你想要的布局,而且使用很方便

    SimpleAdapter(Context context, List<? extends Map<String, ?>> data, int resource, String[] from, int[] to)

    参数context:上下文,比如this。关联SimpleAdapter运行的视图上下文

    参数data:Map列表,列表要显示的数据,这部分需要自己实现,如例子中的getData(),类型要与上面的一致,每条项目要与from中指定条目一致

    参数resource:ListView单项布局文件的Id,这个布局就是你自定义的布局了,你想显示什么样子的布局都在这个布局中。这个布局中必须包括了to中定义的控件id

    参数 from:一个被添加到Map上关联每一个项目列名称的列表,数组里面是列名称

    参数 to:是一个int数组,数组里面的id是自定义布局中各个控件的id,需要与上面的from对应

    main.xml

     1 <?xml version="1.0" encoding="utf-8"?>  
     2 <LinearLayout   
     3         android:id="@+id/LinearLayout01"   
     4         android:layout_width="fill_parent"   
     5         android:layout_height="fill_parent"   
     6         xmlns:android="http://schemas.android.com/apk/res/android">  
     7           
     8         <ListView android:layout_width="wrap_content"   
     9                   android:layout_height="wrap_content"   
    10                   android:id="@+id/MyListView">  
    11         </ListView>  
    12 </LinearLayout> 

    my_listitem.xml的代码如下,my_listitem.xml用于设计ListView的Item:

     1 <?xml version="1.0" encoding="utf-8"?>  
     2 <LinearLayout   
     3         android:layout_width="fill_parent"   
     4         xmlns:android="http://schemas.android.com/apk/res/android"   
     5         android:orientation="vertical"  
     6         android:layout_height="wrap_content"   
     7         android:id="@+id/MyListItem"   
     8         android:paddingBottom="3dip"   
     9         android:paddingLeft="10dip">  
    10         <TextView   
    11                 android:layout_height="wrap_content"   
    12                 android:layout_width="fill_parent"   
    13                 android:id="@+id/ItemTitle"   
    14                 android:textSize="30dip">  
    15         </TextView>  
    16         <TextView   
    17                 android:layout_height="wrap_content"   
    18                 android:layout_width="fill_parent"   
    19                 android:id="@+id/ItemText">  
    20         </TextView>  
    21 </LinearLayout>  

    JAVA

     1 public void onCreate(Bundle savedInstanceState) {  
     2     super.onCreate(savedInstanceState);  
     3     setContentView(R.layout.main);  
     4     //绑定XML中的ListView,作为Item的容器   
     5     ListView list = (ListView) findViewById(R.id.MyListView);  
     6       
     7     //生成动态数组,并且转载数据   
     8     ArrayList<HashMap<String, String>> mylist = new ArrayList<HashMap<String, String>>();  
     9     for(int i=0;i<30;i++)  
    10     {  
    11         HashMap<String, String> map = new HashMap<String, String>();  
    12         map.put("ItemTitle", "This is Title.....");  
    13         map.put("ItemText", "This is text.....");  
    14         mylist.add(map);  
    15     }  
    16     //生成适配器,数组===》ListItem   
    17     SimpleAdapter mSchedule = new SimpleAdapter(this, //没什么解释   
    18                                                 mylist,//数据来源    
    19                                                 R.layout.my_listitem,//ListItem的XML实现   
    20                                                   
    21                                                 //动态数组与ListItem对应的子项           
    22                                                 new String[] {"ItemTitle", "ItemText"},   
    23                                                   
    24                                                 //ListItem的XML文件里面的两个TextView ID   
    25                                                 new int[] {R.id.ItemTitle,R.id.ItemText});  
    26     //添加并且显示   
    27     list.setAdapter(mSchedule);  
    28 }  

    ListView是一个经常用到的控件,ListView里面的每个子项Item可以使一个字符串,也可以是一个组合控件。先说说ListView的实现:

    1.准备ListView要显示的数据

    2.使用 一维或多维 动态数组 保存数据;

    2.构建适配器 简单地来说, 适配器就是 Item数组 动态数组 有多少元素就生成多少个Item;

    3.把 适配器 添加到ListView,并显示出来。

     

    接下来,看看本文代码所实现的ListView:

  • 相关阅读:
    关于JVM的一些想法
    hashMap理解以及jdk1.7、jdk1.8其中区别
    各数据库如何实现自增
    dubbo遇坑记录
    mysql建表语句问题
    @Configuration
    生成一个唯一的ID
    门面模式
    关于getClass().getClassLoader()
    元素链
  • 原文地址:https://www.cnblogs.com/ejllen/p/3727671.html
Copyright © 2011-2022 走看看