今天自己简单的总结了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 代码如下:

package lm.muilThreadDownload;import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;import lm.muilThreadEntity.DownloadInfo;
import lm.muilThreadService.Downloader;import android.app.ListActivity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.SimpleAdapter;
import android.widget.TextView;
import android.widget.Toast;public class MuilThreadDownloadActivity extends ListActivity {
@Overridepublic void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.main);showListView();//显示listView}private void showListView() {List<Map<String, String>> data = new ArrayList<Map<String, String>>();Map<String, String> map = new HashMap<String, String>();map.put("name", "liming.mp3");data.add(map);map = new HashMap<String, String>();map.put("name", "liming2.mp3");data.add(map);map = new HashMap<String, String>();map.put("name", "liming3.mp3");data.add(map);SimpleAdapter adapter = new SimpleAdapter(this, data,R.layout.list_item, new String[] { "name" },new int[] { 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">
<ListViewandroid:id="@android:id/list"  android:layout_width="fill_parent" android:layout_height="fill_parent" />
</LinearLayout>

我们看到,上面的ListView的id用的就是系统自带的"@android:id/list"。

其次,我们也可以不用布局文件,自己定义一个ListView的对象,通过id来获得加载的视图文件。具体代码如下:

package lm.mediaPlayer;import android.app.ListActivity;
import android.content.Intent;
import android.content.IntentFilter;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;public class MyMediaPlayerActivity extends ListActivity {private ListView listView;private ScannerSDCardReceiver receiver;private boolean b = false;@Overridepublic void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);listView = new ListView(this);listView.setId(android.R.id.list);//获得listView的idsetContentView(listView);//加载listViewshowListView();}private void showListView() {//显示listViewString[] from = {"全部音乐","最近播放音乐"};ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,from);listView.setAdapter(adapter);}
}

运行结果如下:

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

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

我们看到“国家”和“语言”分别是组名,每个组名下面还有很多child(中国,美国),(汉语,英语),其实ExpandableListActivity就是实现这样的功能,能更方便的现实一些列表信息。具体代码如下:

package lm.expendablelistAcitivity;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;import android.app.ExpandableListActivity;
import android.os.Bundle;
import android.widget.SimpleExpandableListAdapter;
//首先继承ExpandableListActivity
public class MyExpendableListActivityActivity extends ExpandableListActivity{/** Called when the activity is first created. */@Overridepublic void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.main);List<Map<String,String>> list = new ArrayList<Map<String,String>>();//组名Map<String,String> map1 = new HashMap<String,String>();map1.put("group", "国家");Map<String,String> map2 = new HashMap<String,String>();map2.put("group", "语言");list.add(map1);list.add(map2);List<Map<String,String>> listChild1 = new ArrayList<Map<String,String>>();//childMap<String,String> map3 = new HashMap<String,String>();map3.put("country", "中国");listChild1.add(map3);Map<String,String> map4 = new HashMap<String,String>();map4.put("country", "美国");listChild1.add(map4);List<Map<String,String>> listChild2 = new ArrayList<Map<String,String>>();//childMap<String,String> map5 = new HashMap<String,String>();map5.put("country", "汉语");listChild2.add(map5);Map<String,String> map6 = new HashMap<String,String>();map6.put("country", "英语");listChild2.add(map6);List<List<Map<String,String>>> childs = new  ArrayList<List<Map<String,String>>>();//将两个child加入的集合中childs.add(listChild1);childs.add(listChild2);SimpleExpandableListAdapter adapter = new SimpleExpandableListAdapter(this, list, R.layout.group, new String[]{"group"},new int[]{R.id.tv_group}, childs, R.layout.child, new String[]{"country"}, new int[]{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">
<TextViewandroid: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">
<TextViewandroid: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>

好了,以上就是我总结的内容,希望大家多多指教!

转载于:https://www.cnblogs.com/limingblogs/archive/2011/10/09/2204866.html

listActivity和ExpandableListActivity的简单用法相关推荐

  1. html.renderaction 控制器,Html.RenderAction简单用法

    CATransition(os开发之画面切换) 的简单用法 CATransition 的简单用法 //引进CATransition 时要添加包"QuartzCore.framework&qu ...

  2. 反编译工具jad简单用法

    反编译工具jad简单用法 下载地址: [url]http://58.251.57.206/down1?cid=B99584EFA6154A13E5C0B273C3876BD4CC8CE672& ...

  3. QCustomPlot的简单用法总结

    QCustomPlot的简单用法总结 第一部分:QCustomPlot的下载与安装 第二部分:QCustomPlot在VS2013+QT下的使用 QCustomPlot的简单用法总结    写在前面, ...

  4. python matplotlib 简单用法

    python matplotlib 简单用法 具体内容请参考官网 代码 import matplotlib.pyplot as plt import numpy as np # 支持中文 plt.rc ...

  5. Windump网络命令的简单用法

    Windump网络命令的简单用法 大家都知道,unix系统下有个tcpdump的抓包工具,非常好用,是做troubleshooting的好帮手.其实在windows下也有一个类似的工作,叫windum ...

  6. Android TabLayout(选项卡布局)简单用法实例分析

    本文实例讲述了Android TabLayout(选项卡布局)简单用法.分享给大家供大家参考,具体如下: 我们在应用viewpager的时候,经常会使用TabPageIndicator来与其配合.达到 ...

  7. shell expect的简单用法

    为什么需要expect?     我们通过Shell可以实现简单的控制流功能,如:循环.判断等.但是对于需要交互的场合则必须通过人工来干预,有时候我们可能会需要实现和交互程序如 telnet服务器等进 ...

  8. Shellz中awk的简单用法

    其实shell脚本的功能常常被低估.在实际应用中awk sed 等用法可以为shell提供更为强大的功能.下面我们将一下awk调用的简单方法进行了总结.方便同学们学习: awk的简单用法: 第一种调用 ...

  9. python装饰器实例-Python装饰器原理与简单用法实例分析

    本文实例讲述了Python装饰器原理与简单用法.分享给大家供大家参考,具体如下: 今天整理装饰器,内嵌的装饰器.让装饰器带参数等多种形式,非常复杂,让人头疼不已.但是突然间发现了装饰器的奥秘,原来如此 ...

最新文章

  1. iPhone SDK Examples
  2. mybatis注解开发
  3. 为什么在大型 Angular 应用里我们需要使用 ngrx
  4. My appointment应用点了Edit后的roundtrip分析
  5. 前端学习(1686):前端系列javascript基础面试总结
  6. mysql 索引越界_mysql隐式转换造成索引失效的事故总结
  7. 百度知道1000指数的关键词留链接排名到第一的实战案例
  8. 问题描述】原始题目:一只公鸡 5 文钱,一只母鸡 3 文钱,三只小鸡 1 文钱,用 100 文钱买 100 只鸡,请问公鸡,母鸡,小鸡各多少只?(推广)
  9. 小赛毛游C记-初识C语言(2)
  10. 支持查看朋友圈的微信Mac版客户端
  11. Podman 部署私有镜像仓库
  12. ExaGrid入围2021年网络计算大奖
  13. win10此电脑桌面显示(桌面显示)
  14. 职场老司机:能走捷径,为什么不呢?
  15. arm汇编指令探究之 ldmia
  16. 计算机考试一级表格考试题,计算机一级Word 表格考试题
  17. 华为服务器培训文档,服务器云培训
  18. 土地面积计算单位及其换算单位-亩分厘
  19. 并行计算机原理实验报告,b计算机原理b实验报告.doc
  20. 将进酒计算机应用技术学院信息门户,智慧校园信息门户

热门文章

  1. java统计一个字符串中每个字符出现的次数_剑指offer算法题054:字符流中第一个不重复的字符...
  2. 程序员最喜欢用的在线IDE代码编译器,什么?你竟然不知道!
  3. org.apache.hadoop.hdfs.server.namenode.SafeModeException
  4. 节能以太网EEE(Energy Efficient Ethernet)
  5. 商业银行为什么大量组织高净值小规模活动?
  6. 387. First Unique Character in a String QuestionEditorial Solution
  7. Linux--根文件系统的挂载过程分析
  8. cad考试题库绘图题答案_证券从业资格考试证券市场基本法律法规题库答案
  9. 常用于评价回归模型优劣的统计量包括( )。_第四十一讲 R-判断回归模型性能的指标...
  10. b类 蚂蚁金服_股权设计与合伙制,解析蚂蚁金服与华为的顶层结构