通过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 ...
随机推荐
- POJ 3468 A Simple Problem with Integers(线段树区间更新,模板题,求区间和)
#include <iostream> #include <stdio.h> #include <string.h> #define lson rt<< ...
- 关于JS中变量的作用域-实例
先看问题,如下,自己运行一下吧! if (!('_qyzA' in window)) { var _qyzA = 1; } alert(_qyzA);//undefined 分析:首先,所有的全局变量 ...
- 套题T4
Problem 1 无聊的gcd(gcd.c/cpp/pas) 话说出题人不会被查水表吧. 简单的问题描述:从N个正整数里面取出K个数的最大公因数最大是多少.(请将答案乘上k之后输出哦,谢谢合作.) ...
- Project Euler 98:Anagramic squares 重排平方数
Anagramic squares By replacing each of the letters in the word CARE with 1, 2, 9, and 6 respectively ...
- React组件测试(模拟组件、函数和事件)
一.模拟组件 1.用到的工具 (1)browerify (2)jasmine-react-helpers (3)rewireify(依赖注入) (4)命令:browserify - t reactif ...
- 与Google轻轻地擦肩而过
第一集 因为那几年三天两头往硅谷里飞,所以我实在记不清这个故事到底是发生在98年还是99年夏天某日的一个下午. 那天我和Excite.com的创始人Mark V. H.在Palo Alto的一家餐厅共 ...
- ReadDirectoryChangesW 监控文件夹 (一个简单的监控示例程序)(文件被修改了,也可以探测到)
// .h文件 #pragma once typedef void (*PFN_NotifyAction)(DWORD dwAction, LPWSTR szFile, DWORD dwLength) ...
- 59. Spiral Matrix II
题目: Given an integer n, generate a square matrix filled with elements from 1 to n2 in spiral order. ...
- 显示Servlet API主要版本,次要版本以及服务器系统信息
package com.mhb; import java.io.IOException; import java.io.PrintWriter; import javax.servlet.Servle ...
- Android zxing连续扫描
initCamera(); if (mHandler != null) mHandler.restartPreviewAndDecode(); 在扫描完毕后执行这3句即可. 说明: 1.扫描处理方法为 ...