ListActivity 与SimpleAdapter

ListActivity是一个绑定到一个数据源,并且用来显示这一串数据的Activity。

ListActivity拥有一个listview对象来实现数据源的绑定与显示,通常会是一个array或者一个拥有查询结果的cursor.

ListActivity本身有一个默认的layout,其中包含一个全屏的list。

如果用默认的layout,你必须要在onCreate()中注释掉setContentView()那一句。

但是如果你如果你想要定制自己的layout你可以创建一个你自己的layout文件,并且在onCreate()中调用 setContenttView()来指定这个layout.,需要注意的是你自己的layout中必须要有一个id 为"@android:id/list"的ListView


public SimpleAdapter (Context context, List<? extends Map<String, ?>> data, int resource, String[] from, int[] to)

官方说明了其各个参数含义,我这里根据自己的理解解释下:

第一个context,很明显大家根据英文可以知道是上下文的意思,它官方的意思是:SimpleAdapter所要运行关联到的视图,这个是什么呢?就是你这个SimpleAdapter所在的Activity(一般而言),所以这个参数一般是this

第二个是一个泛型只要是一个List就行,这一般会想到是ArrayList,而他内部存储的则是Map或者继承自Map的对象,比如HashMap,这些语法都是Java的基本语法,不再详述了!这里呢是作为数据源,而且每一个ArraList中的一行就代表着呈现出来的一行,Map的键就是这一行的列名,值也是有列名的。

第三个资源文件,就是说要加载这个两列所需要的视图资源文件,你可以左边一个TextView右边一个TextView,目的在于呈现左右两列的值!

第四个参数是一个数组,主要是将Map对象中的名称映射到列名,一一对应

第五个是将第四个参数的值一一对象的显示(一一对应)在接下来的int形的id数组中,这个id数组就是LayOut的xml文件中命名id形成的唯一的int型标识符


mail.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
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="ListView Demo"
/>
<ListView
android:layout_height="wrap_content"
android:id="@id/android:list"
android:layout_width="fill_parent"
></ListView>
</LinearLayout>



user.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView android:id="@+id/user"
android:layout_width="wrap_content"
android:layout_height="wrap_content"

/>
<TextView android:id="@+id/sex"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="right"

/>

</LinearLayout>





package com.demo;

import java.util.ArrayList;
import java.util.HashMap;

import android.app.Activity;
import android.app.ListActivity;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.SimpleAdapter;

public class Activity1 extends ListActivity {
/** Called when the activity is first created. */



private ArrayList getDataList(){
ArrayList<HashMap<String, String>> list = new ArrayList<HashMap<String, String>>();
HashMap<String, String> map = new HashMap<String, String>();
map.put("name", "zhangsan");
map.put("sex", "male");

HashMap<String, String> map2 = new HashMap<String, String>();
map2.put("name", "Kate");
map2.put("sex", "female");

HashMap<String, String> map3 = new HashMap<String, String>();
map3.put("name", "李四");
map3.put("sex", "男");

list.add(map);
list.add(map2);
list.add(map3);
return list;
}


//处理ListView
private void doListView(){

ListAdapter adapter = new SimpleAdapter(
this, getDataList(), R.layout.user,
new String[]{"name", "sex"},
new int[]{R.id.user, R.id.sex});

this.setListAdapter(adapter); //ListActivity本身含有ListView
}




@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);


doListView();

}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值