通过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 ...
随机推荐
- Android ActionBar下拉选项
package com.example.actionBarTest.actionBarList; import android.app.ActionBar; import android.app.Ac ...
- iOS视频录制、压缩导出、取帧
概述 花了点时间研究了一下常用的视频获取.录制.压缩.取帧图功能,分享给大家了!相信阅读完本篇文章,会对你有很大的帮助的! 本篇文章研究几下以个功能: 视频录制 从相册选择视频 保持视频到相册 获取视 ...
- jQuery编程基础精华02(属性、表单过滤器,元素的each,表单选择器,子元素过滤器(*),追加方法,节点,样式操作)
属性.表单过滤器 属性过滤选择器: $("div[id]")选取有id属性的<div> $("div[title=test]")选取title属性为 ...
- 深度卷积神经网络用于图像缩放Image Scaling using Deep Convolutional Neural Networks
This past summer I interned at Flipboard in Palo Alto, California. I worked on machine learning base ...
- wordCount程序中MapReduce工作过程分析
Map处理的是一个纯文本.Mapper处理的数据是由InputFormat分解过的数据集,其中InputFormat的作用是将数据集切割成小数据集InputSplit,每一个InputSplit将由一 ...
- Drawable(5)关于从资源文件构造的Drawable不显示
要给它设置个bounds才可以 TextView noticeHeaderView; TextView headerRefreshText; ProgressBar headerRefreshPgrs ...
- 【设计模式】—— 单例模式Singleton
前言:[模式总览]——————————by xingoo 模式意图 保证类仅有一个实例,并且可以供应用程序全局使用.为了保证这一点,就需要这个类自己创建自己的对象,并且对外有公开的调用方法. 模式结构 ...
- [Codeforces677C]Vanya and Label(组合数学,快速幂)
题目链接:http://codeforces.com/contest/677/problem/C 题意:给一个字符和数字的映射关系,然后再给一个字符串.问有多少个其他的字符串,使得那些字符串之间相互操 ...
- 关于 Memcached 的一些使用
关于Memcached的一些用法, Memcached 在Windows下的版本费了很大劲,才找到.win32,win64都有.本来想自己build的,但是Cygwin下载包也是费老劲了,下不下来. ...
- STL源码中map和set中key值不能修改的实现
前言 最近正好刚刚看完,<stl源码剖析>这本书的map和set的源码部分.但是看完之后又突然发现,之前怎么没有注意到map和set容器中key不能修改是怎么实现的.故,特此整理如下. s ...