android ListView详解

http://www.cnblogs.com/allin/archive/2010/05/11/1732200.html



在android开发中ListView是比较常用的组件,它以列表的形式展示具体内容,并且能够根据数据的长度自适应显示。抽空把对ListView的使用做了整理,并写了个小例子,如下图。

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

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

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

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

根据列表的适配器类型,列表分为三种,ArrayAdapter,SimpleAdapter和SimpleCursorAdapter

其中以ArrayAdapter最为简单,只能展示一行字。SimpleAdapter有最好的扩充性,可以自定义出各种效果。SimpleCursorAdapter可以认为是SimpleAdapter对数据库的简单结合,可以方面的把数据库的内容以列表的形式展示出来。

 我们从最简单的ListView开始:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
/**
  * @author allin
  *
  */
public  class  MyListView extends  Activity {
 
     private  ListView listView;
     //private List<String> data = new ArrayList<String>();
     @Override
     public  void  onCreate(Bundle savedInstanceState){
         super .onCreate(savedInstanceState);
         
         listView = new  ListView( this );
         listView.setAdapter( new  ArrayAdapter<String>( this , android.R.layout.simple_expandable_list_item_1,getData()));
         setContentView(listView);
     }
     
     
     
     private  List<String> getData(){
         
         List<String> data = new  ArrayList<String>();
         data.add( "测试数据1" );
         data.add( "测试数据2" );
         data.add( "测试数据3" );
         data.add( "测试数据4" );
         
         return  data;
     }
}

上面代码使用了ArrayAdapter(Context context, int textViewResourceId, List<T> objects)来装配数据,要装配这些数据就需要一个连接ListView视图对象和数组数据的适配器来两者的适配工作,ArrayAdapter的构造需要三个参数,依次为this,布局文件(注意这里的布局文件描述的是列表的每一行的布局,android.R.layout.simple_list_item_1是系统定义好的布局文件只显示一行文字,数据源(一个List集合)。同时用setAdapter()完成适配的最后工作。运行后的现实结构如下图:

SimpleCursorAdapter

  sdk的解释是这样的:An easy adapter to map columns from a cursor to TextViews or ImageViews defined in an XML file. You can specify which columns you want, which views you want to display the columns, and the XML file that defines the appearance of these views。简单的说就是方便把从游标得到的数据进行列表显示,并可以把指定的列映射到对应的TextView中。

  下面的程序是从电话簿中把联系人显示到类表中。先在通讯录中添加一个联系人作为数据库的数据。然后获得一个指向数据库的Cursor并且定义一个布局文件(当然也可以使用系统自带的)。

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
/**
  * @author allin
  *
  */
public  class  MyListView2 extends  Activity {
 
     private  ListView listView;
     //private List<String> data = new ArrayList<String>();
     @Override
     public  void  onCreate(Bundle savedInstanceState){
         super .onCreate(savedInstanceState);
         
         listView = new  ListView( this );
         
         Cursor cursor = getContentResolver().query(People.CONTENT_URI, null , null , null , null );
         startManagingCursor(cursor);
         
         ListAdapter listAdapter = new  SimpleCursorAdapter( this , android.R.layout.simple_expandable_list_item_1,
                 cursor,
                 new  String[]{People.NAME},
                 new  int []{android.R.id.text1});
         
         listView.setAdapter(listAdapter);
         setContentView(listView);
     }
     
     
}

 Cursor cursor = getContentResolver().query(People.CONTENT_URI, null, null, null, null);先获得一个指向系统通讯录数据库的Cursor对象获得数据来源。

 startManagingCursor(cursor);我们将获得的Cursor对象交由Activity管理,这样Cursor的生命周期和Activity便能够自动同步,省去自己手动管理Cursor。

 SimpleCursorAdapter 构造函数前面3个参数和ArrayAdapter是一样的,最后两个参数:一个包含数据库的列的String型数组,一个包含布局文件中对应组件id的int型数组。其作用是自动的将String型数组所表示的每一列数据映射到布局文件对应id的组件上。上面的代码,将NAME列的数据一次映射到布局文件的id为text1的组件上。

注意:需要在AndroidManifest.xml中如权限:<uses-permission android:name="android.permission.READ_CONTACTS"></uses-permission>

运行后效果如下图:

SimpleAdapter

simpleAdapter的扩展性最好,可以定义各种各样的布局出来,可以放上ImageView(图片),还可以放上Button(按钮),CheckBox(复选框)等等。下面的代码都直接继承了ListActivity,ListActivity和普通的Activity没有太大的差别,不同就是对显示ListView做了许多优化,方面显示而已。

下面的程序是实现一个带有图片的类表。

首先需要定义好一个用来显示每一个列内容的xml

vlist.xml

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
<? xml  version = "1.0"  encoding = "utf-8" ?>
< LinearLayout  xmlns:android = "http://schemas.android.com/apk/res/android"
     android:orientation = "horizontal"  android:layout_width = "fill_parent"
     android:layout_height = "fill_parent" >
 
 
     < ImageView  android:id = "@+id/img"
         android:layout_width = "wrap_content"
         android:layout_height = "wrap_content"
         android:layout_margin = "5px" />
 
     < LinearLayout  android:orientation = "vertical"
         android:layout_width = "wrap_content"
         android:layout_height = "wrap_content" >
 
         < TextView  android:id = "@+id/title"
             android:layout_width = "wrap_content"
             android:layout_height = "wrap_content"
             android:textColor = "#FFFFFFFF"
             android:textSize = "22px"  />
         < TextView  android:id = "@+id/info"
             android:layout_width = "wrap_content"
             android:layout_height = "wrap_content"
             android:textColor = "#FFFFFFFF"
             android:textSize = "13px"  />
 
     </ LinearLayout >
 
 
</ LinearLayout >

下面是实现代码:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
/**
  * @author allin
  *
  */
public  class  MyListView3 extends  ListActivity {
 
 
     // private List<String> data = new ArrayList<String>();
     @Override
     public  void  onCreate(Bundle savedInstanceState) {
         super .onCreate(savedInstanceState);
 
         SimpleAdapter adapter = new  SimpleAdapter( this ,getData(),R.layout.vlist,
                 new  String[]{ "title" , "info" , "img" },
                 new  int []{R.id.title,R.id.info,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;
     }
}

使用simpleAdapter的数据用一般都是HashMap构成的List,list的每一节对应ListView的每一行。HashMap的每个键值数据映射到布局文件中对应id的组件上。因为系统没有对应的布局文件可用,我们可以自己定义一个布局vlist.xml。下面做适配,new一个SimpleAdapter参数一次是:this,布局文件(vlist.xml),HashMap的 title 和 info,img。布局文件的组件id,title,info,img。布局文件的各组件分别映射到HashMap的各元素上,完成适配。

运行效果如下图:

有按钮的ListView

但是有时候,列表不光会用来做显示用,我们同样可以在在上面添加按钮。添加按钮首先要写一个有按钮的xml文件,然后自然会想到用上面的方法定义一个适配器,然后将数据映射到布局文件上。但是事实并非这样,因为按钮是无法映射的,即使你成功的用布局文件显示出了按钮也无法添加按钮的响应,这时就要研究一下ListView是如何现实的了,而且必须要重写一个类继承BaseAdapter。下面的示例将显示一个按钮和一个图片,两行字如果单击按钮将删除此按钮的所在行。并告诉你ListView究竟是如何工作的。效果如下:

vlist2.xml

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
<? xml  version = "1.0"  encoding = "utf-8" ?>
< LinearLayout  xmlns:android = "http://schemas.android.com/apk/res/android"
     android:orientation = "horizontal"
     android:layout_width = "fill_parent"
     android:layout_height = "fill_parent" >
 
 
     < ImageView  android:id = "@+id/img"
         android:layout_width = "wrap_content"
         android:layout_height = "wrap_content"
         android:layout_margin = "5px" />
 
     < LinearLayout  android:orientation = "vertical"
         android:layout_width = "wrap_content"
         android:layout_height = "wrap_content" >
 
         < TextView  android:id = "@+id/title"
             android:layout_width = "wrap_content"
             android:layout_height = "wrap_content"
             android:textColor = "#FFFFFFFF"
             android:textSize = "22px"  />
         < TextView  android:id = "@+id/info"
             android:layout_width = "wrap_content"
             android:layout_height = "wrap_content"
             android:textColor = "#FFFFFFFF"
             android:textSize = "13px"  />
 
     </ LinearLayout >
 
 
     < Button  android:id = "@+id/view_btn"
         android:layout_width = "wrap_content"
         android:layout_height = "wrap_content"
         android:text = "@string/s_view_btn"
         android:layout_gravity = "bottom|right"  />
</ LinearLayout >

程序代码:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
/**
  * @author allin
  *
  */
public  class  MyListView4 extends  ListActivity {
 
 
     private  List<Map<String, Object>> mData;
     
     @Override
     public  void  onCreate(Bundle savedInstanceState) {
         super .onCreate(savedInstanceState);
         mData = getData();
         MyAdapter adapter = new  MyAdapter( this );
         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;
     }
     
     // ListView 中某项被选中后的逻辑
     @Override
     protected  void  onListItemClick(ListView l, View v, int  position, long  id) {
         
         Log.v( "MyListView4-click" , (String)mData.get(position).get( "title" ));
     }
     
     /**
      * listview中点击按键弹出对话框
      */
     public  void  showInfo(){
         new  AlertDialog.Builder( this )
         .setTitle( "我的listview" )
         .setMessage( "介绍..." )
         .setPositiveButton( "确定" , new  DialogInterface.OnClickListener() {
             @Override
             public  void  onClick(DialogInterface dialog, int  which) {
             }
         })
         .show();
         
     }
     
     
     
     public  final  class  ViewHolder{
         public  ImageView img;
         public  TextView title;
         public  TextView info;
         public  Button viewBtn;
     }
     
     
     public  class  MyAdapter extends  BaseAdapter{
 
         private  LayoutInflater mInflater;
         
         
         public  MyAdapter(Context context){
             this .mInflater = LayoutInflater.from(context);
         }
         @Override
         public  int  getCount() {
             // TODO Auto-generated method stub
             return  mData.size();
         }
 
         @Override
         public  Object getItem( int  arg0) {
             // TODO Auto-generated method stub
             return  null ;
         }
 
         @Override
         public  long  getItemId( int  arg0) {
             // TODO Auto-generated method stub
             return  0 ;
         }
 
         @Override
         public  View getView( int  position, View convertView, ViewGroup parent) {
             
             ViewHolder holder = null ;
             if  (convertView == null ) {
                 
                 holder= new  ViewHolder(); 
                 
                 convertView = mInflater.inflate(R.layout.vlist2, null );
                 holder.img = (ImageView)convertView.findViewById(R.id.img);
                 holder.title = (TextView)convertView.findViewById(R.id.title);
                 holder.info = (TextView)convertView.findViewById(R.id.info);
                 holder.viewBtn = (Button)convertView.findViewById(R.id.view_btn);
                 convertView.setTag(holder);
                 
             } else  {
                 
                 holder = (ViewHolder)convertView.getTag();
             }
             
             
             holder.img.setBackgroundResource((Integer)mData.get(position).get( "img" ));
             holder.title.setText((String)mData.get(position).get( "title" ));
             holder.info.setText((String)mData.get(position).get( "info" ));
             
             holder.viewBtn.setOnClickListener( new  View.OnClickListener() {
                 
                 @Override
                 public  void  onClick(View v) {
                     showInfo();                
                 }
             });
             
             
             return  convertView;
         }
         
     }
     
     
     
     
}

  下面将对上述代码,做详细的解释,listView在开始绘制的时候,系统首先调用getCount()函数,根据他的返回值得到listView的长度(这也是为什么在开始的第一张图特别的标出列表长度),然后根据这个长度,调用getView()逐一绘制每一行。如果你的getCount()返回值是0的话,列表将不显示同样return 1,就只显示一行。

  系统显示列表时,首先实例化一个适配器(这里将实例化自定义的适配器)。当手动完成适配时,必须手动映射数据,这需要重写getView()方法。系统在绘制列表的每一行的时候将调用此方法。getView()有三个参数,position表示将显示的是第几行,covertView是从布局文件中inflate来的布局。我们用LayoutInflater的方法将定义好的vlist2.xml文件提取成View实例用来显示。然后将xml文件中的各个组件实例化(简单的findViewById()方法)。这样便可以将数据对应到各个组件上了。但是按钮为了响应点击事件,需要为它添加点击监听器,这样就能捕获点击事件。至此一个自定义的listView就完成了,现在让我们回过头从新审视这个过程。系统要绘制ListView了,他首先获得要绘制的这个列表的长度,然后开始绘制第一行,怎么绘制呢?调用getView()函数。在这个函数里面首先获得一个View(实际上是一个ViewGroup),然后再实例并设置各个组件,显示之。好了,绘制完这一行了。那 再绘制下一行,直到绘完为止。在实际的运行过程中会发现listView的每一行没有焦点了,这是因为Button抢夺了listView的焦点,只要布局文件中将Button设置为没有焦点就OK了。

运行效果如下图:

源码下载


内容概要:《2024年中国城市低空经济发展指数报告》由36氪研究院发布,指出低空经济作为新质生产力的代表,已成为中国经济新的增长点。报告从发展环境、资金投入、创新能力、基础支撑和发展成效五个维度构建了综合指数评价体系,评估了全国重点城市的低空经济发展状况。北京和深圳在总指数中名列前茅,分别以91.26和84.53的得分领先,展现出强大的资金投入、创新能力和基础支撑。低空经济主要涉及无人机、eVTOL(电动垂直起降飞行器)和直升机等产品,广泛应用于农业、物流、交通、应急救援等领域。政策支持、市场需求和技术进步共同推动了低空经济的快速发展,预计到2026年市场规模将突破万亿元。 适用人群:对低空经济发展感兴趣的政策制定者、投资者、企业和研究人员。 使用场景及目标:①了解低空经济的定义、分类和发展驱动力;②掌握低空经济的主要应用场景和市场规模预测;③评估各城市在低空经济发展中的表现和潜力;④为政策制定、投资决策和企业发展提供参考依据。 其他说明:报告强调了政策监管、产业生态建设和区域融合错位的重要性,提出了加强法律法规建设、人才储备和基础设施建设等建议。低空经济正加速向网络化、智能化、规模化和集聚化方向发展,各地应找准自身比较优势,实现差异化发展。
数据集一个高质量的医学图像数据集,专门用于脑肿瘤的检测和分类研究以下是关于这个数据集的详细介绍:该数据集包含5249张脑部MRI图像,分为训练集和验证集。每张图像都标注了边界框(Bounding Boxes),并按照脑肿瘤的类型分为四个类别:胶质瘤(Glioma)、脑膜瘤(Meningioma)、无肿瘤(No Tumor)和垂体瘤(Pituitary)。这些图像涵盖了不同的MRI扫描角度,包括矢状面、轴面和冠状面,能够全面覆盖脑部解剖结构,为模型训练提供了丰富多样的数据基础。高质量标注:边界框是通过LabelImg工具手动标注的,标注过程严谨,确保了标注的准确性和可靠性。多角度覆盖:图像从不同的MRI扫描角度拍摄,包括矢状面、轴面和冠状面,能够全面覆盖脑部解剖结构。数据清洗与筛选:数据集在创建过程中经过了彻底的清洗,去除了噪声、错误标注和质量不佳的图像,保证了数据的高质量。该数据集非常适合用于训练和验证深度学习模型,以实现脑肿瘤的检测和分类。它为开发医学图像处理中的计算机视觉应用提供了坚实的基础,能够帮助研究人员和开发人员构建更准确、更可靠的脑肿瘤诊断系统。这个数据集为脑肿瘤检测和分类的研究提供了宝贵的资源,能够帮助研究人员开发出更准确、更高效的诊断工具,从而为脑肿瘤患者的早期诊断和治疗规划提供支持。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值