Android 中 ListAactivity 和Activity 使用ListView 以及ArrayAdapter和SimpleAdapter参数详解

来源:互联网 发布:云南地税网络申报平台 编辑:程序博客网 时间:2024/05/22 02:11

在使用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:

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" };@Overrideprotected 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" };@Overrideprotected 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每一行的布局(同上)


0 0
原创粉丝点击