Android之SimpleAdapter使用

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="@drawable/a5i"
        android:layout_gravity="center"/>
    <LinearLayout 
        android:id="@+id/ll_right"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:layout_marginTop="4dp">
        <TextView 
            android:id="@+id/tv_title"
            android:layout_height="wrap_content"
            android:layout_width="wrap_content"
            android:textSize="20sp"
            android:text="AS"/>
         <TextView 
            android:id="@+id/tv_des"
            android:layout_height="wrap_content"
            android:layout_width="wrap_content"
            android:textSize="13sp"
            android:text="AS"
            />
    </LinearLayout>
</LinearLayout>
</RelativeLayout></span>


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  情绪控_

 

 

 

 

 

 

  • 6
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值