您的位置:首页 > 移动开发 > Android开发

android ListView之——SimpleAdapter使用

2015-08-17 20:19 579 查看
上一篇我们讲到了最基本的ArrayAdapter,这次来说一下SimpleAdapter,同样作为适配器,它们有什么区别呢?

与ArrayAdapter中适配的数据列表List< String>不同,SimpleAdapter使用List< Map>数据源。由于Map可以包含各种不同的控件资源,比如:图片、文本、按钮的组合等,所以SimpleAdapter可以为包含多个控件的Item填充数据。其中,List中的一个Map对应一个Item,Map中的一个元素对应Item中的一个控件资源,这样,就可以把SimpleAdapter中的数据填充到ListView中。

来实现如下的界面

效果图:



下面看代码:

SimpleAdapter_Aty.java

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.app.Activity;
import android.os.Bundle;
import android.widget.ListView;
import android.widget.SimpleAdapter;

import com.example.practice.R;

public class SimpleAdapter_Aty extends Activity {
private ListView listView;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.listview_aty);
listView = (ListView) findViewById(R.id.listView);

/*
* 这就是上述的List<Map>; API中给出的是List<? extends Map<String,Object>>;
* 所以说,其中Map要是Map<String,Object>它的子类; 需要注意的是:它的Key值是String类型,
* 它的Value是Object类型,我们可以想一下,由于这个map里面存放的是控件资源,所以Object类型是很有必要的。
*/
List<Map<String, Object>> data = new ArrayList<Map<String, Object>>();
HashMap<String, Object> map1 = new HashMap<String, Object>();
map1.put("img", android.R.drawable.ic_menu_add);
map1.put("id", "01");
map1.put("name", "mary");

HashMap<String, Object> map2 = new HashMap<String, Object>();
map2.put("img", android.R.drawable.ic_menu_always_landscape_portrait);
map2.put("id", "02");
map2.put("name", "lili");

HashMap<String, Object> map3 = new HashMap<String, Object>();
map3.put("img", android.R.drawable.ic_menu_camera);
map3.put("id", "03");
map3.put("name", "tom");

HashMap<String, Object> map4 = new HashMap<String, Object>();
map4.put("img", android.R.drawable.ic_menu_crop);
map4.put("id", "04");
map4.put("name", "jack");

HashMap<String, Object> map5 = new HashMap<String, Object>();
map5.put("img", android.R.drawable.ic_menu_directions);
map5.put("id", "05");
map5.put("name", "bob");

data.add(map1);
data.add(map2);
data.add(map3);
data.add(map4);
data.add(map5);

/*
* String[] from和int[] to是将Map里的数据和Item对应起来的关键; String[]
* from里面存放的是Map的Key,int[] to里面存放的是Item的资源Id。
* 什么意思呢,也就是把from里的资源,给to相对应的Id,它们是一一对应的,这里一定不能弄错了。
*/
String[] from = { "img", "id", "name" };
int[] to = { R.id.simpleAdapter_iv_icon, R.id.simpleAdapter_iv_id,
R.id.simpleAdapter_tv_name };

SimpleAdapter adapter = new SimpleAdapter(this, data,
R.layout.listview_item2, from, to);
listView.setAdapter(adapter);

}
}


listview_aty.xml

<?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="match_parent"
android:orientation="vertical" >

<ListView
android:id="@+id/listView"
android:layout_width="match_parent"
android:layout_height="match_parent" >
</ListView>

</LinearLayout>


listview_item2.xml

<?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="match_parent"
android:orientation="horizontal" >

<ImageView
android:id="@+id/simpleAdapter_iv_icon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="32dp" />

<TextView
android:id="@+id/simpleAdapter_iv_id"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="16dp" />

<TextView
android:id="@+id/simpleAdapter_tv_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="16dp" />

</LinearLayout>


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