ListView

设计一个ListView,放置中国各省的省会城市,并为每一个省会城市安放对应图片。

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 MainActivity extends Activity {
	ListView listView;
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		listView=(ListView)findViewById(R.id.lv);
		SimpleAdapter adapter=new SimpleAdapter(this, getData(), R.layout.second_activity, new String[]{"tet","img"},new int[]{R.id.tet,R.id.img});
		listView.setAdapter(adapter);	
	}
	private List<Map<String,Object>> getData(){
		List<Map<String, Object>> list=new ArrayList<Map<String,Object>>();
		Map<String, Object> map=new HashMap<String, Object>();
		map.put("tet", "上海");
		map.put("img", R.drawable.shanghai);
		list.add(map);
		
		map=new HashMap<String, Object>();
		map.put("tet", "澳门");
		map.put("img", R.drawable.aomen);
		list.add(map);
		
		map=new HashMap<String, Object>();
		map.put("tet", "福州");
		map.put("img", R.drawable.fuzhou);
		list.add(map);
		
		map=new HashMap<String, Object>();
		map.put("tet", "深圳");
		map.put("img", R.drawable.shenzhen);
		list.add(map);
		
		map=new HashMap<String, Object>();
		map.put("tet", "温州");
		map.put("img", R.drawable.wenzhou);
		list.add(map);
		
		map=new HashMap<String, Object>();
		map.put("tet", "厦门");
		map.put("img", R.drawable.xiamen);
		list.add(map);
		return list;
	}
}

布局:

<?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/img"
        android:layout_width="30px"
        android:layout_height="30px"
        android:layout_marginLeft="5px" />

    <TextView
        android:id="@+id/tet"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="15px" />

</LinearLayout>


posted @ 2017-05-10 11:06  yfceshi  阅读(137)  评论(0编辑  收藏  举报