先简单的介绍一下ListActivity

ListActivity是一个专门显示ListView的Activity类,它内置了ListView对象,只要我们设置了数据源,就会自动地显示出来。ListActivity和普通的Activity没有太大的差别,不同就是对显示ListView做了许多优化,方面显示而已。

我们知道

列表的显示需要三个元素:

1.ListVeiw 用来展示列表的View。

2.适配器 用来把数据映射到ListView上的中介。

3.数据    具体的将被映射的字符串,图片,或者基本组件。

根据列表的适配器类型,列表分为三种,ArrayAdapter,SimpleAdapter和SimpleCursorAdapter

simpleAdapter的扩展性最好,可以定义各种各样的布局出来,可以放上ImageView(图片),还可以放上Button(按钮),CheckBox(复选框)等等。

下面的代码都直接继承了ListActivity,采用simpleAdapter

先上两个图:

上代码

MoreActivity.java
______________________________________

  1. import java.util.ArrayList;
  2. import java.util.HashMap;
  3. import java.util.List;
  4. import java.util.Map;
  5. import android.app.AlertDialog;
  6. import android.app.ListActivity;
  7. import android.content.DialogInterface;
  8. import android.os.Bundle;
  9. import android.view.View;
  10. import android.widget.ListView;
  11. import android.widget.SimpleAdapter;
  12. public class MoreActivity extends ListActivity {
  13. private String discount_info;
  14. private String recommend_shop;
  15. private String about_us;
  16. @Override
  17. public void onCreate(Bundle savedInstanceState) {
  18. super.onCreate(savedInstanceState);
  19. SimpleAdapter adapter = new SimpleAdapter(this,getData(),R.layout.more,
  20. new String[]{"title","info","img"},
  21. new int[]{R.id.title,R.id.info,R.id.img});
  22. setListAdapter(adapter);
  23. }
  24. private List<HashMap> getData() {
  25. List<HashMap> list = new ArrayList<HashMap>();
  26. Map map = new HashMap();
  27. discount_info=this.getString(R.string.discount_info);//相应字符串在String.xml中设置
  28. recommend_shop=this.getString(R.string.recommend_shop);
  29. about_us=this.getString(R.string.about_us);
  30. map.put("title", discount_info);
  31. map.put("info", "android 1");
  32. map.put("img", R.drawable.i1);
  33. list.add(map);
  34. map = new HashMap();
  35. map.put("title", recommend_shop);
  36. map.put("info", "android 2");
  37. map.put("img", R.drawable.i2);
  38. list.add(map);
  39. map = new HashMap();
  40. map.put("title", about_us);
  41. map.put("info", "android 3");
  42. map.put("img", R.drawable.i3);
  43. list.add(map);
  44. return list;
  45. }
  46. @Override
  47. protected void onListItemClick(ListView l, View v, int position, long id) {
  48. // TODO Auto-generated method stub
  49. super.onListItemClick(l, v, position, id);
  50. if(0==id)
  51. {
  52. showInfo();//点击第一项执行函数,其它id可以自己设置执行函数
  53. }
  54. }
  55. //定义启动Dialog
  56. public void showInfo(){
  57. new AlertDialog.Builder(this)
  58. .setTitle("title")
  59. .setMessage("bla..bla")
  60. .setPositiveButton("OK", new DialogInterface.OnClickListener() {
  61. @Override
  62. public void onClick(DialogInterface dialog, int which) {
  63. }
  64. })
  65. .show();
  66. }
  67. }

more.xml

______________________________________________

  1. <?xml version=”1.0″ encoding=”utf-8″?>
  2. <LinearLayout xmlns:android=”http://schemas.android.com/apk/res/android”
  3. android:orientation=”horizontal”
  4. android:layout_width=”fill_parent”
  5. android:layout_height=”fill_parent”>
  6. <ImageView
  7. android:id=”@+id/img”
  8. android:layout_width=”wrap_content”
  9. android:layout_height=”wrap_content”
  10. android:layout_margin=”5px”/>
  11. <LinearLayout
  12. android:orientation=”vertical”
  13. android:layout_width=”wrap_content”
  14. android:layout_height=”wrap_content”>
  15. <TextView
  16. android:id=”@+id/title”
  17. android:layout_width=”wrap_content”
  18. android:layout_height=”wrap_content”
  19. android:textColor=”#000000″
  20. android:textSize=”22px” />
  21. <TextView
  22. android:id=”@+id/info”
  23. android:layout_width=”wrap_content”
  24. android:layout_height=”wrap_content”
  25. android:textColor=”#000000″
  26. android:textSize=”13px” />
  27. </LinearLayout>
  28. </LinearLayout>

参考文章:

http://www.cnblogs.com/allin/archive/2010/05/11/1732200.html

通过ListActivity使用ListView布局方法的更多相关文章

  1. Android使用ListView使用方法

    Android使用ListView应该注意的地方   在ListView中设置Selector为null会报空指针? mListView.setSelector(null);//空指针 试试下面这种: ...

  2. Android ListView getView()方法重复调用导致position错位

    问题现状:Android ListView getView()方法重复调用导致position错位 解决办法:把ListView布局文件的layout_height属性改为fill_parent或者m ...

  3. flex 布局 初次接触这个好使又不是特别好用的布局方法

    刚开始学前端的童鞋们应该也是一样先学习的table然后再学习了盒子模型,感觉终于学会了简单的网页布局,使用各种display,float,position绞尽脑汁给页面布局成自己想要的页面样式,然而, ...

  4. Android开发日常-listview滚动方法梳理

    listview滚动方法梳理 1.setSelection(position); 滚动到指定条目 2.setSelectionFromTop(position,y): 距离指定条目向下偏移y(像素) ...

  5. android listView布局等分列

    android listView布局4等分列. 必须要加上<RelativeLayout 在外层,不然等分不起作用 <RelativeLayout xmlns:android=" ...

  6. QMdiArea及QMdiSubWindow实现父子窗口及布局方法

    版权声明:若无来源注明,Techie亮博客文章均为原创. 转载请以链接形式标明本文标题和地址: 本文标题:QMdiArea及QMdiSubWindow实现父子窗口及布局方法     本文地址:http ...

  7. 结合CSS3的布局新特征谈谈常见布局方法

    写在前面最近看到<图解CSS3>的布局部分,结合自己以前阅读过的一些布局方面的知识,这里进行一次基于CSS2.3的各种布局的方法总结. 常见的页面布局 在拿到设计稿时,作为一个前端人员,我 ...

  8. 一个ListView布局的不断演化

    刚出来工作,就负责一个APP的某块功能的编写,该功能就是类似微博那样的界面.微博界面的编写实际上是非常复杂的,虽然它只是一个ListView,但要想让这个ListView滑得动,是的,在一些配置低的手 ...

  9. ListView 使用方法(Asp.Net)

    您将须要用到的独有数据绑定控件. Fritz Onion 代码下载位置: ExtremeASPNET2008_03.exe (192 KB) Browse the Code Online  文件夹 L ...

随机推荐

  1. 实战案例--Grunt构建Web程序

    GruntJS构建Web程序.使用Gruntjs来搭建一个前端项目,然后使用grunt合并,压缩JS文件,熟练了node.js安装和grunt.js安装后,接下来来实战一个案例,案例是根据snandy ...

  2. POJ 1691 Painting A Board(DFS)

    链接 题意 : 看了好长时间终于看懂题目了,将一个大矩形划分成若干小矩形,告诉你每个小矩形的左上角那个点和右下角那个点的坐标,告诉你这个小矩形要涂的颜色,每个颜色对应一个刷子,问你最少要使用几次刷子. ...

  3. POJ 2993Emag eht htiw Em Pleh

    http://poj.org/problem?id=2993 这个题与POJ2996正好反着,但个人认为,比2996好做多了,那些边边框框都挺容易输出的,剩下的注意p别忘了给输进去就行,还有白色的是大 ...

  4. docker: "build" requires 1 argument. See 'docker build --help'.

    http://bbs.csdn.net/topics/391040030 docker build  --tag="ouruser/sinatra:v3" -<Dockerf ...

  5. Android中JSON数据格式的简单使用

    源码: package com.wangzhu.demo; import java.io.BufferedReader; import java.io.IOException; import java ...

  6. cojs 疯狂的重心 疯狂的机器人 题解报告

    疯狂的重心 话说做过幻想乡战略游戏的人应该很容易切掉这道题目吧 我们考虑一棵树如果添加了一个叶子,那么其重心最多向叶子方向移动1的距离 而是否移动我们只需要记录子树中有多少个点就可以判断啦 也就是说这 ...

  7. lintcode 中等题:subsets II 带重复元素的子集

    题目 带重复元素的子集 给定一个可能具有重复数字的列表,返回其所有可能的子集 样例 如果 S = [1,2,2],一个可能的答案为: [ [2], [1], [1,2,2], [2,2], [1,2] ...

  8. SpingMVC中利用BindingResult将错误信息返回到页面中

    SpingMVC中利用BindingResult将错误信息返回到页面中. ActionFrom中: private String name; private String password; get( ...

  9. linux上tomcat安装

    转载:http://my.oschina.net/zmf/blog/309484 tomcat安装 下载后解压:tar -zxvf apache-tomcat-7.0.28 移动到/usr/local ...

  10. C语言命名规则

    一.程序风格:          1.严格采用阶梯层次组织程序代码:          各层次缩进的分格采用VC的缺省风格,即每层次缩进为4格,括号位于下一行.     要求相匹配的大括号在同一列,对 ...