SimpleAdapter

来源:互联网 发布:qq2012国际版 linux 编辑:程序博客网 时间:2024/05/18 11:16

activity_main.xml

<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=".MainActivity" >    <ListView        android:id="@+id/main_lv"        android:layout_width="match_parent"        android:layout_height="match_parent" /></LinearLayout>

list_view.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="match_parent"    android:orientation="horizontal" >    <ImageView        android:id="@+id/tx_img"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_marginRight="25dp"        android:src="@drawable/btn_plus_disable" />    <TextView        android:id="@+id/tx_name"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_marginRight="25dp"        android:text="我是名字"        android:textSize="20dip" />    <TextView        android:id="@+id/tx_address"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="我是地址"        android:textSize="20dip" /></LinearLayout>

MainActivity.java

package com.demo.simpleadapter;import java.util.ArrayList;import java.util.HashMap;import java.util.List;import java.util.Map;import android.os.Bundle;import android.app.Activity;import android.view.Menu;import android.widget.ListView;import android.widget.SimpleAdapter;public class MainActivity extends Activity {private ListView listView;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);listView = (ListView) findViewById(R.id.main_lv);List<Map<String, Object>> data = new ArrayList<Map<String, Object>>();Map<String, Object> map1 = new HashMap<String, Object>();map1.put("name", "杨幂");map1.put("address", "北京");map1.put("photo", R.drawable.btn_plus_disable);Map<String, Object> map2 = new HashMap<String, Object>();map2.put("name", "刘亦菲");map2.put("address", "北京");map2.put("photo", R.drawable.btn_radio_off_pressed);Map<String, Object> map3 = new HashMap<String, Object>();map3.put("name", "景甜");map3.put("address", "北京");map3.put("photo", R.drawable.btn_rating_star_off_pressed_holo_light);data.add(map1);data.add(map2);data.add(map3);listView.setAdapter(new SimpleAdapter(this, data, R.layout.list_view,new String[]{"photo","name","address"},new int[]{R.id.tx_img,R.id.tx_name,R.id.tx_address} ));}}

效果图:


0 0
原创粉丝点击