Layout1.5

ListView图文列表:  item模板

<?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" 
    android:gravity="center_horizontal">

    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/abc_ic_go_search_api_holo_light" />

    <TextView
        android:id="@+id/textView1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="TextView" />

</LinearLayout>

使用SimpleAdapter:

package com.example.android_listview_activity;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.app.Activity;
import android.os.Bundle;
import android.widget.ListView;
import android.widget.SimpleAdapter;

public class MainActivity4 extends Activity{
	
	private ListView listView;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		
		listView = (ListView)this.findViewById(R.id.listView1);
		
		Map<String,Object> item1 = new HashMap<String,Object>();
		item1.put("pic", R.drawable.abc_ic_go_search_api_holo_light);
		item1.put("name", "北京");
		
		Map<String,Object> item2 = new HashMap<String,Object>();
		item2.put("pic", R.drawable.abc_ic_go_search_api_holo_light);
		item2.put("name", "上海");
		
		Map<String,Object> item3 = new HashMap<String,Object>();
		item3.put("pic", R.drawable.abc_ic_go_search_api_holo_light);
		item3.put("name", "成都");
		
		Map<String,Object> item4 = new HashMap<String,Object>();
		item4.put("pic", R.drawable.abc_ic_go_search_api_holo_light);
		item4.put("name", "深圳");
		
		List<Map<String,Object>> data = new ArrayList<Map<String,Object>>();
		data.add(item1);
		data.add(item2);
		data.add(item3);
		data.add(item4);
		
		// from和to一一对应  绑定组件
		String[] from = {"pic","name"};
		
		int[] to = {R.id.imageView1,R.id.textView1};
		
		SimpleAdapter simpleAdapter = new SimpleAdapter(this, data, R.layout.source, from, to);
		
		listView.setAdapter(simpleAdapter);
	}
	
}

猜你喜欢

转载自luan.iteye.com/blog/2109461
1.5
今日推荐