1673
- 收藏
- 点赞
- 分享
- 举报
Android学习笔记(5)-关于ListActivity的简单体验
Android学习笔记(5)-关于ListActivity的简单体验
今天学习点轻松的内容吧,看看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 items = fillArray();
ArrayAdapter adapter = new ArrayAdapter(this,R.layout.list_row,items);
this.setListAdapter(adapter);
}
private List fillArray()
{
List items = new ArrayList();
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描述一下这个列表窗口。
androidrientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text=""
/>
android:layout_width="fill_parent"
android:layout_height="0dip"
android:layout_weight="1"
android:drawSelectorOnTop="false"
/>
这里需要注意的是那个ListView的ID,是系统自定义的android:list,不是我们随便取的,否则系统会说找不到它想要的listview了。然后,在这个listview之外,我们又增加了一个TextView,用来显示选中的条目。
再来说说这里用到的ArrayAdapter,它的构造函数中第二个参数是一个资源ID,ArrayAdapter的API文档中说是要求用一个包含TextView的layout文件,平台用它来显示每个选择条目的样式,这里的取值是R.layout.list_row,所以,我们还有一个list_row.xml文件来描述这个布局,相当简单。
androidrientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
从ArrayAdapter上溯到BaseAdapter,发现还有几个同源的Adapter也应该可以使用,象SimpleAdapter和CursorAdapter,还是做个例子来实验一下吧。
先看看SimpleAdapter,说是simple却不simple。
首先看看这个fillMaps方法,基本上就明白这个simpleAdapter是怎么回事了,在有些场合它还是挺有用的,可以为每个条目绑定一个值:
private List> fillMaps()
{
List> items = new ArrayList>();
HashMap i = new HashMap();
i.put("name","日曜日");
i.put("key", "SUN");
items.add(i);
HashMap i1 = new HashMap();
i1.put("name","月曜日");
i1.put("key", "MON");
items.add(i1);
HashMap i2 = new HashMap();
i2.put("name","火曜日");
i2.put("key", "TUE");
items.add(i2);
HashMap i3 = new HashMap();
i3.put("name","水曜日");
i3.put("key", "WED");
items.add(i3);
HashMap i4= new HashMap();
i4.put("name","木曜日");
i4.put("key", "THU");
items.add(i4);
HashMap i5 = new HashMap();
i5.put("name","金曜日");
i5.put("key", "FRI");
items.add(i5);
HashMap i6 = new HashMap();
i6.put("name","土曜日");
i.put("key", "SAT");
items.add(i6);
return items;
}
然后,在HelloTwoB中的onCreate函数中,修改代码,有几个不同:items的元素是HashMap实例,这是一点变化,然后构造函数除了要求items以外,还要求提供一个string[]来说明用hash表中的哪个字段显示在列表中,而后是一个资源ID的数组。我的代码是这样的:
//SimpleAdapter demo
List> 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文件,让我们的应用可以访问到通讯簿:
package="cn.sharetop.android.hello.two">
... ...
来自: [url]www.android.org.cn[/url]
今天学习点轻松的内容吧,看看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
ArrayAdapter
this.setListAdapter(adapter);
}
private List
{
List
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描述一下这个列表窗口。
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
android:layout_height="wrap_content"
android:text=""
/>
android:layout_height="0dip"
android:layout_weight="1"
android:drawSelectorOnTop="false"
/>
这里需要注意的是那个ListView的ID,是系统自定义的android:list,不是我们随便取的,否则系统会说找不到它想要的listview了。然后,在这个listview之外,我们又增加了一个TextView,用来显示选中的条目。
再来说说这里用到的ArrayAdapter,它的构造函数中第二个参数是一个资源ID,ArrayAdapter的API文档中说是要求用一个包含TextView的layout文件,平台用它来显示每个选择条目的样式,这里的取值是R.layout.list_row,所以,我们还有一个list_row.xml文件来描述这个布局,相当简单。
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
从ArrayAdapter上溯到BaseAdapter,发现还有几个同源的Adapter也应该可以使用,象SimpleAdapter和CursorAdapter,还是做个例子来实验一下吧。
先看看SimpleAdapter,说是simple却不simple。
首先看看这个fillMaps方法,基本上就明白这个simpleAdapter是怎么回事了,在有些场合它还是挺有用的,可以为每个条目绑定一个值:
private List
{
List
HashMap
i.put("name","日曜日");
i.put("key", "SUN");
items.add(i);
HashMap
i1.put("name","月曜日");
i1.put("key", "MON");
items.add(i1);
HashMap
i2.put("name","火曜日");
i2.put("key", "TUE");
items.add(i2);
HashMap
i3.put("name","水曜日");
i3.put("key", "WED");
items.add(i3);
HashMap
i4.put("name","木曜日");
i4.put("key", "THU");
items.add(i4);
HashMap
i5.put("name","金曜日");
i5.put("key", "FRI");
items.add(i5);
HashMap
i6.put("name","土曜日");
i.put("key", "SAT");
items.add(i6);
return items;
}
然后,在HelloTwoB中的onCreate函数中,修改代码,有几个不同:items的元素是HashMap实例,这是一点变化,然后构造函数除了要求items以外,还要求提供一个string[]来说明用hash表中的哪个字段显示在列表中,而后是一个资源ID的数组。我的代码是这样的:
//SimpleAdapter demo
List
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文件,让我们的应用可以访问到通讯簿:
... ...
来自: [url]www.android.org.cn[/url]
我来回答
回答0个
时间排序
认可量排序

或将文件直接拖到这里
悬赏:
E币
网盘
* 网盘链接:
* 提取码:
悬赏:
E币
Markdown 语法
- 加粗**内容**
- 斜体*内容*
- 删除线~~内容~~
- 引用> 引用内容
- 代码`代码`
- 代码块```编程语言↵代码```
- 链接[链接标题](url)
- 无序列表- 内容
- 有序列表1. 内容
- 缩进内容
- 图片
相关问答
-
2012-11-27 20:39:59
-
2012-11-27 20:03:33
-
2012-11-27 20:07:15
-
2012-11-27 20:00:45
-
2013-08-29 15:08:54
-
02018-12-19 16:17:42
-
2018-12-07 17:44:57
-
02008-11-12 19:40:51
-
2015-06-01 16:14:50
-
2015-07-10 11:01:46
-
192019-08-20 16:57:22
-
2013-08-28 13:57:20
-
2015-07-15 11:52:17
-
2013-08-25 11:17:43
-
2013-08-25 11:11:11
-
2018-12-14 10:24:23
-
2019-09-09 16:47:05
-
2020-07-06 18:49:01
-
2013-08-25 12:02:13
无更多相似问答 去提问

点击登录
-- 积分
-- E币
提问
—
收益
—
被采纳
—
我要提问
切换马甲
上一页
下一页
举报反馈
举报类型
- 内容涉黄/赌/毒
- 内容侵权/抄袭
- 政治相关
- 涉嫌广告
- 侮辱谩骂
- 其他
详细说明
提醒
你的问题还没有最佳答案,是否结题,结题后将扣除20%的悬赏金
取消
确认
提醒
你的问题还没有最佳答案,是否结题,结题后将根据回答情况扣除相应悬赏金(1回答=1E币)
取消
确认