- 浏览: 110087 次
- 性别:
- 来自: 成都
文章分类
最新评论
-
eyeandroid_com:
貌似掉线 写道其实ImageView, ImageButton ...
【Android每日一讲】2012.11.12 设计具有背景图的按钮 - ImageButton的焦点及事件处理 -
貌似掉线:
其实ImageView, ImageButton, Butto ...
【Android每日一讲】2012.11.12 设计具有背景图的按钮 - ImageButton的焦点及事件处理 -
lhw0531:
有推广嫌疑
【史上最全的】Android应用源码免费下载 -
tenderuser:
L_sharp 写道no pic u c a xx没有图片,你 ...
自定义ViewGroup实现ViewPager的滑动效果(附源码下载) -
L_sharp:
no pic u c a xx
自定义ViewGroup实现ViewPager的滑动效果(附源码下载)
今天学习点轻松的内容吧,看看android.app包里的几个类。首先是这个在平台自的例子中被广泛使用的ListActivity。这个类其实就是一个含有一个ListView组件的Activity类。也就是说,如果我们直接在一个普通的Activity中自己加一个ListView也是完全可以取代这个ListActivity的,只是它更方便而已,方便到什么程度呢?来做个例子瞧瞧。
public class HelloTwoB extends ListActivity...{
public void onCreate(Bundle icicle) ...{
< super.onCreate(icicle);
< setTheme(android.R.style.Theme_Dark);
< setContentView(R.layout.mainb);
< List<String> items = fillArray();
< ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,R.layout.list_row,items);
<
< this.setListAdapter(adapter);
< }
private List<String> fillArray() ...{
< List<String> items = new ArrayList<String>();
< items.add("日曜日");
< items.add("月曜日");
< items.add("火曜日");
< items.add("水曜日");
< items.add("木曜日");
< items.add("金曜日");
< items.add("土曜日");
< return items;
< }
< @Override
< protected void onListItemClick(ListView l, View v, int position, long id)
...{
< TextView txt = (TextView)this.findViewById(R.id.text);
< txt.setText("あすは "+l.getSelectedItem().toString()+"です。");
< }
<}
的确可以简单到只需准备一个List对象并借助Adapter就可以构造出一个列表。重载onListItemClick方法可以响应选择事件,利用第一个参数可以访问到这个ListView实例以得到选中的条目信息。这里有一点要说明的,就是如果更简单的话,其实连那个setContentView都可以不要了,Android也会自动帮我们构造出一个全屏的列表。但是本例中我们需要一个TextView来显示选中的条目,所以我们需要一个layout.mainb描述一下这个列表窗口。
<?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"
< >
<<TextView id="@+id/text"
< android:layout_width="fill_parent"
< android:layout_height="wrap_content"
< android:text=""
< />
<<ListView id="@id/android:list"
< android:layout_width="fill_parent"
< android:layout_height="0dip"
< android:layout_weight="1"
< android:drawSelectorOnTop="false"
< />
<</LinearLayout>
这里需要注意的是那个ListView的ID,是系统自定义的android:list,不是我们随便取的,否则系统会说找不到它想要的listview了。然后,在这个listview之外,我们又增加了一个TextView,用来显示选中的条目。
再来说说这里用到的ArrayAdapter,它的构造函数中第二个参数是一个资源ID,ArrayAdapter的API文档中说是要求用一个包含TextView的layout文件,平台用它来显示每个选择条目的样式,这里的取值是R.layout.list_row,所以,我们还有一个list_row.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"
< >
<<TextView id="@+id/item"
< xmlns:android="http://schemas.android.com/apk/res/android"
< android:layout_width="wrap_content"
< android:layout_height="wrap_content"/>
<<TextView id="@+id/item2"
< xmlns:android="http://schemas.android.com/apk/res/android"
< android:layout_width="wrap_content"
< android:layout_height="wrap_content"/>
</LinearLayout>
从ArrayAdapter上溯到BaseAdapter,发现还有几个同源的Adapter也应该可以使用,象SimpleAdapter和CursorAdapter,还是做个例子来实验一下吧。
先看看SimpleAdapter,说是simple却不simple。
首先看看这个fillMaps方法,基本上就明白这个simpleAdapter是怎么回事了,在有些场合它还是挺有用的,可以为每个条目绑定一个值:
<private List<HashMap<String, String>> fillMaps()
...{
< List<HashMap<String, String>> items = new ArrayList<HashMap<String,String>>();
<
< HashMap<String,String> i = new HashMap<String,String>();
< i.put("name","日曜日");
< i.put("key", "SUN");
< items.add(i);
< HashMap<String,String> i1 = new HashMap<String,String>();
< i1.put("name","月曜日");
< i1.put("key", "MON");
< items.add(i1);
< HashMap<String,String> i2 = new HashMap<String,String>();
< i2.put("name","火曜日");
< i2.put("key", "TUE");
< items.add(i2);
< HashMap<String,String> i3 = new HashMap<String,String>();
< i3.put("name","水曜日");
< i3.put("key", "WED");
< items.add(i3);
< HashMap<String,String> i4= new HashMap<String,String>();
< i4.put("name","木曜日");
< i4.put("key", "THU");
< items.add(i4);
< HashMap<String,String> i5 = new HashMap<String,String>();
< i5.put("name","金曜日");
< i5.put("key", "FRI");
< items.add(i5);
< HashMap<String,String> i6 = new HashMap<String,String>();
< i6.put("name","土曜日");
< i.put("key", "SAT");
< items.add(i6);
<
< return items;
< }
然后,在HelloTwoB中的onCreate函数中,修改代码,有几个不同:items的元素是HashMap实例,这是一点变化,然后构造函数除了要求items以外,还要求提供一个string[]来说明用hash表中的哪个字段显示在列表中,而后是一个资源ID的数组。我的代码是这样的:
<//SimpleAdapter demo
<List<HashMap<String, String>> items = fillMaps();
SimpleAdapter adapter=new SimpleAdapter(this,items,R.layout.list_row,new String[]{"name"},new int[]{R.id.item});
编译跑一下可以看到结果了,是吧?只是显示的文字不太对,再改一下:
<protected void onListItemClick(ListView l, View v, int position, long id)
{
< TextView txt = (TextView)this.findViewById(R.id.text);
< txt.setText("あすは "+((HashMap)l.obtainItem(position)).get("key").toString()+"です。");
< }
这样就好多了,其实一般情况下我们都是用ListView中的obtainItem取得当前选中的条目,然后转成List中的对应类型来使用的。
上面的例子中只显示name对应的值,其实你也可以试一下这样:
SimpleAdapter adapter=new SimpleAdapter(this,items,R.layout.list_row,new String[]{"name","key"},new int[]{R.id.item,R.id.item2});
看看是什么效果。
再看看那个CursorAdapter吧,它的列表中元素要求是Cursor,这东西与DB有关,不过最简单的DB就是通讯簿。先从Contacts.People入手吧,同样修改代码:
<//CursorAdapter demo
Cursor mCursor = this.getContentResolver().query(Contacts.People.CONTENT_URI, null, null, null, null);
SimpleCursorAdapter adapter=new SimpleCursorAdapter(this,R.layout.list_row,mCursor,new String[]{Contacts.People.NAME},new int[]{R.id.item});
因为单纯的CursorAdapter是抽象类,所以我用的是它的子类SimpleCursorAdapter,很好理解,先用ContentResolver查询通讯簿得到一个游标,然后告诉SimpleCursorAdapter要用其中的People.NAME作为显示项来构造出一个adapter即可。
现在的onListItemClick也不一样了,如下:
< protected void onListItemClick(ListView l, View v, int position, long id)
{
< TextView txt = (TextView)this.findViewById(R.id.text);
< Cursor c = (Cursor)l.obtainItem(position);
< txt.setText("SEL = "+c.getString(c.getColumnIndex(Contacts.People.NUMBER)));
< }
这里同样是先用obtainItem取到游标,然后用从记录中取出想要的字段显示即可。在做这个例子时,因为权限的问题我们还得修改一下AndroidManifest.xml文件,让我们的应用可以访问到通讯簿:
<<manifest xmlns:android="http://schemas.android.com/apk/res/android"
< package="cn.sharetop.android.hello.two">
< <uses-permission id="android.permission.READ_CONTACTS" />
< <application android:icon="@drawable/icon">
< ... ...
public class HelloTwoB extends ListActivity...{
public void onCreate(Bundle icicle) ...{
< super.onCreate(icicle);
< setTheme(android.R.style.Theme_Dark);
< setContentView(R.layout.mainb);
< List<String> items = fillArray();
< ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,R.layout.list_row,items);
<
< this.setListAdapter(adapter);
< }
private List<String> fillArray() ...{
< List<String> items = new ArrayList<String>();
< items.add("日曜日");
< items.add("月曜日");
< items.add("火曜日");
< items.add("水曜日");
< items.add("木曜日");
< items.add("金曜日");
< items.add("土曜日");
< return items;
< }
< @Override
< protected void onListItemClick(ListView l, View v, int position, long id)
...{
< TextView txt = (TextView)this.findViewById(R.id.text);
< txt.setText("あすは "+l.getSelectedItem().toString()+"です。");
< }
<}
的确可以简单到只需准备一个List对象并借助Adapter就可以构造出一个列表。重载onListItemClick方法可以响应选择事件,利用第一个参数可以访问到这个ListView实例以得到选中的条目信息。这里有一点要说明的,就是如果更简单的话,其实连那个setContentView都可以不要了,Android也会自动帮我们构造出一个全屏的列表。但是本例中我们需要一个TextView来显示选中的条目,所以我们需要一个layout.mainb描述一下这个列表窗口。
<?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"
< >
<<TextView id="@+id/text"
< android:layout_width="fill_parent"
< android:layout_height="wrap_content"
< android:text=""
< />
<<ListView id="@id/android:list"
< android:layout_width="fill_parent"
< android:layout_height="0dip"
< android:layout_weight="1"
< android:drawSelectorOnTop="false"
< />
<</LinearLayout>
这里需要注意的是那个ListView的ID,是系统自定义的android:list,不是我们随便取的,否则系统会说找不到它想要的listview了。然后,在这个listview之外,我们又增加了一个TextView,用来显示选中的条目。
再来说说这里用到的ArrayAdapter,它的构造函数中第二个参数是一个资源ID,ArrayAdapter的API文档中说是要求用一个包含TextView的layout文件,平台用它来显示每个选择条目的样式,这里的取值是R.layout.list_row,所以,我们还有一个list_row.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"
< >
<<TextView id="@+id/item"
< xmlns:android="http://schemas.android.com/apk/res/android"
< android:layout_width="wrap_content"
< android:layout_height="wrap_content"/>
<<TextView id="@+id/item2"
< xmlns:android="http://schemas.android.com/apk/res/android"
< android:layout_width="wrap_content"
< android:layout_height="wrap_content"/>
</LinearLayout>
从ArrayAdapter上溯到BaseAdapter,发现还有几个同源的Adapter也应该可以使用,象SimpleAdapter和CursorAdapter,还是做个例子来实验一下吧。
先看看SimpleAdapter,说是simple却不simple。
首先看看这个fillMaps方法,基本上就明白这个simpleAdapter是怎么回事了,在有些场合它还是挺有用的,可以为每个条目绑定一个值:
<private List<HashMap<String, String>> fillMaps()
...{
< List<HashMap<String, String>> items = new ArrayList<HashMap<String,String>>();
<
< HashMap<String,String> i = new HashMap<String,String>();
< i.put("name","日曜日");
< i.put("key", "SUN");
< items.add(i);
< HashMap<String,String> i1 = new HashMap<String,String>();
< i1.put("name","月曜日");
< i1.put("key", "MON");
< items.add(i1);
< HashMap<String,String> i2 = new HashMap<String,String>();
< i2.put("name","火曜日");
< i2.put("key", "TUE");
< items.add(i2);
< HashMap<String,String> i3 = new HashMap<String,String>();
< i3.put("name","水曜日");
< i3.put("key", "WED");
< items.add(i3);
< HashMap<String,String> i4= new HashMap<String,String>();
< i4.put("name","木曜日");
< i4.put("key", "THU");
< items.add(i4);
< HashMap<String,String> i5 = new HashMap<String,String>();
< i5.put("name","金曜日");
< i5.put("key", "FRI");
< items.add(i5);
< HashMap<String,String> i6 = new HashMap<String,String>();
< i6.put("name","土曜日");
< i.put("key", "SAT");
< items.add(i6);
<
< return items;
< }
然后,在HelloTwoB中的onCreate函数中,修改代码,有几个不同:items的元素是HashMap实例,这是一点变化,然后构造函数除了要求items以外,还要求提供一个string[]来说明用hash表中的哪个字段显示在列表中,而后是一个资源ID的数组。我的代码是这样的:
<//SimpleAdapter demo
<List<HashMap<String, String>> items = fillMaps();
SimpleAdapter adapter=new SimpleAdapter(this,items,R.layout.list_row,new String[]{"name"},new int[]{R.id.item});
编译跑一下可以看到结果了,是吧?只是显示的文字不太对,再改一下:
<protected void onListItemClick(ListView l, View v, int position, long id)
{
< TextView txt = (TextView)this.findViewById(R.id.text);
< txt.setText("あすは "+((HashMap)l.obtainItem(position)).get("key").toString()+"です。");
< }
这样就好多了,其实一般情况下我们都是用ListView中的obtainItem取得当前选中的条目,然后转成List中的对应类型来使用的。
上面的例子中只显示name对应的值,其实你也可以试一下这样:
SimpleAdapter adapter=new SimpleAdapter(this,items,R.layout.list_row,new String[]{"name","key"},new int[]{R.id.item,R.id.item2});
看看是什么效果。
再看看那个CursorAdapter吧,它的列表中元素要求是Cursor,这东西与DB有关,不过最简单的DB就是通讯簿。先从Contacts.People入手吧,同样修改代码:
<//CursorAdapter demo
Cursor mCursor = this.getContentResolver().query(Contacts.People.CONTENT_URI, null, null, null, null);
SimpleCursorAdapter adapter=new SimpleCursorAdapter(this,R.layout.list_row,mCursor,new String[]{Contacts.People.NAME},new int[]{R.id.item});
因为单纯的CursorAdapter是抽象类,所以我用的是它的子类SimpleCursorAdapter,很好理解,先用ContentResolver查询通讯簿得到一个游标,然后告诉SimpleCursorAdapter要用其中的People.NAME作为显示项来构造出一个adapter即可。
现在的onListItemClick也不一样了,如下:
< protected void onListItemClick(ListView l, View v, int position, long id)
{
< TextView txt = (TextView)this.findViewById(R.id.text);
< Cursor c = (Cursor)l.obtainItem(position);
< txt.setText("SEL = "+c.getString(c.getColumnIndex(Contacts.People.NUMBER)));
< }
这里同样是先用obtainItem取到游标,然后用从记录中取出想要的字段显示即可。在做这个例子时,因为权限的问题我们还得修改一下AndroidManifest.xml文件,让我们的应用可以访问到通讯簿:
<<manifest xmlns:android="http://schemas.android.com/apk/res/android"
< package="cn.sharetop.android.hello.two">
< <uses-permission id="android.permission.READ_CONTACTS" />
< <application android:icon="@drawable/icon">
< ... ...
发表评论
-
[Android Training视频系列] 8.3 Dealing with Audio Output Hardware
2013-07-19 21:29 711用户在播放音乐的时候 ... -
[Android Training视频系列] 8.2 Managing Audio Focus
2013-07-19 00:20 1075视频讲解:http://www.eyeandroid.com/ ... -
[Android Training视频系列] 8.1 Controlling Your App’s Volume and Playback
2013-07-17 22:26 796主要内容: 1 鉴别使用 ... -
[Android教程]通过Intent分享数据内容给其他应用程序
2013-05-16 07:57 644一:主要内容 本文档首先介绍如何通过发送Intent来分享数据 ... -
[Android Training视频系列] 6.3 Saving Data in SQL Databases(保存数据到SQLite)
2013-05-06 07:54 734一 主要内容 1.概述 2.定义Schema与Contract ... -
[Android Training视频系列] 6.2 Saving Files(保存文件)
2013-02-19 00:11 717一:主要内容 1.概述 2.选择内部存储还是外部存储 3.获取 ... -
【Android您问我讲】Android 2.x中使用actionbar - Actionbarsherlock的使用
2013-01-06 11:34 16271. 范例说明 从Android 3.0开始,Android引 ... -
【Android您问我讲】带左右滑动的选项卡 - ViewPager与Tabhost的结合使用
2013-01-02 00:34 22241. 范例说明 ViewPager实现滑动切换的效果,但是切换 ... -
【Android您问我讲】如何使用选显卡 - Tabhost的使用
2012-12-23 00:40 10621. 范例说明 TabHost是Android系统中提供的一 ... -
[Android Training视频系列]6.1 Saving Key-Value Sets
2012-12-22 10:35 7741.主要内容 本小节介绍S ... -
[Android Training视频系列]2.4 Recreating an Activity
2012-12-18 00:34 8891.主要内容 本讲讲解onSaveInstanceState与 ... -
【Android您问我讲】超炫的左右滑动效果 - ViewPager的使用
2012-12-16 20:45 9831. 范例说明 •android-support-v4.jar ... -
[Android Training视频系列]2.3 Stopping and Restarting an Activity
2012-12-16 13:14 8921.主要内容 本小节介绍onStop与onRestart以及 ... -
[Android Training视频系列]2.2 Pausing and Resuming an Activity
2012-12-14 00:58 9811.主要内容 本讲介绍onPause和onResume,主要分 ... -
[Android Training视频系列]2.1 Starting an Activity
2012-12-13 00:07 8451.主要内容 本小节首先对Activity的各个生命周期作简 ... -
[Android Training视频系列]1.4 Starting Another Activity
2012-12-12 00:29 7981.主要内容 本小结在1.3 ... -
[Android Samples视频系列之ApiDemos] App-Activity-TranslucentBlur和Wallpaper
2012-12-09 23:56 7231.Demo说明与演示 TranslucentBulr :这个 ... -
[Android Samples视频系列之ApiDemos] App-Activity-Translucent
2012-12-05 00:12 8451.Demo说明与演示 该Demo显示一个半透明Activit ... -
[Android Samples视频系列之ApiDemos] App-Activity-SetWallpaper
2012-12-03 00:12 9041.Demo说明与演示 该Demo介绍如何在Android获取 ... -
【Android每日一讲】2012.11.29 自定义下拉菜单模式 - Spinner与setDropDownViewResource
2012-12-01 13:19 10681. 范例说明 Spinner就是下拉菜单,由于手机画面有限, ...
相关推荐
Android-ListActivity实现列表[汇编].pdf
NULL 博文链接:https://ylxy3058.iteye.com/blog/2229306
android 以动态列表配置选项 ListActivity 与Menu整合技巧
android 从activity用intent跳转到listactivity并在其中用simpleadapter在listview中显示数据列
Android开发教程_018_使用ListActivity创建列表菜单_标清
aspects of using a ListView/ListActivity to display items in a list, possibly with header(s) and footer(s) and various possible events tied to different parts of the layout. It will evolve as I find ...
extends ListActivity 和 extends BaseAdapter
NULL 博文链接:https://l-x.iteye.com/blog/1408246
Android开源代码解读のOnScrollListener实现ListActivity滚屏首字母提示_IT168文库ListActivity
本文实例分析了Android中ListActivity用法。分享给大家供大家参考,具体如下: 程序如下: import android.app.ListActivity; import android.os.Bundle; import android.view.Menu; import android.view.MenuItem;...
android 查找程序根目录下所有文件 java i/o与ListActivity的结合
活动1.SlidingActivity ----活动2.SlidingFragmentActivity ------- FragmentActivity 3.SlidingListActivity ------- ListActivity
import android.app.ListActivity; import android.os.Bundle; import android.util.Log; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android....
4.4 用checkbox实现一个简单的物品清单 4.5 实现同意条款效果 4.6 radiogroup选择 4.7 imageview相框 4.8 spinner选择处理 4.9 gallery相簿 4.10 用.iava.io.file实现文件搜索 4.11 置换imagebutton按钮 4.12 实现...
在ListActivity中显示图标,好像并不复杂,实现起来却不轻松,我们下面一步步来实现ListActivity中显示图标
这里运用Java I/O、ListActivity、Dialog、Bitmap等实现简单文件管理器,可以查看目录文件,修改文件名,删除文件,打开文件。比较简单,直接看代码: 先看布局文件: layout/main.xml <?xml version=1.0 ...
该资源讲解了如何使用ListActivity中自带的ListView. 如果在 onCreate 方法中添加代码 setContentView(R.... 来设置ListActivity的布局,那在layout文件中必须添加 ListView 控件,而且它的id为 “android:list”.
很不错的Android文件管理器程序,实现了界面ListActivity,以及文件、路径选择,具有良好的界面和不错的功能
显示 ListActivity 和 TypedArrays 的简单 Android 代码。 旧代码迁移至顶级 Android Studio。 去做: 使用 json 从 res/raw 加载状态数据 创建细节片段 针对平板电脑进行优化 使用 RecyclerView 应用材料设计 ...