listActivity和ExpandableListActivity的简单用法
http://www.cnblogs.com/limingblogs/archive/2011/10/09/2204866.html
今天自己简单的总结了listActivity和ExpandableListActivity二者的简单用法。
首先,先说一下listActivity的用法:
ListActivity是一个绑定到一个数据源,并且用来显示这一串数据的Activity。ListActivity拥有一个listview对象来实现数据源的绑定与显示,通常会是一个array或者一个拥有查询结果的cursor.ListActivity本身有一个默认的layout,其中包含一个全屏的list。如果用默认的layout,你必须要在onCreate()中注释掉setContentView()那一句。但 是如果你如果你想要定制自己的layout你可以创建一个你自己的layout文件,并且在onCreate()中调用setContentView() 来指定这个layout.,需要注意的是你自己的layout中必须用到系统给定的id为"@android:id/list"的ListView。
下面是一个简单的例子,运行结果如下:

activityde 代码如下:
| packagelm.muilThreadDownload;  importjava.util.ArrayList; importjava.util.HashMap; importjava.util.List; importjava.util.Map;  importlm.muilThreadEntity.DownloadInfo; importlm.muilThreadService.Downloader;  importandroid.app.ListActivity; importandroid.os.Bundle; importandroid.os.Handler; importandroid.os.Message; importandroid.util.Log; importandroid.view.View; importandroid.widget.LinearLayout; importandroid.widget.ProgressBar; importandroid.widget.SimpleAdapter; importandroid.widget.TextView; importandroid.widget.Toast;  publicclassMuilThreadDownloadActivity extendsListActivity { @Override    publicvoidonCreate(Bundle savedInstanceState) {         super.onCreate(savedInstanceState);         setContentView(R.layout.main);         showListView();//显示listView     }      privatevoidshowListView() {         List<Map<String, String>> data = newArrayList<Map<String, String>>();         Map<String, String> map = newHashMap<String, String>();         map.put("name", "liming.mp3");         data.add(map);         map = newHashMap<String, String>();         map.put("name", "liming2.mp3");         data.add(map);         map = newHashMap<String, String>();         map.put("name", "liming3.mp3");         data.add(map);         SimpleAdapter adapter = newSimpleAdapter(this, data,                 R.layout.list_item, newString[] { "name"},                 newint[] { R.id.tv_resouce_name });         setListAdapter(adapter);     } } | 
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"    android:id="@+id/mainlayout"    > <ListView     android:id="@android:id/list"    android:layout_width="fill_parent"    android:layout_height="fill_parent"    /> </LinearLayout> | 
我们看到,上面的ListView的id用的就是系统自带的"@android:id/list"。
其次,我们也可以不用布局文件,自己定义一个ListView的对象,通过id来获得加载的视图文件。具体代码如下:
| packagelm.mediaPlayer;  importandroid.app.ListActivity; importandroid.content.Intent; importandroid.content.IntentFilter; importandroid.net.Uri; importandroid.os.Bundle; importandroid.os.Environment; importandroid.util.Log; importandroid.view.Menu; importandroid.view.MenuItem; importandroid.view.View; importandroid.widget.ArrayAdapter; importandroid.widget.ListView;   publicclassMyMediaPlayerActivity extendsListActivity {     privateListView listView;     privateScannerSDCardReceiver receiver;     privatebooleanb = false;     @Override    publicvoidonCreate(Bundle savedInstanceState) {         super.onCreate(savedInstanceState);         listView = newListView(this);         listView.setId(android.R.id.list);//获得listView的id         setContentView(listView);//加载listView         showListView();     }          privatevoidshowListView() {//显示listView         String[] from = {"全部音乐","最近播放音乐"};         ArrayAdapter<String> adapter = newArrayAdapter<String>(this,android.R.layout.simple_list_item_1,from);         listView.setAdapter(adapter);     } } | 
运行结果如下:

最后,我们看一下ExpandableListActivity的用法,开始运行效果图如下:

当我们展开向右的箭头时,效果如下:

我们看到“国家”和“语言”分别是组名,每个组名下面还有很多child(中国,美国),(汉语,英语),其实ExpandableListActivity就是实现这样的功能,能更方便的现实一些列表信息。具体代码如下:
| packagelm.expendablelistAcitivity; importjava.util.ArrayList; importjava.util.HashMap; importjava.util.List; importjava.util.Map;  importandroid.app.ExpandableListActivity; importandroid.os.Bundle; importandroid.widget.SimpleExpandableListAdapter; //首先继承ExpandableListActivity publicclassMyExpendableListActivityActivity extendsExpandableListActivity{     /** Called when the activity is first created. */    @Override    publicvoidonCreate(Bundle savedInstanceState) {         super.onCreate(savedInstanceState);         setContentView(R.layout.main);                  List<Map<String,String>> list = newArrayList<Map<String,String>>();//组名         Map<String,String> map1 = newHashMap<String,String>();         map1.put("group", "国家");         Map<String,String> map2 = newHashMap<String,String>();         map2.put("group", "语言");         list.add(map1);         list.add(map2);                  List<Map<String,String>> listChild1 = newArrayList<Map<String,String>>();//child         Map<String,String> map3 = newHashMap<String,String>();         map3.put("country", "中国");         listChild1.add(map3);         Map<String,String> map4 = newHashMap<String,String>();         map4.put("country", "美国");         listChild1.add(map4);                  List<Map<String,String>> listChild2 = newArrayList<Map<String,String>>();//child         Map<String,String> map5 = newHashMap<String,String>();         map5.put("country", "汉语");         listChild2.add(map5);         Map<String,String> map6 = newHashMap<String,String>();         map6.put("country", "英语");         listChild2.add(map6);                  List<List<Map<String,String>>> childs = newArrayList<List<Map<String,String>>>();//将两个child加入的集合中         childs.add(listChild1);         childs.add(listChild2);         SimpleExpandableListAdapter adapter = newSimpleExpandableListAdapter(this, list, R.layout.group, newString[]{"group"},                 newint[]{R.id.tv_group}, childs, R.layout.child, newString[]{"country"}, newint[]{R.id.tv_child});         setListAdapter(adapter);//适配器     } } | 
| 其中group的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"    > <TextView     android:id="@+id/tv_group"    android:layout_width="fill_parent"    android:layout_height="fill_parent"    android:paddingLeft="60px"    android:paddingTop="10px"    android:paddingBottom="10px"    android:textSize="25sp"    android:text="无数据"    /> </LinearLayout> | 
child的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"    > <TextView     android:id="@+id/tv_child"    android:layout_width="fill_parent"    android:layout_height="fill_parent"    android:paddingLeft="50px"    android:paddingTop="5px"    android:paddingBottom="5px"    android:textSize="20sp"    android:text="无数据"    /> </LinearLayout> | 
好了,以上就是我总结的内容,希望大家多多指教!
listActivity和ExpandableListActivity的简单用法的更多相关文章
- CATransition(os开发之画面切换) 的简单用法
		CATransition 的简单用法 //引进CATransition 时要添加包“QuartzCore.framework”,然后引进“#import <QuartzCore/QuartzCo ... 
- jquery.validate.js 表单验证简单用法
		引入jquery.validate.js插件以及Jquery,在最后加上这个插件的方法名来引用.$('form').validate(); <!DOCTYPE html PUBLIC " ... 
- NSCharacterSet 简单用法
		NSCharacterSet 简单用法 NSCharacterSet其实是许多字符或者数字或者符号的组合,在网络处理的时候会用到 NSMutableCharacterSet *base = [NSMu ... 
- [转]Valgrind简单用法
		[转]Valgrind简单用法 http://www.cnblogs.com/sunyubo/archive/2010/05/05/2282170.html Valgrind的主要作者Julian S ... 
- Oracle的substr函数简单用法
		substr(字符串,截取开始位置,截取长度) //返回截取的字 substr('Hello World',0,1) //返回结果为 'H' *从字符串第一个字符开始截取长度为1的字符串 subst ... 
- Ext.Net学习笔记19:Ext.Net FormPanel 简单用法
		Ext.Net学习笔记19:Ext.Net FormPanel 简单用法 FormPanel是一个常用的控件,Ext.Net中的FormPanel控件同样具有非常丰富的功能,在接下来的笔记中我们将一起 ... 
- TransactionScope简单用法
		记录TransactionScope简单用法,示例如下: void Test() { using (TransactionScope scope = new TransactionScope()) { ... 
- WPF之Treeview控件简单用法
		TreeView:表示显示在树结构中分层数据具有项目可展开和折叠的控件 TreeView 的内容是可以包含丰富内容的 TreeViewItem 控件,如 Button 和 Image 控件.TreeV ... 
- SQL*Plus break与compute的简单用法
		SQL*Plus break与compute的简单用法在SQL*Plus提示符下输出求和报表,我们可以借助break与compute两个命令来实现.这个两个命令简单易用,可满足日常需求,其实质也相当于 ... 
随机推荐
- 手动更改WIN远程桌面端口,要改两个地方的注册表哟
			看到我的服务器有老多人在用桌面连接,虽然进不去,但他们不停地试,浪费掉不少服务器资源,我看到网上有不少关于修改3389的介绍.修改3389的工具,一些工具一点用都没有,纯属扯淡.修改后照样是3389. ... 
- 14.6.3.3 Making the Buffer Pool Scan Resistant
			14.6.3.3 Making the Buffer Pool Scan Resistant 相比使用一个严格的LRU算法,InnoDB 使用一个技术来最小化数据总量 带入到buffer pool 而 ... 
- spin_count
			oracle的一个隐藏参数_spin_count当中记录了这个值,如果超过这个参数就那这个进程就释放cpu进入睡眠状态.(然后这里有了争议,传统的说法是在睡眠了一段时间以后会醒来,但是也有人说是进入了 ... 
- 【转】Eclipse使用git最简易流程
			原文网址:http://www.cnblogs.com/ZhangWanFan/p/3993733.html git有诸多好处,网上都说的很清楚了,在这里我不再赘述.对于我来说,私下里想做一些项目,而 ... 
- Spark SQL利器:cacheTable/uncacheTable
			Spark相对于Hadoop MapReduce有一个很显著的特性就是“迭代计算”(作为一个MapReduce的忠实粉丝,能这样说,大家都懂了吧),这在我们的业务场景里真的是非常有用. 假设我们有 ... 
- (转载)PHP json_encode() 函数介绍
			(转载) 在 php 中使用 json_encode() 内置函数(php > 5.2)可以使用得 php 中数据可以与其它语言很好的传递并且使用它. 这个函数的功能是将数值转换成json数据存 ... 
- 机器安装第二个tomcat ,出现报错如何解决
			1.本机安装第二个 tomcat 后,出现 报错如下图所示 最后解决办法 是 在安装的时候 ,windows 服务名称 和 另一个tomcat 起不一样的 名称就可以了 如下图 
- PHP 小代码
			//获取网上的一个文件function getUrlImage($url, $file = '', $maxExe = 0, $safe = false){ $urlExt = explode('.' ... 
- lightoj 1030 概率dp
			题目链接:http://lightoj.com/volume_showproblem.php?problem=1030 #include<cstdio> #include<cstri ... 
- Inheritance - SGU 129(线段与多边形相交的长度)
			题目大意:给一个凸多边形(点不是按顺序给的),然后计算给出的线段在这个凸多边形里面的长度,如果在边界不计算. 分析:WA2..WA3...WA4..WA11...WA的无话可说,总之细节一定考虑清楚, ... 
