学习笔记-ListView

学习笔记-ListView

一、使用ListView的最简单的方法:

(1)定义列表中的数据

使用private String data[]={“1111”,”2222”}的方法定义字符串数组;

(2)定义ListView组件

Private ListViewmListView

主程序

(1)实例化mListView组件

this.mListView =new ListView(this);

(2)将数据进行包装

this.mListView.setAdapter(newArrayAdapter<String>(

扫描二维码关注公众号,回复: 9187542 查看本文章

this,android.R.layout.simple_expandable_list_item_1,//每行显示一条数据

this.data));//显示的数据

二、如何让ListView显示更加丰富的内容

(1)定义List数据显示的布局文件

(2)定义Activity的布局文件

(3)定义Activity程序,显示ListView

       a.定义要显示的数据

              private String data[][] = newString[][]{

{“01” , ”姓名”},

{“02” , ”性别”}};

              b.定义一个List,以保存所有的List数据

                     List<Map<String,String>>list=

                            newArrayList<Map<String,String>>();

//ArrayList为动态数组

              c.循环设置数据

                     for(intx=0,x<this.data.length;x++){

       Map<String,String>map = newHashMap<String,String>();//定义Map集合

       map.put(“_id” , data[x][0]);//设置_id组件显示数据

       map.put(“name” , data[x][1]”);//设置name组件显示数据

       this.list.add(map);//增加数据

}

List数据结构

其中,map是hashmap,List是ArrayList;

              d.实例化SimpleAdapter

this.simpleAdapter= new SimpleAdapter(this,

this.list,//                                                 //要包装的数据集合

R.layout.data.list,                                    //要使用的显示模板

new String[]{“_id”, “name”},                 //定义要显示的Map的key

new int{R.id._id, R.id.name});                //与模板(已设置好的layout)中的组件匹配

 参考:

1.android开发实战经典

发布了78 篇原创文章 · 获赞 54 · 访问量 30万+

猜你喜欢

转载自blog.csdn.net/chjr1000/article/details/43967917