Android ListActivity应用技巧全解

移动开发 Android
在这篇文章中我们先通过一段代码的解读,为大家详细介绍Android ListActivity的应用方法,以方便大家的学习应用。

今天为大家带来的是有关Android ListActivity的相关内容的介绍。我们可以从这篇文章中介绍的内容详细的对这一方面的知识进行一个全面的认识。首先看看android.app包里的几个类。首先是这个在平台自的例子中被广泛使用的Android ListActivity。这个类其实就是一个含有一个ListView组件的Activity类。也就是说,如果我们直接在一个普通的Activity中自己加一个ListView也是完全可以取代这个Android ListActivity的,只是它更方便而已,方便到什么程度呢?来做个例子瞧瞧。 

  1. public class HelloTwoB extends ListActivity   
  2. ...{   
  3. public void onCreate(Bundle icicle) ...{   
  4. super.onCreate(icicle);   
  5. setTheme(android.R.style.Theme_Dark);   
  6. setContentView(R.layout.mainb);   
  7. List< String> items = fillArray();   
  8. ArrayAdapter< String> adapter = new ArrayAdapter< String>
    (this,R.layout.list_row,items);   
  9. this.setListAdapter(adapter);   
  10. }   
  11. private List< String> fillArray()   
  12. ...{   
  13. List< String> items = new ArrayList< String>();   
  14. items.add("日曜日");   
  15. items.add("月曜日");   
  16. items.add("火曜日");   
  17. items.add("水曜日");   
  18. items.add("木曜日");   
  19. items.add("金曜日");   
  20. items.add("土曜日");   
  21. return items;   
  22. }   
  23. @Override   
  24. protected void onListItemClick(ListView l, 
    View v, int position, long id)   
  25. ...{   
  26. TextView txt = (TextView)this.findViewById(R.id.text);   
  27. txt.setText("あすは "+l.getSelectedItem().toString()+"です。");   
  28. }   

的确可以简单到只需准备一个List对象并借助Adapter就可以构造出一个列表。重载onListItemClick方法可以响应选择事件,利用***个参数可以访问到这个ListView实例以得到选中的条目信息。这里有一点要说明的,就是如果更简单的话,其实连那个setContentView都可以不要了,Android也会自动帮我们构造出一个全屏的列表。但是本例中我们需要一个TextView来显示选中的条目,所以我们需要一个layout.mainb描述一下这个列表窗口。

  1. < ?xml version="1.0" encoding="utf-8"?>   
  2. < LinearLayout xmlns:android=
    "http://schemas.android.com/apk/res/android"   
  3. android:orientation="vertical"   
  4. android:layout_width="fill_parent"   
  5. android:layout_height="fill_parent"   
  6. >   
  7. < TextView id="@+id/text"   
  8. android:layout_width="fill_parent"   
  9. android:layout_height="wrap_content"   
  10. android:text=""   
  11. />   
  12. < ListView id="@id/android:list"   
  13. android:layout_width="fill_parent"   
  14. android:layout_height="0dip"   
  15. android:layout_weight="1"   
  16. android:drawSelectorOnTop="false"   
  17. />   
  18. < /LinearLayout>  

在Android ListActivity操作中需要注意的是那个ListView的ID,是系统自定义的android:list,不是我们随便取的,否则系统会说找不到它想要的listview了。然后,在这个listview之外,我们又增加了一个TextView,用来显示选中的条目。

再来说说这里用到的ArrayAdapter,它的构造函数中第二个参数是一个资源ID,ArrayAdapter的API文档中说是要求用一个包含 TextView的layout文件,平台用它来显示每个选择条目的样式,这里的取值是R.layout.list_row,所以,我们还有一个list_row.xml文件来描述这个布局,相当简单。

  1. < ?xml version="1.0" encoding="utf-8"?>   
  2. < LinearLayout xmlns:android=
    "http://schemas.android.com/apk/res/android"   
  3. android:orientation="vertical"   
  4. android:layout_width="fill_parent"   
  5. android:layout_height="fill_parent"   
  6. >   
  7. < TextView id="@+id/item"   
  8. xmlns:android="http://schemas.android.com/apk/res/android"   
  9. android:layout_width="wrap_content"   
  10. android:layout_height="wrap_content"/>   
  11. < TextView id="@+id/item2"   
  12. xmlns:android="http://schemas.android.com/apk/res/android"   
  13. android:layout_width="wrap_content"   
  14. android:layout_height="wrap_content"/>   
  15. < /LinearLayout>  

从ArrayAdapter上溯到BaseAdapter,发现还有几个同源的Adapter也应该可以使用,象SimpleAdapter和CursorAdapter,还是做个例子来实验一下吧。

Android ListActivity的相关内容就为大家介绍到这里。

【编辑推荐】

  1. Android Timer编写方式深解
  2. Android SQLite数据库应用技巧分享
  3. Android模拟器应用技巧详细介绍
  4. Android画图技巧应用方式分析
  5. Android dialog应用经验总结
责任编辑:曹凯 来源: javaeye.com
相关推荐

2013-12-19 13:51:12

Android ApiAndroid开发Android SDK

2009-11-11 18:07:07

路由器设备

2009-12-17 17:37:42

Ruby on Rai

2010-09-07 09:35:22

2010-02-04 14:11:26

以太网交换机

2009-11-03 09:56:48

2010-01-27 17:45:15

Android应用技巧

2010-03-04 16:38:37

Android开发技巧

2010-03-05 13:46:12

Android编程学习

2010-07-13 14:44:11

SNMP服务设置

2009-09-23 17:36:26

Hibernate优点

2010-07-14 16:21:31

Telnet服务配置

2010-01-04 09:39:39

Silverlight

2010-07-28 22:20:10

RIP路由配置

2010-04-20 11:51:31

负载均衡

2011-03-30 10:07:02

Zabbix安装

2010-07-13 13:59:04

ICMP协议

2010-09-25 13:07:50

DHCP协议结构

2010-01-25 11:09:58

Android Htt

2010-01-25 17:43:13

Android资源
点赞
收藏

51CTO技术栈公众号