监听器返回fasle,则事件还会分发给其他监听器。
SimpleAdapter是BaseAdapter的子类,对适配器进行了简化,数据的格式是List,List的元素必须是Map, public SimpleAdapter(Context context, List<? extends Map<String, ?>> data,
int resource, String[] from, int[] to) {//Activity是Context的子类,resource是每一行ListViewItem布局,from是Map中头像和名字的所有的键,所有头像的健和所有名字的健,to是ListViewItem(每一行)布局的id值,
开发不灵活。
MainActivity.java
package com.sxt.day05_02; 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; public class MainActivity extends Activity { ListView mlvGenerals;//<ListView/>
List<Map<String, Object>> mGenerals;
SimpleAdapter mAdapter; static final String KEY_PHOTOID="photoid";
static final String KEY_NAME="name"; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initData();//初始化数据
initView();
} private void initView() {
mlvGenerals=(ListView) findViewById(R.id.lvGeneral);
mAdapter=new SimpleAdapter(this, mGenerals,
R.layout.item_generals,
new String[]{KEY_PHOTOID,KEY_NAME},
new int[]{R.id.ivThumb,R.id.tvName});
mlvGenerals.setAdapter(mAdapter);
} private void initData() {
int[] resid={
R.drawable.baiqi,R.drawable.caocao,R.drawable.chengjisihan,
R.drawable.hanxin,R.drawable.lishimin,R.drawable.nuerhachi,
R.drawable.sunbin,R.drawable.sunwu,R.drawable.yuefei,
R.drawable.zhuyuanzhang
};
String[] names=getResources().getStringArray(R.array.generals);//R.array.generals是string.xml中定义的数组,
mGenerals=new ArrayList<Map<String,Object>>();
HashMap<String, Object> general;
for (int i = 0; i < names.length; i++) {//List是一个集合,
general=new HashMap<String, Object>();//集合里面是Map
general.put(KEY_PHOTOID, resid[i]);//每一个Map里面放了2个键值对
general.put(KEY_NAME, names[i]);
mGenerals.add(general);
}
} }
activity_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" > <ListView
android:id="@+id/lvGeneral"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:divider="#ccc"
android:dividerHeight="2dp"/> </RelativeLayout>
item_generals.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="wrap_content"
android:orientation="horizontal" >
<ImageView
android:id="@+id/ivThumb"
android:layout_width="80dp"
android:layout_height="80dp"
android:scaleType="fitXY"
android:src="@drawable/baiqi"/>
<TextView
android:id="@+id/tvName"
android:layout_width="wrap_content"
android:layout_height="80dp"
android:layout_marginLeft="10dp"
android:text="白起"
android:textSize="20sp"
android:gravity="center_vertical"/>
</LinearLayout>