您的位置:首页 > 其它

ListView 与 SimpleAdapter的简单用法

2014-03-27 16:03 435 查看
SimpleAdapter的构造函数是:

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

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

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

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

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

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

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

比如下面这个实例:



一: 1.ListView   ------->    A
view that shows items in a vertically scrolling list. The items come from   the ListAdapter associated
with this view.(通过垂直滚动条查看的列表视图,ListAdapter里的内容和视图相关联。)    用法详见http://developer.android.com/reference/android/widget/ListView.html

 

2.创建ListView的两种方式:

a)       直接使用ListView进行创建。

b)       让Activity继承ListActivity(相当于显示的组件为ListView)。

 

:列表显示的三元素:数据、视图、适配器

1.ListView这个组件用于显示;

2.适配器用于绑定数据,即将数据映射到ListView上;

3.数据需映射到ListView的数据可以是字符串、图片或者基本的组件。

 

:适配器的类型:

1.ArrayAdapter  只能显示一行字。

2.SimpleAdapter 有最好的扩充性,可以定义出各种各样的布局出来,可以放ImageView 、Button 、CheckBox 等。使用时直接继承ListActivity 。

3.SimpleCursorAdapter可以方便的把数据库的内容以列表的形式展示出来。

 

特别注意:使用SimpleAdapter的数据一般都是HashMap构成的List ,list的每一节对应ListView的每一行。HashMap的每个键值数据映射到布局文件中对应的id的组件上。由于系统没有可对应的布局文件使用,一般我们自己定义一个布局main.xml;

适配构建过程:

参数1:this

参数2:A List of Maps(数据)

参数3:布局文件,main.xml用于显示在列表上的布局文件;

参数4:A list of column names that will be added to the Map associated with each item.(HashMap的键

参数5:The views that should display column in the "from" parameter. These should all be TextViews.

(布局文件的组件ID)

 

示例如下:

MainActivity:

package com.example.listviewdemo;

import java.util.ArrayList;

import java.util.HashMap;

import android.widget.*;

import android.os.Bundle;

import android.app.Activity;

import android.view.Menu;

public class MainActivity extends Activity

{

private ListView lv;

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.main);

lv = (ListView)findViewById(R.id.listview);

//构造一个适配器

SimpleAdapter adapter = new SimpleAdapter(MainActivity.this, getData(), R.layout.item,

      new String[]{"bookname","writer"}, new int[]{R.id.bookname,R.id.writer});

lv.setAdapter(adapter);

}

 

private ArrayList<HashMap<String,String>> getData()

{

ArrayList<HashMap<String,String>> list = new ArrayList<HashMap<String,String>>();

HashMap<String,String> map1 = new HashMap<String,String>();

HashMap<String,String> map2 = new HashMap<String,String>();

HashMap<String,String> map3 = new HashMap<String,String>();

HashMap<String,String> map4 = new HashMap<String,String>();

map1.put("bookname", "三国演义");

map1.put("writer","罗贯中");

map2.put("bookname", "红楼梦");

map2.put("writer","曹雪芹");

map3.put("bookname", "水浒传");

map3.put("writer", "施耐庵");

map4.put("bookname", "西游记");

map4.put("writer","吴承恩");

list.add(map4);

list.add(map3);

list.add(map2);

list.add(map1);

return list ;

}

@Override

public boolean onCreateOptionsMenu(Menu menu) {

// Inflate the menu; this adds items to the action bar if it is present.

getMenuInflater().inflate(R.menu.main, menu);

return true;

}

}

 

 

item.xml

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

android:layout_width="fill_parent"

android:layout_height="fill_parent"

android:orientation="horizontal"

android:paddingLeft="10dip"

android:paddingRight="10dip"

android:paddingTop="1dip"

android:paddingBottom="1dip">

<TextView

android:id="@+id/bookname"

android:layout_width="180dip"

android:layout_height="30dip"

android:singleLine="true"/>

<TextView

android:id="@+id/writer"

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:gravity="right"/>

</LinearLayout>

 

 

 

main.xml

<LinearLayout

xmlns:android="http://schemas.android.com/apk/res/android"

android:orientation="vertical"

android:layout_width="fill_parent"

android:layout_height="fill_parent">

 

<ListView

android:id="@+id/listview"

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:drawSelectorOnTop="true"/>

</LinearLayout>

 

 

效果图:





 

以上就关于是ListView的一种简单用法。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: