主要代码
package com.example.test;
import android.app.Activity;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class ListSimple extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_list_array);
ListView list= (ListView) findViewById(R.id.list);
List<Map<String, Object>> maps=new ArrayList<Map<String, Object>>();
Map<String,Object> map=new HashMap<String,Object>();
map.put("name","顾哥");
map.put("photo", R.drawable.hot1);
maps.add(map);
Map<String,Object> map2=new HashMap<String,Object>();
map2.put("name","张哥");
map2.put("photo", R.drawable.hot2);
maps.add(map2);
Map<String,Object> map3=new HashMap<String,Object>();
map3.put("name","李哥");
map3.put("photo", R.drawable.hot3);
maps.add(map3);
list.setAdapter(new SimpleAdapter(
this,
maps,
R.layout.activity_list_data_simple,
new String[]{"name","photo"},
new int[]{R.id.tv_name,R.id.iv_photo}));
}
}
布局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"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.test.ListArray">
<ListView
android:id="@+id/list"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
</RelativeLayout>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:id="@+id/tv_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true" />
<ImageView
android:id="@+id/iv_photo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:src="@drawable/hot1" />
</RelativeLayout>