码迷,mamicode.com
首页 > 其他好文 > 详细

ListView

时间:2014-05-09 09:04:00      阅读:376      评论:0      收藏:0      [点我收藏+]

标签:android   style   blog   class   code   java   

设计一个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>

bubuko.com,布布扣

ListView,布布扣,bubuko.com

ListView

标签:android   style   blog   class   code   java   

原文地址:http://blog.csdn.net/crescent__moon/article/details/25362563

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!