Android中ListView的使用

网上有一种方法是使用ListActivity,但是经过使用之后操作性没有Activity大,所以建议还是使用正常的Activity类,在Layout中添加ListView控件

首先在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="match_parent"
    android:orientation="vertical" >

    <ListView
        android:id="@+id/building_information_list"
        android:layout_width="match_parent"
        android:layout_height="match_parent" >
    </ListView>
    
</LinearLayout>
ListView要设置ID,起用法同普通控件一样,用findViewById获取之后使用

Activity中:

protected void onCreate(Bundle savedInstanceState) {
	    super.onCreate(savedInstanceState);
	    setContentView(R.layout.building_information);
	    ListView listView = (ListView)findViewById(R.id.building_information_list);           
	    SimpleAdapter adapter = new SimpleAdapter(this, getData(),
	                R.layout.building_information_subitem, new String[] { "building_information_title", "building_information_info" },
	                new int[] { R.id.building_information_title, R.id.building_information_info });
	    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("building_information_info","this is Test1");
        map.put("building_information_title","Test1");
        list.add(map);
        
        map = new HashMap<String,Object>();
        map.put("building_information_info","this is Test2");
        map.put("building_information_title","Test2");
        list.add(map);
        
        for(int i=3;i<=20;i++){
        	map = new HashMap<String,Object>();
        	map.put("building_information_info","this is Test"+i);
        	map.put("building_information_title","Test"+i);
        	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="vertical" >
    
    <TextView 
            android:id="@+id/building_information_title"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:textSize="20sp"
            />
        <TextView
            android:id="@+id/building_information_info"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:textSize="15sp"
            />
</LinearLayout>


SimpleAdapter是适配器,其中参数的含义为:

SimpleAdapter(Context context, List<? extends Map<String, ?>> data, int resource, String[] from, int[] to)
参数context:上下文,比如this。关联SimpleAdapter运行的视图上下文
参数data:Map列表,列表要显示的数据,这部分需要自己实现,如例子中的getData(),类型要与上面的一致,每条项目要与from中指定条目一致
参数resource:ListView单项布局文件的Id,这个布局就是你自定义的布局了,你想显示什么样子的布局都在这个布局中。这个布局中必须包括了to中定义的控件id
参数 from:一个被添加到Map上关联每一个项目列名称的列表,数组里面是列名称
参数 to:是一个int数组,数组里面的id是自定义布局中各个控件的id,需要与上面的from对应

较之其他适配器,SimpleAdapter功能比较丰富,可以添加图片等结构


基本用法即是如此;

如果要增加固定的头部,只需要在xml文件中<ListView>之前引用header.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="match_parent"
    android:orientation="vertical" >
   
    <include layout = "@layout/building_information_header"/>

    <ListView
        android:id="@+id/building_information_list"
        android:layout_width="match_parent"
        android:layout_height="match_parent" >
    </ListView>
</LinearLayout>
固定的头部中,要注意根节点的layout_height要设置为wrap_content否则会导致下面的ListView无法显示

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        <span style="color:#ff6666;">android:layout_width="wrap_content"
        android:layout_height="wrap_content" ></span>
        <Button
            android:id="@+id/button2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentRight="true"
            android:layout_alignParentTop="true"
            android:text="Button" />
        <Spinner
            android:id="@+id/spinner1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_alignParentTop="true" />
</RelativeLayout>
如果要设置可滑动的头部,需要在java文件中使用listView.addHeaderView(R.layout);

注意这个方法要在设置适配器之前使用





  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值