Android中经常用到列表,ListActivity是实现列表的一种好方法。

使用ListActivity的方法,首先定义布局文件:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" > <LinearLayout
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:layout_weight="5" > <TextView
android:id="@+id/textView1"
style="@style/my_style"
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:layout_weight="4" /> <TextView
android:id="@+id/textView2"
style="@style/my_style"
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:layout_gravity="center_vertical|center_horizontal"
android:layout_weight="1"
android:gravity="center_vertical|center_horizontal"
android:text="应用名称" /> <TextView
android:id="@+id/textView3"
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:layout_weight="4"
android:gravity="center_vertical|center_horizontal"
android:text="是否系统应用" /> </LinearLayout> <ListView
android:id="@id/android:list"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:layout_weight="1" > </ListView> <Button
android:id="@+id/btn_apk"
style="@style/my_style"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:layout_weight="5"
android:text="卸载" /> </LinearLayout>

其中,最关键的就是ListView控件。

然后,再用一个布局文件定义列表中的每一行:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" > <CheckBox
android:id="@+id/chk_apk"
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:layout_weight="4" /> <TextView
android:id="@+id/txt_item_name"
style="@style/my_style"
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:layout_weight="1"
android:text="TextView" /> <TextView
android:id="@+id/txt_item_flag"
style="@style/my_style"
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:layout_weight="4"
android:text="TextView" /> </LinearLayout>

最后,在类文件中使用SimpleAdapter类把列表显示出来。

package com.hzhi.sysinfor;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map; import android.app.ListActivity;
import android.content.Context;
import android.content.pm.PackageInfo;
import android.content.pm.PackageItemInfo;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.widget.SimpleAdapter; public class apk extends ListActivity{ Context mCtx;
SimpleAdapter my_adaptor; protected void onCreate(Bundle savedInstanceState){         super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_list_apk);
        my_adaptor = new SimpleAdapter(this,
get_data(),
R.layout.list_item_apk,
new String[]{"name", "flag"},
new int[]{R.id.txt_item_name, R.id.txt_item_flag});
    setListAdapter(my_adaptor);
    mCtx = MainActivity.mContext; } }

其中get_data()返回一个List<Map<String,String>>,R.layout.activity_list_apk是定义整个列表的布局文件,R.layout.list_item_apk是定义列表中每一行的布局文件。

运行的结果:

ListActivity的使用的更多相关文章

  1. ListActivity的CheckBox点击事件

    在一个ListActivity中,可能有很多行,如何在某个动作(如点击某个按钮)时判断哪些行的CheckBox被选中了,哪些行的CheckBox没有被选中,这是一个需要解决的问题. 首先,需要让Bas ...

  2. 在ListActivity中显示图标

    在ListActivity中显示图标,好像并不复杂,实现起来却不轻松. 首先,定义列表中的每一行,这里不是用xml文件定义,而是用一个类定义,CheckBox.ImageView.TextView等控 ...

  3. android ListView详解继承ListActivity

    [转]http://www.cnblogs.com/allin/archive/2010/05/11/1732200.html 在android开发中ListView是比较常用的组件,它以列表的形式展 ...

  4. Activity中与ListActivity中使用listview区别

    Activity中与ListActivity中使用listview区别 一.Activity中与ListActivity中使用listview区别(本身没多大区别,只是ListActivity在lis ...

  5. Android ListView ListActivity PreferenceActivity背景变黑的问题ZT

    Android ListView ListActivity PreferenceActivity背景变黑的问题 ListView在滚动时背景会变暗甚至变黑,这个要从Listview的效果说起,默认的L ...

  6. android学习笔记12——ListView、ListActivity

    ListView.ListActivity ==> ListView以垂直列表的形式显示所有列表项. 创建ListView的方式: 1.直接使用ListView创建 2.Activity继承Li ...

  7. 通过ListActivity使用ListView布局方法

    先简单的介绍一下ListActivity ListActivity是一个专门显示ListView的Activity类,它内置了ListView对象,只要我们设置了数据源,就会自动地显示出来.ListA ...

  8. Android 自学之列表视图ListView和ListActivity

    ListView是手机系统中使用非常广泛的一种组件,它以垂直列表的形式显示所有列表项. 创建ListView有两种方式: 直接使用ListView创建. 让Activity继承ListActivity ...

  9. AdapterView及其子类之二:使用ListActivity及ArrayAdapter创建列表

    见归档项目ListActivityDemo.zip. 基本步骤如下: 1.创建一个TextView,用于指定每一个ListView的格式 <?xml version="1.0" ...

随机推荐

  1. EF架构~CodeFirst自关联表的插入

    回到目录 这个文章对之前EF的一个补充,对于一些自关联表的添加,如果你建立了表约束确实有这种问题,一般主键为整形自增,父ID为可空,这时,在添加时如果不为ID赋值,结果就会出错. 错误: 无法确定依赖 ...

  2. Atitit  深入理解命名空间namespace  java c# php js

    Atitit  深入理解命名空间namespace  java c# php js 1.1. Namespace还是package1 1.2. import同时解决了令人头疼的include1 1.3 ...

  3. Atitit 发帖机系列(7) 词法分析的方法attilax大总结)

    Atitit 发帖机系列(7) 词法分析的方法attilax大总结) 1.1. 词法分析貌似俩大方法,一个直接根据状态图转换,一个根据dfa1 1.2. switchcase或者ifelse 最原始方 ...

  4. Atitit 常用比较复杂的图像滤镜 attilax大总结

    Atitit 常用比较复杂的图像滤镜 attilax大总结 像素画滤镜 水彩油画滤镜 素描滤镜 梦幻镜 特点是中央集焦,周围景物朦化微带光晕,使人产生如入梦境的感觉.常用于拍摄婚纱.明星照,也用于其它 ...

  5. 搭建LNAMP环境(四)- 源码安装PHP7

    上一篇:搭建LNAMP环境(三)- 源码安装Apache2.4 一.安装PHP7 1.yum安装编译php需要的包 yum -y install libxml2 libxml2-devel curl- ...

  6. Searching for a valid kernel header path... The path "" is not a valid path to the ***** kernel headers. Would you like to change it? [yes]

    在centos 6.5安装vmtools时候,解压包,mount挂载后,安装pl过程中报 这个问题,半天没有解决,google 后发现这样就行了 I installed the kernel-deve ...

  7. IOS开发之控件篇UICollectionViewControllor第一章 - 普通介绍

    1.介绍 UICollectionView和UICollectionViewControllor是IOS6.0后引入的新控件 使用UICollectionView必须实现三个接口: UICollect ...

  8. Android开发之广播

    广播是Android开发中的一个重要的功能,在Android里面有各式各样的广播,比如:电池的状态变化.信号的强弱状态.电话的接听和短信的接收等等,现在给大家简单介绍一下系统发送.监听这些广播的机制. ...

  9. python--基础学习(四)自然字符串、重复字符串、子字符串

    python系列均基于python3.4环境 1.自然字符串和重复字符串 代码示例: str1=r'hello \npython' str2='hello \npython' str3="h ...

  10. java.lang.Long cannot be cast to java.lang.Integer解决办法

    情景: mybatis连接oracle 报错: 测试增的时候,报错 Java.lang.Long cannot be cast to  java.lang.Integer:删改没有报错. 排查过程: ...