通过ListActivity使用ListView布局方法
先简单的介绍一下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
______________________________________
- import java.util.ArrayList;
- import java.util.HashMap;
- import java.util.List;
- import java.util.Map;
- import android.app.AlertDialog;
- import android.app.ListActivity;
- import android.content.DialogInterface;
- import android.os.Bundle;
- import android.view.View;
- import android.widget.ListView;
- import android.widget.SimpleAdapter;
- public class MoreActivity extends ListActivity {
- private String discount_info;
- private String recommend_shop;
- private String about_us;
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- SimpleAdapter adapter = new SimpleAdapter(this,getData(),R.layout.more,
- new String[]{"title","info","img"},
- new int[]{R.id.title,R.id.info,R.id.img});
- setListAdapter(adapter);
- }
- private List<HashMap> getData() {
- List<HashMap> list = new ArrayList<HashMap>();
- Map map = new HashMap();
- discount_info=this.getString(R.string.discount_info);//相应字符串在String.xml中设置
- recommend_shop=this.getString(R.string.recommend_shop);
- about_us=this.getString(R.string.about_us);
- map.put("title", discount_info);
- map.put("info", "android 1");
- map.put("img", R.drawable.i1);
- list.add(map);
- map = new HashMap();
- map.put("title", recommend_shop);
- map.put("info", "android 2");
- map.put("img", R.drawable.i2);
- list.add(map);
- map = new HashMap();
- map.put("title", about_us);
- map.put("info", "android 3");
- map.put("img", R.drawable.i3);
- list.add(map);
- return list;
- }
- @Override
- protected void onListItemClick(ListView l, View v, int position, long id) {
- // TODO Auto-generated method stub
- super.onListItemClick(l, v, position, id);
- if(0==id)
- {
- showInfo();//点击第一项执行函数,其它id可以自己设置执行函数
- }
- }
- //定义启动Dialog
- public void showInfo(){
- new AlertDialog.Builder(this)
- .setTitle("title")
- .setMessage("bla..bla")
- .setPositiveButton("OK", new DialogInterface.OnClickListener() {
- @Override
- public void onClick(DialogInterface dialog, int which) {
- }
- })
- .show();
- }
- }
more.xml
______________________________________________
- <?xml version=”1.0″ encoding=”utf-8″?>
- <LinearLayout xmlns:android=”http://schemas.android.com/apk/res/android”
- android:orientation=”horizontal”
- android:layout_width=”fill_parent”
- android:layout_height=”fill_parent”>
- <ImageView
- android:id=”@+id/img”
- android:layout_width=”wrap_content”
- android:layout_height=”wrap_content”
- android:layout_margin=”5px”/>
- <LinearLayout
- android:orientation=”vertical”
- android:layout_width=”wrap_content”
- android:layout_height=”wrap_content”>
- <TextView
- android:id=”@+id/title”
- android:layout_width=”wrap_content”
- android:layout_height=”wrap_content”
- android:textColor=”#000000″
- android:textSize=”22px” />
- <TextView
- android:id=”@+id/info”
- android:layout_width=”wrap_content”
- android:layout_height=”wrap_content”
- android:textColor=”#000000″
- android:textSize=”13px” />
- </LinearLayout>
- </LinearLayout>
参考文章:
http://www.cnblogs.com/allin/archive/2010/05/11/1732200.html
通过ListActivity使用ListView布局方法的更多相关文章
- Android使用ListView使用方法
Android使用ListView应该注意的地方 在ListView中设置Selector为null会报空指针? mListView.setSelector(null);//空指针 试试下面这种: ...
- Android ListView getView()方法重复调用导致position错位
问题现状:Android ListView getView()方法重复调用导致position错位 解决办法:把ListView布局文件的layout_height属性改为fill_parent或者m ...
- flex 布局 初次接触这个好使又不是特别好用的布局方法
刚开始学前端的童鞋们应该也是一样先学习的table然后再学习了盒子模型,感觉终于学会了简单的网页布局,使用各种display,float,position绞尽脑汁给页面布局成自己想要的页面样式,然而, ...
- Android开发日常-listview滚动方法梳理
listview滚动方法梳理 1.setSelection(position); 滚动到指定条目 2.setSelectionFromTop(position,y): 距离指定条目向下偏移y(像素) ...
- android listView布局等分列
android listView布局4等分列. 必须要加上<RelativeLayout 在外层,不然等分不起作用 <RelativeLayout xmlns:android=" ...
- QMdiArea及QMdiSubWindow实现父子窗口及布局方法
版权声明:若无来源注明,Techie亮博客文章均为原创. 转载请以链接形式标明本文标题和地址: 本文标题:QMdiArea及QMdiSubWindow实现父子窗口及布局方法 本文地址:http ...
- 结合CSS3的布局新特征谈谈常见布局方法
写在前面最近看到<图解CSS3>的布局部分,结合自己以前阅读过的一些布局方面的知识,这里进行一次基于CSS2.3的各种布局的方法总结. 常见的页面布局 在拿到设计稿时,作为一个前端人员,我 ...
- 一个ListView布局的不断演化
刚出来工作,就负责一个APP的某块功能的编写,该功能就是类似微博那样的界面.微博界面的编写实际上是非常复杂的,虽然它只是一个ListView,但要想让这个ListView滑得动,是的,在一些配置低的手 ...
- ListView 使用方法(Asp.Net)
您将须要用到的独有数据绑定控件. Fritz Onion 代码下载位置: ExtremeASPNET2008_03.exe (192 KB) Browse the Code Online 文件夹 L ...
随机推荐
- 实战案例--Grunt构建Web程序
GruntJS构建Web程序.使用Gruntjs来搭建一个前端项目,然后使用grunt合并,压缩JS文件,熟练了node.js安装和grunt.js安装后,接下来来实战一个案例,案例是根据snandy ...
- POJ 1691 Painting A Board(DFS)
链接 题意 : 看了好长时间终于看懂题目了,将一个大矩形划分成若干小矩形,告诉你每个小矩形的左上角那个点和右下角那个点的坐标,告诉你这个小矩形要涂的颜色,每个颜色对应一个刷子,问你最少要使用几次刷子. ...
- POJ 2993Emag eht htiw Em Pleh
http://poj.org/problem?id=2993 这个题与POJ2996正好反着,但个人认为,比2996好做多了,那些边边框框都挺容易输出的,剩下的注意p别忘了给输进去就行,还有白色的是大 ...
- docker: "build" requires 1 argument. See 'docker build --help'.
http://bbs.csdn.net/topics/391040030 docker build --tag="ouruser/sinatra:v3" -<Dockerf ...
- Android中JSON数据格式的简单使用
源码: package com.wangzhu.demo; import java.io.BufferedReader; import java.io.IOException; import java ...
- cojs 疯狂的重心 疯狂的机器人 题解报告
疯狂的重心 话说做过幻想乡战略游戏的人应该很容易切掉这道题目吧 我们考虑一棵树如果添加了一个叶子,那么其重心最多向叶子方向移动1的距离 而是否移动我们只需要记录子树中有多少个点就可以判断啦 也就是说这 ...
- lintcode 中等题:subsets II 带重复元素的子集
题目 带重复元素的子集 给定一个可能具有重复数字的列表,返回其所有可能的子集 样例 如果 S = [1,2,2],一个可能的答案为: [ [2], [1], [1,2,2], [2,2], [1,2] ...
- SpingMVC中利用BindingResult将错误信息返回到页面中
SpingMVC中利用BindingResult将错误信息返回到页面中. ActionFrom中: private String name; private String password; get( ...
- linux上tomcat安装
转载:http://my.oschina.net/zmf/blog/309484 tomcat安装 下载后解压:tar -zxvf apache-tomcat-7.0.28 移动到/usr/local ...
- C语言命名规则
一.程序风格: 1.严格采用阶梯层次组织程序代码: 各层次缩进的分格采用VC的缺省风格,即每层次缩进为4格,括号位于下一行. 要求相匹配的大括号在同一列,对 ...