系统的listview的layout在frameworks/base/core/res/res下面
1 simple_list_item_1.xml
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@android:id/text1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:gravity="center_vertical"
android:paddingLeft="6dip"
android:minHeight="?android:attr/listPreferredItemHeight"
/>
的确够simple吧。。
2 simple_list_item_2.xml
<TwoLineListItem xmlns:android="http://schemas.android.com/apk/res/android"
android:paddingTop="2dip"
android:paddingBottom="2dip"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:minHeight="?android:attr/listPreferredItemHeight"
android:mode="twoLine"
>
<TextView android:id="@android:id/text1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="6dip"
android:layout_marginTop="6dip"
android:textAppearance="?android:attr/textAppearanceLarge"
/>
<TextView android:id="@android:id/text2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="@android:id/text1"
android:layout_alignLeft="@android:id/text1"
android:textAppearance="?android:attr/textAppearanceSmall"
/>
</TwoLineListItem>
列表每项都有两行内容的listview
3. simple_list_item_checked.xml
<CheckedTextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@android:id/text1"
android:layout_width="fill_parent"
android:layout_height="?android:attr/listPreferredItemHeight"
android:textAppearance="?android:attr/textAppearanceLarge"
android:gravity="center_vertical"
android:checkMark="?android:attr/textCheckMark"
android:paddingLeft="6dip"
android:paddingRight="6dip"
/>
4. simple_list_item_single_choice.xml
<CheckedTextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@android:id/text1"
android:layout_width="fill_parent"
android:layout_height="?android:attr/listPreferredItemHeight"
android:textAppearance="?android:attr/textAppearanceLarge"
android:gravity="center_vertical"
android:checkMark="?android:attr/listChoiceIndicatorSingle"
android:paddingLeft="6dip"
android:paddingRight="6dip"
/>
5.
simple_list_item_multiple_choice.xml
<CheckedTextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@android:id/text1"
android:layout_width="fill_parent"
android:layout_height="?android:attr/listPreferredItemHeight"
android:textAppearance="?android:attr/textAppearanceLarge"
android:gravity="center_vertical"
android:checkMark="?android:attr/listChoiceIndicatorMultiple"
android:paddingLeft="6dip"
android:paddingRight="6dip"
/>
这些代码很简单,功能却很强大。
甚至可以在里面添加饰品音频等。。求相关资料。
这里转一些其他看见的资料。
-------------------
http://dev.10086.cn/cmdn/bbs/viewthread.php?tid=17908
Android之ListActivity(一):布局与数据绑定
Android中的列表,当然也可以用ListView来完成所需要的功能,用法是一样的。
废话不说,来关键的。
LiveActivity本身继承了关于List操作的众多接口,我们可以方便的重写这些操作中需要的方法来实现自己需要的功能。
如果要用ListActivity,则 Activity的Layout文件中必须包括一个(只能一个)ListView,且ListView的id= "@id/android:list"。
如下代码,一个标准的ListActivity Layout文件:
<?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"
android:paddingLeft="8dp"
android:paddingRight="8dp">
<ListView android:id="@id/android:list"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#00FF00"
android:layout_weight="1"
android:drawSelectorOnTop="false"/>
<TextView id="@id/android:empty"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#FF0000"
android:text="No data"/>
</LinearLayout>
请注意 ListView与TextView的id。前面说了,ListView的Id为固定不变的,为”@./idandroid:ost”,ListActivity会根据id自动查找ListView引用;但如果当ListView中没有值而又想提示一句话时,那么用于指定显示提示信息的TextView的id 必须为”"@id/android:empty",提示的信息可以通过android:text进行指定。
OK,关于如何布局说完了,那么如何给List绑定值,并进行操作呢?
首先我们需要确实的是,ListView的布局也完成了,并通过调用setContentView(…)进行了绑定,但直到现在我们还没有确定ListView中的第一行显示的格式是什么,是直接显示文字还是要“图文并茂”的显示。
Android系统为我们提供了多种模板进行选择(android.R.layout),如
Ø Simple_list_item_1 每项有一个TextView
Ø Simple_list_item_2 每项有两个TextView
Ø Simple_list_item_checked 带CheckView的项
Ø Simple_list_item_multiple_choise 每项有一个TextView并可以多选
Ø Simple_list_item_single_choice 每项有一个TextView,但只能进行单选。
但然,如果以上项模板还无法满足你的要求,那只能自定义模板了(相当简单,就是定义一个layout布局)。如果你做的asp.net的开发的话,是否对dataList控件有印象呢。如果对DataList有印象,那么理解ListView也就相当的简单了。
自定义模板可以根据自己的需要定义成任意的格式,包括图片、方案及其他可显示的View,不用多说,自己定义就好了,关键是如果使用并进行模板的绑定。
如何要对ListView进行数据绑定,必须使用到一个接口:Adapter。
其中最经常与ListView进行配合使用的有ArrayAdapter、 CursorAdapter及SimpleAdapter等。
从名称可以看出ArrayAdapter使用的是一个ArrayAdapter做为数据源,SimpleCursorAdapter使用的是一个Cursor使用数据源,都比较容易理解,那么如何使用SimpleAdapter作为数据的适配器呢。Ok,从易到难。
ArrayAdapter:
String[] data = { "Item1", "Item2",
"Item3", "Item4", "Item5" };
listView.setAdapter(new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_single_choice, data));
SimpleCursorAdapter:
//从数据库中查询Cursor
cursor = adapter.getAllNotes();
startManagingCursor(cursor);
//设置要显示的数据源中的列名(需要包含在cursor中)
String[] from = new String[] { DiaryDbAdapter.KEY_COLUMN_TITLE,
DiaryDbAdapter.KEY_COLUMN_CREATEED };
//显示的View(自定义模板中的View)
int[] to = new int[] { R.id.txtRowTitle, R.id.txtRowCreateed };
//绑定
SimpleCursorAdapter notes = new SimpleCursorAdapter(this,
R.layout.diaryrow, cursor, from, to);
setListAdapter(notes);、
SimpleAdapter:
SimpleAdapter将一个List做为数据源,可以让ListView进行更加个性化的显示。而List中的第一项是个Map<String,?>(用到泛型),其中Map中的每项将与ListView中的每项进行一一对应绑定。Ok,看一下构造:
SimpleAdapter(Context context,List<? Extends Map<String,?>> data,int resource,String [] form, int [] to);
² Context:当前上下文,一般把Activity.this传递进行。
² Data: 数据源。
² Resource: 自定义的layout模板资源,可以用 R.layout.xxx获取引用。
² Form: 定义ListView中的每一项数据索引,索引来自于Map<String,?>,即指定要显示的内容。
² To:View数组,在ListView模板中的定义View,与Form中需要一一对应。
事例代码:
List<Hashtable<String, Object>> listContent
= new ArrayList<Hashtable<String, Object>>();
for (int i = 0; i < deviceList.size(); i++) {
Hashtable<String, Object> table
= new Hashtable<String, Object>();
table.put("name", deviceList.get(i).Name);
table.put("address", deviceList.get(i).Address);
table.put("type", deviceList.get(i).Type + "");
listContent.add(table);
}
adapter = new SimpleAdapter(HeartActivity.this,
listContent, R.layout.child, //自定义的layout
new String[] { "name", "address" },
new int[] {R.id.txtDeviceName, R.id.txtDeviceAddress });
setListAdapter(adapter);
以上代码使用了Hashtable做为一个Map,并添加到一个List<Hashtable<String, Object>>当中。
之后new一个SimpleAdapter,注意SimpleAdapter是如何生成的。
转自:http://www.cnblogs.com/wservices/archive/2010/06/17/1759793.html
--------------------------
还有个ophone的科普说明。
http://www.ophonesdn.com/article/show/93
分享到:
相关推荐
安卓自定义list的listitem,item中添加图片,按钮等元素
item_simple_list_2.xml
Android源码:ListView组件应用演示代码,layout的xml文件名需要全部小写。 ActivityList1: 1.初步介绍ListView这个组件。 2.... ... 2.simple_list_item_1和simple_list_item_2的介绍。
R.layout.list_say_me_item); list.add(d1); DetailEntity d2 = new DetailEntity("美女", "2010-11-11", "你好!", R.layout.list_say_he_item); list.add(d2); DetailEntity d3 = new DetailEntity...
经常会遇到ExpandableListView这个控件,有的时候会有需求是当下拉列表子item被选中时要进行颜色的改变,以便标识
在list control中加入鼠标右键弹出菜单并做出删除所点的item项
item_simple_list.xml
1.用分类做为导航拦 <?php wp_list_categories(‘depth=2&title_li=0&orderby=name&show_...复制代码代码如下: <nav> ”navDiv”> ”nav”> ”current_page_item”‘>首页</a></li> <?php //wp_list_pages(‘tit
wxCheckedListCtrl类 .h/.cpp 带checkbox 的wxListCtrl,从...wxEVT_COMMAND_LIST_ITEM_CHECKED / wxEVT_COMMAND_LIST_ITEM_UNCHECKED 选种和取消勾选事件,事件手柄类型为 wxListEvent& event. 经过测试,可以使用。
woaidu_item['author'] = [list_first_item(response_selector.select('//div[@class="xiaoxiao"][1]/text()').extract())[5:].strip(),] woaidu_item['book_description'] = list_first_item(response_selector....
List Control 可编辑任意item文本,设置行高 使用Owner Draw实现List Control的重绘
* LVS_LIST:显示一列带有小图标的 item * LVS_REPORT:显示 item 详细资料 可以使用 GetWindowLong 函数获取当前窗口的风格,然后使用位操作符来设置或清除风格位。例如: LONG lStyle; lStyle = GetWindowLong(m...
列出系统运行的进程并KILL某些进程,tlist 可以列出系统中正在运行的进程,kill 工具可以杀死进程,COMMON文件是公共函数
List with images shows alert when selected any list item Source code J2ME.
解雇 一个新的Flutter项目。 入门 该项目是Flutter应用程序的起点。 如果这是您的第一个Flutter项目,那么有一些资源可以帮助您入门: 要获得Flutter入门方面的帮助,请查看我们的,其中提供了教程,示例,有关移动...
更好的物品清单 从字面上看只是但更好 目标: 始终了解最新版本的krunker 比yeehow当前提供的搜索选项更多 基于url的搜索,因此您可以共享搜索链接 侧面的json信息和链接更详细的项目信息选项卡 ...
map.put("child_item_title", "九州八拖二火锅" + i); map.put("child_item_price_old", "4." + i + "分"); map.put("child_item_price_score", "(100人)"); map.put("parent_lv", childList); ...
# simple_list_item_single_choice # simple_list_item_1 # simple_list_item_multiple_choice 3.上边的相关介绍。 ActivityList2:(有错误,没有调试成功,回头再看) 1.介绍cusor。 2.介绍CursorAdapter。 ...
List 指令还隐含了两个循环变量:item_index:当前迭代项在所有迭代项中的位置,是数字值。item_has_next:用于判断当前迭代项是否是所有迭代项中的最后一项。注意:在使用上述两个循环变量时,一定要将 item 换成你...
multiSelectView.setAdapter(new ArrayAdapter(this, android.R.layout.simple_list_item_1, getResources().getStringArray(android.R.array.imProtocols)), new ArrayList()); Methods multiSelectView....