Android ListView使用SimpleAdapter

ArrayAdapter只能实现简单的ListView的数据绑定,用SimpleAdapter能实现复杂的数据绑定,使用的步骤。
(1)定义一个HashMap构成的列表,将ListView中item的内容以键值对的方式存放在里面。
(2)通过实现SimpleAdapter的构造函数来创建一个SimpleAdapter的对象。
(3)通过ListView的setAdapter()方法绑定SimpleAdapter。

res/layout/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"   
    tools:context=".MainActivity" >
</RelativeLayout>
res/layout/list.xml
<?xml version="1.0" encoding="utf-8"?>
<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" >  
	<TextView  
	    android:id="@+id/ItemTitle"
	    android:layout_width="wrap_content"
	    android:layout_height="wrap_content"
	    android:textSize="22sp"
	   >
	</TextView>
	<TextView 
	    android:id="@+id/ItemContent"
	    android:layout_width="wrap_content"
	    android:layout_height="wrap_content"   
	    android:textSize="13sp"
	   >
	</TextView>
</LinearLayout>

src/MainActivity.java

package com.example.listviewdemo1;

import java.util.ArrayList;
import java.util.HashMap;

import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;
import android.app.Activity;
import android.graphics.Color;

public class MainActivity extends Activity {

	private ArrayList<HashMap<String,Object>> array = new ArrayList<HashMap<String,Object>>();	
	private LinearLayout layout;
	private ListView list;
	private TextView tv;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		
		for(int i=0; i< 20;i++){  
			HashMap<String,Object> map = new HashMap<String,Object>();
			map.put("ItemTitle", "数据项" + i);
			map.put("ItemContent", "数据项内容" + i + "...");
			array.add(map);
		}
				
		/*添加LinearLayout*/
		layout = new LinearLayout(this);
		layout.setOrientation(LinearLayout.VERTICAL);
		layout.setBackgroundColor(Color.WHITE);
				
		/*添加TextView*/
		tv = new TextView(this);
		LinearLayout.LayoutParams param1 = new LinearLayout.LayoutParams(
				LinearLayout.LayoutParams.MATCH_PARENT,
				LinearLayout.LayoutParams.WRAP_CONTENT
		);
		tv.setTextColor(getResources().getColor(R.drawable.blue));		
		layout.addView(tv, param1);
				
		/*添加ListView*/
		list = new ListView(this);
		LinearLayout.LayoutParams param2 = new LinearLayout.LayoutParams(
				LinearLayout.LayoutParams.MATCH_PARENT,
				LinearLayout.LayoutParams.WRAP_CONTENT
		);
		list.setBackgroundColor(getResources().getColor(R.drawable.ltgray));
		layout.addView(list, param2);
		setContentView(layout);
			
		SimpleAdapter simpleAdapter = new SimpleAdapter(this, array, R.layout.list, new String[]{"ItemTitle","ItemContent"}, new int[]{R.id.ItemTitle, R.id.ItemContent});
		list.setAdapter(simpleAdapter);
				
		list.setOnItemClickListener(
				new AdapterView.OnItemClickListener() {
					@Override
					public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {						
						HashMap<String,Object> map = array.get(arg2);
						tv.setText("点击了:" + arg2 + "行:" + map.get("ItemTitle"));
					}
				}
		);
	}
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值