ListView所在布局文件:
<LinearLayout 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:orientation="vertical"
tools:context="com.yin.simpleadapter2.MainActivity" >
<ListView
android:id="@+id/listView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
>
</ListView>
</LinearLayout>
子条目的布局文件:
<?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/imageView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/ic_launcher"
/>
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="15dip"
android:text="李四"
/>
</LinearLayout>
Activity代码:
package com.yin.simpleadapter2;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.widget.ListView;
import android.widget.SimpleAdapter;
public class MainActivity extends ActionBarActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//找到listView控件
ListView listView = (ListView) findViewById(R.id.listView);
//创建一个simpleAdapter
/**
* public SimpleAdapter(Context context, List<? extends Map<String, ?>> data,
int resource, String[] from, int[] to) {
mData = data;
mResource = mDropDownResource = resource;
mFrom = from;
mTo = to;
mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
*/
List<Map<String,Object>> data=new ArrayList<Map<String,Object>>();
Map<String,Object> map=new HashMap<String, Object>();
map.put("img", R.drawable.ic_launcher);
map.put("name", "成龙1");
data.add(map);
map=new HashMap<String, Object>();
map.put("img", R.drawable.ic_launcher);
map.put("name", "成龙1");
data.add(map);
map=new HashMap<String, Object>();
map.put("img", R.drawable.ic_launcher);
map.put("name", "成龙1");
data.add(map);
map=new HashMap<String, Object>();
map.put("img", R.drawable.ic_launcher);
map.put("name", "成龙1");
data.add(map);
map=new HashMap<String, Object>();
map.put("img", R.drawable.ic_launcher);
map.put("name", "成龙1");
data.add(map);
map=new HashMap<String, Object>();
map.put("img", R.drawable.ic_launcher);
map.put("name", "成龙1");
data.add(map);
map=new HashMap<String, Object>();
map.put("img", R.drawable.ic_launcher);
map.put("name", "成龙1");
data.add(map);
map=new HashMap<String, Object>();
map.put("img", R.drawable.ic_launcher);
map.put("name", "成龙1");
data.add(map);
map=new HashMap<String, Object>();
map.put("img", R.drawable.ic_launcher);
map.put("name", "成龙1");
data.add(map);
map=new HashMap<String, Object>();
map.put("img", R.drawable.ic_launcher);
map.put("name", "成龙1");
data.add(map);
map=new HashMap<String, Object>();
map.put("img", R.drawable.ic_launcher);
map.put("name", "成龙1");
data.add(map);
map=new HashMap<String, Object>();
map.put("img", R.drawable.ic_launcher);
map.put("name", "成龙1");
data.add(map);
map=new HashMap<String, Object>();
map.put("img", R.drawable.ic_launcher);
map.put("name", "成龙1");
data.add(map);
SimpleAdapter adapter=new SimpleAdapter(this, //上下文
data, //Map类型的list集合,list中的每一个元素(map)就是ListView中的一个条目
R.layout.list_item,//指向条目布局的xml文件
new String[]{"img","name"},//map中的key组成的字符串数组
new int[]{R.id.imageView,R.id.textView}//条目中各个子内容,注意要与map中的key组成的String数组顺序对应
);
listView.setAdapter(adapter);
}
}