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. ehcache2拾遗之cache持久化

    问题描述 应用在使用过程中会需要重启等,但是如果ehcache随着应用一起重启,那么刚重启的时候就会出现大量的miss,需要一定的访问量来重建缓存,如果缓存能够持久化,重启之后可以复用将会有助于缓解重 ...

  2. xamarin UWP设置HUD加载功能

    使用xamarin开发的时候经常用到加载HUD功能,就是我们常见的一个加载中的动作,Android 下使用 AndHUD , iOS 下使用 BTProgressHUD, 这两个在在 NuGet 上都 ...

  3. H5 调用摄像头

    WebRTC(Web Real-Time Communication,网页实时通信),是一个支持网页浏览器进行实时语音对话或视频对话的API. 1.getUserMedia 要播放摄像头的影像,首先需 ...

  4. 第十六回 IoC组件Unity续~批量动态为Unity添加类型和行为

    回到目录 之前的一篇Unity的文章主要是基本的实现,并没有什么特别的地方,使用Unity可以方便的实现应用程序的IoC控制反转,这给我们的应用程序在耦合度上变得高了,同时可测试性加强了,当然,这些的 ...

  5. PDO事务处理

    PDO事务处理 2014-9-3 10:44:19 By jiancaigege==================================== 概要:将多条sql操作(增删改)作为一个操作单 ...

  6. CAR

    24.编写一个Car类,具有String类型的属性品牌,具有功能drive: 定义其子类Aodi和Benchi,具有属性:价格.型号:具有功能:变速: 定义主类E,在其main方法中分别创建Aodi和 ...

  7. 批处理集锦——(5)使用dir查找文件

    eg dir /a-d /s /b *.bat -d表示不显示.bat结尾的文件夹

  8. SQLServer查看死锁

    SQLServer查看死锁 if exists ( select * from sys.procedures where name like '%USP_ShowLocks%' ) drop proc ...

  9. SQL*Loader之CASE7

    CASE7 1. SQL脚本 case7包含两个SQL脚本,一个是删除脚本ulcase7e.sql,一个是创建脚本ulcase7s.sql [oracle@node3 ulcase]$ cat ulc ...

  10. Spring学习总结(六)——Spring整合MyBatis完整示例

    为了梳理前面学习的内容<Spring整合MyBatis(Maven+MySQL)一>与<Spring整合MyBatis(Maven+MySQL)二>,做一个完整的示例完成一个简 ...