- 浏览: 752776 次
- 性别:
- 来自: 成都
文章分类
- 全部博客 (139)
- 玩转Android (48)
- Android创意美工 (0)
- Android杂谈 (23)
- Android实例练习 (2)
- Android ROM研究 (5)
- Android NDK开发指南 (5)
- Android NDK (0)
- Android Tips (3)
- Windows Phone 7 (5)
- iPhone (0)
- HTML5学习室 (0)
- JAVA (9)
- SSH+ibatis (8)
- PHP (0)
- IT生活 (1)
- linux (2)
- C (4)
- C++ (1)
- web 前端 (1)
- 云计算 (0)
- 设计模式 (0)
- C# (2)
- 其他 (1)
- 数据结构 (5)
- Web开发 (10)
- 数据库 (3)
- 搜索引擎 (0)
- Go语言 (0)
最新评论
-
wi100sh:
多谢分享~
玩转Android---UI篇---ImageButton(带图标的按钮) -
zhanghaichang:
好文章的。
高性能web开发技术(一) -
yingang:
引用classes.dex.dex2jar.jar 拖入 j ...
Andorid杂谈---Apk文件的反编译 -
扶摇诺:
讲解的简明易懂,多谢啦!
玩转Android---UI篇---LinearLayout(线性布局) -
a13429921973:
更为详细的图文介绍,可参考这个http://blog.csdn ...
Android ROM研究---CyanogenMod源代码下载及编译
ListView是列表组件,这个ListView是我接触的目前所有Android UI控件中最为麻烦的控件,之所以麻烦就是因为它的各种的适配器Adapter特别麻烦,Adapter的组织结构图如下
在ListView中,以内不同的Adapter不同,所以也会有不同的效果,其中比较常用的是SampleAdapter,SimpleCursorAdapter,ArrayAdapter,BaseAdapter等,
万事开头难,还是从最简单的SimpleAdapter说起,以后再一点点学习
simpleAdapter的扩展性最好,可以定义各种各样的布局出来,可以放上ImageView(图片),还可以放上Button(按钮),CheckBox(复选框)等等。下面的代码都直接继承了ListActivity,ListActivity和普通的Activity没有太大的差别,不同就是对显示ListView做了许多优化,方面显示而已。
先看看一个实例,是由SimpleAdapter与ListView绑定后的一个小例子。
ListViewone.java文件
package org.hualang.simpleadapter; import java.util.ArrayList; import java.util.HashMap; import android.app.ListActivity; import android.os.Bundle; import android.view.View; import android.widget.ListView; import android.widget.SimpleAdapter; import android.widget.Toast; public class ListViewone extends ListActivity { /** Called when the activity is first created. */ private Toast toast; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); ArrayList<HashMap<String,String>> list=new ArrayList<HashMap<String,String>>(); HashMap<String,String> map1=new HashMap<String,String>(); HashMap<String,String> map2=new HashMap<String,String>(); HashMap<String,String> map3=new HashMap<String,String>(); map1.put("name", "凝墨"); map1.put("phone", "13699452790"); map2.put("name", "小棕"); map2.put("phone", "15827980910"); map3.put("name", "花郎"); map3.put("phone", "18678091166"); list.add(map1); list.add(map2); list.add(map3); SimpleAdapter listAdapter=new SimpleAdapter(this, list, R.layout.info, new String[] {"name","phone"}, new int[] {R.id.name,R.id.phone}); setListAdapter(listAdapter); } protected void onListItemClick(ListView l,View v,int position,long id) { super.onListItemClick(l,v,position,id); if(l.getItemIdAtPosition(position)==0) { toast.makeText(getApplicationContext(), "我是凝墨", Toast.LENGTH_SHORT).show(); }else if(l.getItemIdAtPosition(position)==1) { toast.makeText(getApplicationContext(), "我是小棕", Toast.LENGTH_SHORT).show(); }else if(l.getItemIdAtPosition(position)==2) { toast.makeText(getApplicationContext(), "我是花郎", Toast.LENGTH_SHORT).show(); } } }
main.xml文件
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" > <LinearLayout android:layout_width="fill_parent" android:layout_height="wrap_content" android:id="@+id/linearlayout" android:orientation="vertical" > <ListView android:id="@id/android:list" android:layout_width="fill_parent" android:layout_height="wrap_content" android:drawSelectorOnTop="false" android:scrollbars="vertical" /> </LinearLayout> </LinearLayout>
info.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="wrap_content" android:layout_height="wrap_content" android:paddingLeft="10dip" android:paddingRight="10dip" android:paddingTop="1dip" android:paddingBottom="1dip" > <TextView android:id="@+id/name" android:layout_width="180dip" android:layout_height="30dip" android:textSize="10pt" android:singleLine="true" /> <TextView android:id="@+id/phone" android:layout_width="fill_parent" android:layout_height="fill_parent" android:gravity="right" android:textSize="10pt" /> </LinearLayout>
使用simpleAdapter的数据用一般都是HashMap构成的List,list的每一节对应ListView的每一行。HashMap的每个键值数据映射到布局文件中对应id的组件上。因为系统没有对应的布局文件可用,我们可以自己定义一个布局info.xml。下面做适配,new一个SimpleAdapter参数一次是:this,布局文件(info.xml)。布局文件的组件name,phone。布局文件的各组件分别映射到HashMap的各元素上,完成适配。
运行结果如下:
当点击了第一行
实例2:显示一个带图片的ListView,使用适配器SampleAdapter
ListViewone.java
package org.hualang.simpleadapter; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import android.app.ListActivity; import android.os.Bundle; import android.widget.SimpleAdapter; public class ListViewone extends ListActivity { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); SimpleAdapter adapter = new SimpleAdapter(this,getData(),R.layout.info, new String[]{"name","phone","img"}, new int[]{R.id.name,R.id.phone,R.id.img}); setListAdapter(adapter); } private List<Map<String, Object>> getData() { List<Map<String, Object>> list = new ArrayList<Map<String, Object>>(); Map<String, Object> map = new HashMap<String, Object>(); map.put("name", "凝墨"); map.put("phone", "13699782346"); map.put("img", R.drawable.pic1); list.add(map); map = new HashMap<String, Object>(); map.put("name", "小棕"); map.put("phone", "15899034671"); map.put("img", R.drawable.pic2); list.add(map); map = new HashMap<String, Object>(); map.put("name", "花郎"); map.put("phone", "18677656526"); map.put("img", R.drawable.pic3); list.add(map); return list; } }
info.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/name" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textColor="#FFFFFFFF" android:textSize="22px" /> <TextView android:id="@+id/phone" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textColor="#FFFFFFFF" android:textSize="13px" /> </LinearLayout> </LinearLayout>
这里,就不做事件处理了,运行结果如下:
发表评论
-
玩转Android---2D图形及动画---动画分析(Tween详细分析)
2011-09-26 21:59 2579在Android系统中提供了两种动画实现方式:一种是Tween ... -
玩转Android---2D图形及动画---图片处理
2011-09-26 13:08 1720在Android中很多地方都使 ... -
玩转Android--UI篇--PreferenceActivity(开启wifi和音乐等)
2011-09-01 11:57 4776本测试主要是为了测试PreferenceActivity的使用 ... -
玩转Android---2D图形及动画---Gif动画
2011-08-05 09:36 2162由于Gif本身就是动画,所以如果能够直接使用的话,会省去很多的 ... -
玩转Android---2D图形及动画---Frame动画
2011-08-01 20:53 2025Frame动画其实就是逐帧动画,用法也比Tween动画简单,只 ... -
玩转Android---2D图形及动画---Tween动画
2011-07-31 22:53 2137Android平台提供了两类动画,分别是Tween动画,和Fr ... -
玩转Android---2D图形及动画---View类使用
2011-07-30 23:39 2752由于游戏界面是由大量美工资源图片构成的,所以,在设计游戏界面的 ... -
玩转Android--组件篇---Handler的使用
2011-07-30 14:01 5307public class Handler ... -
玩转Android---UI篇---ZoomControls放大缩小图片
2011-07-27 13:03 7207ZoomControls控件是一个可以缩放但控件,效果如下图 ... -
玩转Android---组件篇---TextSwitcher
2011-07-25 09:21 2149还记得有一次淘宝的电话面试的时候问了我一个关于Android的 ... -
玩转Android---组件篇---AnalogClock,DigitalClock
2011-07-10 19:28 2877首先要说的是,这两个控件并不是经常的使用,但是如果程序需要的话 ... -
玩转Android---组件篇---SeekBar,RatingBar,Chronometer
2011-07-10 19:12 2802今天补充三个组件的使用,避免日后忘记。它们分别是 SeekB ... -
玩转Android---组件篇---Handler的使用(2)
2011-05-28 17:15 2758对于Handler来说,它和与它调用它的Activity是出于 ... -
玩转Android---组件篇---Handler的使用(1)
2011-05-28 15:37 2110在android中,有很多功能是不能放在onCreate或者o ... -
玩转Android---事件监听篇---第2篇
2011-05-27 10:30 5194事件监听篇---第二篇 下面是各种常用控件的事件监听的 ... -
玩转Android---事件监听篇---第1篇
2011-05-26 21:29 9893事件就是用户与UI界面的交互时所触发的操作。比如点击某一个按钮 ... -
玩转Android---组件篇---数据存储之SQLite
2011-04-17 17:05 8434Android中通过SQLite数据库引擎来实现结构化数据存储 ... -
玩转Android---组件篇---数据存储之File
2011-04-17 11:07 1996我们可以将一些数据直接以文件的形式保存在设备中。例如,一些文本 ... -
玩转Andorid---组件篇---数据存储之preference
2011-04-14 21:58 2256程序是数据的输入、处 ... -
玩转Android---组件篇---Broadcast Receiver(广播接收器)
2011-04-14 19:24 29252Braodcast Receiver顾名思义 ...
相关推荐
•Android---UI篇---ListView之SampleAdapter(列表)---1 • •Android---UI篇---ListView之ArrayAdapter(列表)---2 • •Android---UI篇---ListView之SimpleCursorAdapter(列表)---3 • •Android---UI篇---...
android-world-of-listview-android
Android-ListView-Example
MVC-ListView-DataModel-master.zipMVC-ListView-DataModel-master.zipMVC-ListView-DataModel-master.zipMVC-ListView-DataModel-master.zipMVC-ListView-DataModel-master.zipMVC-ListView-DataModel-master....
android--ListView实现带标题栏效果(附源码)
Android-react-native-timeline-listview.zip,React本机应用程序的时间线组件,安卓系统是谷歌在2008年设计和制造的。操作系统主要写在爪哇,C和C 的核心组件。它是在linux内核之上构建的,具有安全性优势。
android--ListView实现聊天记录
android-ListView内嵌GridView
Android--开发--ListView上下翻页效果
Android--开发-- ListView下拉刷新 Demo
Android--开发--ListView保持在列表的顶部固定View 案例分析 源码
android-pulltorefresh-listview 下拉刷新ListView
Android--开发--ListView 中的item随意拖动
2-android-pulltorefresh-listview 下拉刷新ListView.zip
Android--开发--ListView 实现点击侧边A-Z快速查找
Android -- ListView 返回 第一条数据的实现.rar,太多无法一一验证是否可用,程序如果跑不起来需要自调,部分代码功能进行参考学习。
21.[开源][安卓][拖拽]drag-sort-listview-master DragSortListView(DSLV)是Android ListView的一个扩展,支持拖拽排序和左右滑动删除功能。重写了TouchInterceptor(TI)类来提供更加优美的拖拽动画效果。 DSLV...
Android-ListView监听上下滑动.rar,太多无法一一验证是否可用,程序如果跑不起来需要自调,部分代码功能进行参考学习。
滑动listview标题置顶,listview吸顶效果,安卓系统状态栏透明,有图有源码
android-pro-listview.7z