首页 > 代码库 > 一起学android之SimpleAdapter使用(13)

一起学android之SimpleAdapter使用(13)

SimpleAdapter:将List集合的多个对象包装成列表项。

我们可以通过SimpleAdapter来实现一些复杂的列表,请看以下实例:


simpleadapter_list布局文件:

<span style="font-size:18px;"><?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/lv_simpleadapter"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" >
    </ListView>

</LinearLayout></span>

simpleadapter布局文件:用于显示列表项的组件

<span style="font-size:18px;"><?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >
    <LinearLayout 
        android:id="@+id/ll_top"
        android:layout_height="wrap_content"
        android:layout_width="fill_parent"
        android:orientation="horizontal">
    <ImageView 
        android:id="@+id/iv_image"
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        android:scaleType="fitXY"
        android:src=http://www.mamicode.com/"@drawable/a5i">
SimpleAdapterTest主文件:

<span style="font-size:18px;">public class SimpleAdapterTest extends Activity {
	int[] images = new int[] { R.drawable.a5i, R.drawable.a5j, R.drawable.a5k };
	String[] titles = new String[] { "电话", "QQ", "联系人" };
	String[] des = new String[] { "当前无电话记录", "当前无QQ聊天记录", "最近无联系人" };

	private ListView lv_simple;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.simpleadapter_list);
		initView();
		setData();
	}

	private void initView() {
		lv_simple = (ListView) findViewById(R.id.lv_simpleadapter);
		lv_simple.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				Toast.makeText(SimpleAdapterTest.this, titles[position],
						Toast.LENGTH_SHORT).show();
			}
		});
	}

	private void setData() {
		// 创建数据源
		List<HashMap<String, Object>> listItems = new ArrayList<HashMap<String, Object>>();
		for (int i = 0; i < titles.length; i++) {
			// 每个列表项的内容
			HashMap<String, Object> listItem = new HashMap<String, Object>();
			listItem.put("image", images[i]);
			listItem.put("title", titles[i]);
			listItem.put("desc", des[i]);
			listItems.add(listItem);
		}
		/*
		 * SimpleAdapter(Context context, List<? extends Map<String, ?>> data, int resource, String[] from, int[] to)
		 * 参数说明:
		 * context:整个应用的上下文。
		 * data:是List<? extends Map<String, ?>>的集合对象,其中每个Map<String, ?>代表每列的列表项内容。
		 * resource:界面布局文件的ID。
		 * from:String[]类型的参数,指定了Map<String, ?>中的每个key对应的value来生成列表项。
		 * to:int[]类型的参数,显示每个列表项显示的组件。
		 */
		SimpleAdapter simpleAdapter = new SimpleAdapter(SimpleAdapterTest.this,
				listItems, R.layout.simpleadapter, new String[] { "image",
						"title", "desc" }, new int[] { R.id.iv_image,
						R.id.tv_title, R.id.tv_des });
		
		//绑定适配器
		lv_simple.setAdapter(simpleAdapter);
	}
}</span>

技术分享





转载请注明出处:http://blog.csdn.net/hai_qing_xu_kong/article/details/42361041  情绪控_







一起学android之SimpleAdapter使用(13)