Android 中 ListAactivity 和Activity 使用ListView 以及ArrayAdapter和SimpleAdapter参数详解
2015-08-22 17:33
861 查看
在使用Activity和ListActivity 上的一些区别
首先看一下ArrayAdapter和SimpleAdapter的参数(参数名有可能不对,按顺序排列的)
*1.ArrayAdapter
Context 表示设备上下文(和c中的设备上下文相同,就是代表了该窗口)
resource 表示列表的每个行的布局(可以使用系统提供的,也可以自己定义)
data数据源 数组或list形式 的数据 最终以字符串的形式显示到
resource 指定的布局文件中的textView中(其实系统指定的布局文件中只有一个TextView) 但必须在一个布局文件中
2.SimpleAdapter
context同上(在一个窗口上显示东西,必须可以获得该窗口的属性,context可以 提供这些属性,context就代表该窗口)
data数据源ArrayList<Map<String,Object>> 类型的列表,该列表中每一 个map中都包含了列表一行中要显示的内容
resource 表示列表中每一行所使用的布局文件,该布局文件中包含了要显示map中内容 的控件 form 数据 列表中每个map包含的键,通过这些键可以找到要显示的内容
to 表示要将map的内容显示到的控件,from中表示的key按顺序取出的值,按顺序显示到 to 中列出的控件中,(from
和 to必须一一对应,例如:from中第一个key,从数据源 map中取出该key对应的值,要显示到to中第一个控件)
在Activity中使用ListView
1.和在Activity中使用其他控件一样,需要把listView定义到布局文件中
和使用其他控件一样定义到布局文件中,当activity调用setContentView时展示 该布局文件中的内容
* 2.创建一个适配器(ArrayAdapter 或 SimpleAdapter,详见上述参数说明) 3.findViewById
* 找到布局文件中的listView 4.为这个listView设置上面创建的Adapter,这样就把数据和展示视图用adapter练习起来了
*
src:
在ListActivity中使用ListView ListActivity对ListView 的使用做了优化,
*
* 1. 如果使用ListActivity,和Activity不同的是,Activity中ListView是被当作普通控件的方式来使用的
* 而ListActivity中在setContentView时系统需要为该Activity绑定一个ListView ,该ListView的 id
* 属性必须为"@android:id/list"不然系统是找不到的。所以需要修改上述布局文件,把id属性改为"@android:id/list"
*
* 2.这就完成了ListView于ListActivity的绑定,
* 如果要获得该ListView对象只需调用ListActivity中的getListView就可以 其他的和Activity基本相同
*src:
首先看一下ArrayAdapter和SimpleAdapter的参数(参数名有可能不对,按顺序排列的)
*1.ArrayAdapter
Context 表示设备上下文(和c中的设备上下文相同,就是代表了该窗口)
resource 表示列表的每个行的布局(可以使用系统提供的,也可以自己定义)
data数据源 数组或list形式 的数据 最终以字符串的形式显示到
resource 指定的布局文件中的textView中(其实系统指定的布局文件中只有一个TextView) 但必须在一个布局文件中
2.SimpleAdapter
context同上(在一个窗口上显示东西,必须可以获得该窗口的属性,context可以 提供这些属性,context就代表该窗口)
data数据源ArrayList<Map<String,Object>> 类型的列表,该列表中每一 个map中都包含了列表一行中要显示的内容
resource 表示列表中每一行所使用的布局文件,该布局文件中包含了要显示map中内容 的控件 form 数据 列表中每个map包含的键,通过这些键可以找到要显示的内容
to 表示要将map的内容显示到的控件,from中表示的key按顺序取出的值,按顺序显示到 to 中列出的控件中,(from
和 to必须一一对应,例如:from中第一个key,从数据源 map中取出该key对应的值,要显示到to中第一个控件)
在Activity中使用ListView
1.和在Activity中使用其他控件一样,需要把listView定义到布局文件中
和使用其他控件一样定义到布局文件中,当activity调用setContentView时展示 该布局文件中的内容
* 2.创建一个适配器(ArrayAdapter 或 SimpleAdapter,详见上述参数说明) 3.findViewById
* 找到布局文件中的listView 4.为这个listView设置上面创建的Adapter,这样就把数据和展示视图用adapter练习起来了
*
src:
public class MainActivity extends Activity { private int[] img = { R.drawable.ic_launcher, R.drawable.ic_launcher, R.drawable.ic_launcher, R.drawable.ic_launcher }; private String[] text = { "列表测试1", "列表测试2", "列表测试3", "列表测试4" }; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); initView(); } private void initView() { ArrayList<Map<String, Object>> dataList = new ArrayList<Map<String, Object>>(); for (int i = 0; i < 30; i++) { HashMap<String, Object> item = new HashMap<String, Object>(); item.put("img", img[0]); item.put("text", text[0] + ":" + i); dataList.add(item); } SimpleAdapter adapter = new SimpleAdapter(this, dataList, R.layout.list_main, new String[] { "img", "text" }, new int[] { R.id.imgView, R.id.tView }); ListView listView = (ListView) findViewById(R.id.list); listView.setAdapter(adapter); } }
R.layout.activity_main.<span style="font-family:Arial, Helvetica, sans-serif;">xml</span>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="horizontal" > <ListView android:id="@+id/list" android:layout_width="match_parent" android:layout_height="match_parent" > </ListView> </LinearLayout>
R.layout.list_main.xml每一行的布局
<pre name="code" class="html"><?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="wrap_content" > <TextView android:id="@+id/tView" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <ImageView android:id="@+id/imgView" android:layout_height="wrap_content" android:layout_width="wrap_content"/> </LinearLayout>
在ListActivity中使用ListView ListActivity对ListView 的使用做了优化,
*
* 1. 如果使用ListActivity,和Activity不同的是,Activity中ListView是被当作普通控件的方式来使用的
* 而ListActivity中在setContentView时系统需要为该Activity绑定一个ListView ,该ListView的 id
* 属性必须为"@android:id/list"不然系统是找不到的。所以需要修改上述布局文件,把id属性改为"@android:id/list"
*
* 2.这就完成了ListView于ListActivity的绑定,
* 如果要获得该ListView对象只需调用ListActivity中的getListView就可以 其他的和Activity基本相同
*src:
package com.shilec.main; import java.util.ArrayList; import java.util.HashMap; import java.util.Map; import android.app.ListActivity; import android.os.Bundle; import android.widget.ListView; import android.widget.SimpleAdapter; public class ListviewLayoutActivity extends ListActivity { private int[] img = { R.drawable.ic_launcher, R.drawable.ic_launcher, R.drawable.ic_launcher, R.drawable.ic_launcher }; private String[] text = { "列表测试1", "列表测试2", "列表测试3", "列表测试4" }; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.listview_layout); initView(); } private void initView() { ArrayList<Map<String, Object>> dataList = new ArrayList<Map<String, Object>>(); for (int i = 0; i < img.length; i++) { HashMap<String, Object> item = new HashMap<String, Object>(); item.put("img", img[i]); item.put("text", text[i] + ": ListActivity"); dataList.add(item); } SimpleAdapter adapter = new SimpleAdapter(this, dataList, R.layout.list_main, new String[] { "img", "text" }, new int[] { R.id.imgView, R.id.tView }); ListView listView = this.getListView(); listView.setAdapter(adapter); } }
R.layout.listview_layout.xml
<pre name="code" class="html"><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" > <ListView android:id="@android:id/list" android:layout_width="match_parent" android:layout_height="match_parent" > </ListView> </RelativeLayout>
</pre><pre name="code" class="java" style="font-size: 18px;">R.layout.list_main.xml每一行的布局(同上)
相关文章推荐
- 完美实现Android ListView中的TextView的跑马灯效果
- android上改变listView的选中颜色
- Delphi7中Listview的常用功能汇总
- Delphi控件ListView的属性及使用方法详解
- android中ListView数据刷新时的同步方法
- Android提高之ListView实现自适应表格的方法
- Android中实现水平滑动(横向滑动)ListView示例
- C#实现ListView选中项向上或向下移动的方法
- C# listview添加combobox到单元格的实现代码
- ListView Adapter优化 实例
- Android用ListView显示SDCard文件列表的小例子
- Adapter实现ListView带多选框等状态的自定义控件的注意事项
- asp.net ListView 数据绑定
- Android之ScrollView嵌套ListView和GridView冲突的解决方法
- android ListView和ProgressBar(进度条控件)的使用方法
- Repeater与ListView功能概述及使用介绍
- Android开发之ListView列表刷新和加载更多实现方法
- android ListView和GridView拖拽移位实现代码
- ListView的Adapter使用(绑定数据) 之 自定义每一项的布局去绑定数据
- Android实现为ListView同时设置点击时的背景和点击松手之后的背景