通过ListActivity使用ListView布局方法
来源:互联网 发布:网页游戏平台源码下载 编辑:程序博客网 时间:2024/06/08 13:48
由于本人第一次使用该博客,发表文章使用的不是很熟练,所以文章格式及写作水平可能不是很好,希望各位见谅
先简单的介绍一下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; @Overridepublic 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;} @Overrideprotected void onListItemClick(ListView l, View v, int position, long id) {// TODO Auto-generated method stubsuper.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() {@Overridepublic 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布局之一(继承listActivity、使用arrayAdapter)
- android ListView布局之一(继承listActivity、使用arrayAdapter)
- 通过ListActivity创建一个ListView
- ListView使用详解,listActivity使用方法
- ListActivity 中 ListView 的使用
- Android ListView实现方法一(ListActivity)
- ListFragment,Fragment+ListView,ListActivity,Activity+ListView方法实现listview效果
- Android中ListView的使用(不用ListActivity)
- Android关于ListView的总结--使用ListActivity
- Android关于ListView的总结--使用ListActivity
- ListView,ListActivity的认识及简单使用
- android ListActivity或listview相互数据传递的实现方法
- 使用ListActivity
- Activity中与ListActivity中使用listview区别
- Activity中与ListActivity中使用listview区别
- Android中Activity中与ListActivity中使用listview区别
- ListActivity 的使用和在 ListView中添加图片文字
- 交换机Vlan介绍 以太网端口链路类型介绍 交换机trunk属性使用介绍 数据配置步骤
- Java范例开发大全源程序
- 关于instanceof
- 外频、系统总线频率、主频、倍频、内存频率、前端总线FSB 【精】
- 在office中设置上下标的方式!
- 通过ListActivity使用ListView布局方法
- Session:
- 我眼里的Exchange 2010 之:1—DAG
- VC6.0 动态库或控件代码移植到VC2010一些异常问题
- vxworks测试题及解答
- GPS开发常用方法和Criteria确定android location providerGPS
- 国外嵌入式面试题
- 黑莓开发之环境的搭建
- 《人月神话》笔记:外科手术队伍