SimpleAdapter是扩展性最好的适配器。以下是参数详细介绍:
SimpleAdapter(Context context, List<? extends Map<String, ?>> data, int resource, String[] from, int[] to)
参数context:表示上下文对象或者环境对象。
参数data: 表示数据源。往往采用List<Map<String, Object>>集合对象。
Map列表,列表要显示的数据,如例子中的getData(),类型要与上面的一致,每条项目要与from中指定条目一致
参数resource:自定义的ListView中每个item的布局文件的资源id。用R.layout.文件名的形式来调用。
ListView单项布局文件的Id,这个布局就是你自定义的布局了,你想显示什么样子的布局都在这个布局中。
这个布局中必须包括了to中定义的控件id
参数 from: 其实是数据源中Map的key组成的一个String数组。
一个被添加到Map上关联每一个项目列名称的列表,数组里面是列名称
参数 to: 表示数据源中Map的value要放置在item中的哪个控件位置上。其实就是自定义的item布局文件中每个控件的id。
是一个int数组,数组里面的id是自定义布局中各个控件的id,需要与上面的from对应
下面是个自定义LIstView的例子:
1、MainActivity.java
package com.example.listview;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.Toast;
public class MainActivity extends Activity {
private ListView listView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
SimpleAdapter adapter = new SimpleAdapter(MainActivity.this, getData(), R.layout.listview_item, new String[]{"touxiang","bianhao","xingming","miaoshu"}, new int[]{R.id.touxiang,R.id.bianhao,R.id.xingming,R.id.miaoshu});
listView = (ListView) findViewById(R.id.listView);
listView.setAdapter(adapter);
listView.setOnItemClickListener(new OnItemClickListener() {
@SuppressWarnings("unchecked")
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
ListView myListView = (ListView) arg0;
HashMap<String, Object> item = ((HashMap<String, Object>) myListView.getItemAtPosition(arg2));
Toast.makeText(MainActivity.this, item.get("bianhao").toString(), Toast.LENGTH_LONG).show();
}
});
}
//获取数据:1.可从本地获取 2.从数据库获取
private List<HashMap<String, Object>> getData(){
List<HashMap<String,Object>> data = new ArrayList<HashMap<String,Object>>();
HashMap<String, Object> hashMap = new HashMap<String, Object>();
//hashMap.put("id", student.getStudentId());
hashMap.put("touxiang", R.drawable.ic_launcher);
hashMap.put("bianhao", "编号");
hashMap.put("xingming", "姓名");
hashMap.put("miaoshu", "描述");
data.add(hashMap);
hashMap = new HashMap<String, Object>();
hashMap.put("touxiang", R.drawable.ic_launcher);
hashMap.put("bianhao", "001");
hashMap.put("xingming", "张三");
hashMap.put("miaoshu", "三好坏蛋");
data.add(hashMap);
return data;
}
@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;
}
}
2、activity_main.xml
<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"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<ListView
android:id="@+id/listView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:divider="#00ff00"
android:dividerHeight="1dp"
/>
</RelativeLayout>
3、listview_item.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/touxiang"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="3dp"
/>
<TextView
android:id="@+id/bianhao"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
/>
<TextView
android:id="@+id/xingming"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
/>
<TextView
android:id="@+id/miaoshu"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
/>
</LinearLayout>