不难理解的一段代码,因为自己粗心在for循环里少写了一句把item添加进去导致运行出来没有效果,好在还是弄出来了,能安心的睡觉了,明儿礼拜五,坚持一天,可以小小休息一下了,这天天上班还好,坐车坐的好累。。
代码如下:
public class MainActivity extends Activity {
private String[] name1 = { "张三", "李四", "王五", "赵六" };
private String[] name2 = { "热情的人", "闷骚的人", "活泼的人", "开朗的人" };
private int[] images = { R.drawable.p1, R.drawable.p2, R.drawable.p3,
R.drawable.p4 };
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ListView list = (ListView) findViewById(R.id.mylist);
List<Map<String, Object>> items = new ArrayList<Map<String, Object>>();
for (int i = 0; i < name1.length; i++) {
Map<String, Object> item = new HashMap<String, Object>();
item.put("img", images[i]);
item.put("n1", name1[i]);
item.put("n2", name2[i]);
items.add(item);
}
SimpleAdapter simpleAdapter = new SimpleAdapter(this, items,
R.layout.simple_item, new String[] { "n1", "n2", "img" },
new int[] { R.id.name1, R.id.name2, R.id.header });
list.setAdapter(simpleAdapter);
}
}
main的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="wrap_content"
android:orientation="horizontal" >
<ListView
android:id="@+id/mylist"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
</ListView>
</LinearLayout>
simple_item的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="wrap_content"
android:orientation="horizontal" >
<ImageView
android:id="@+id/header"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="10dp" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<TextView
android:id="@+id/name1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="10dp"
android:textColor="#ff00"
android:textSize="20dp" />
<TextView
android:id="@+id/name2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="10dp"
android:textSize="15dp" />
</LinearLayout>
</LinearLayout>
效果如下: