Android ListView 控件学习

本文介绍了一个简单的 Android 应用示例,展示了如何使用 ListView 控件来显示来自 HashMap 的数据集合,并通过 SimpleAdapter 进行适配。同时提供了布局文件和样式定义。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >


1.创建Activity

package com.coolbi;

import java.util.ArrayList;
import java.util.HashMap;

import android.app.ListActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ListView;
import android.widget.SimpleAdapter;

public class ListViewTest extends ListActivity
{
	@Override
	public void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		
		ArrayList<HashMap<String,String>> list = new ArrayList<HashMap<String,String>>();
		HashMap<String,String> map1 = new HashMap<String,String>();
		HashMap<String,String> map2 = new HashMap<String,String>();
		HashMap<String,String> map3 = new HashMap<String,String>();
		map1.put("userName","张三");
		map1.put("userStatus","大学");
		map1.put("sex","男");
		
		map2.put("userName","李四");
		map2.put("userStatus", "研究生");
		map2.put("sex","女");
		
		map3.put("userName","至死不悟");
		map3.put("userStatus", "博士");
		map3.put("sex","男");
		
		list.add(map1);
		list.add(map2);
		list.add(map3);
		
		SimpleAdapter adapter = new SimpleAdapter(this,list,R.layout.listview,new String[]{"userName","userStatus","sex"},new int[]{R.id.textView1,R.id.textView2,R.id
				.textView3});
		setListAdapter(adapter);
		
	}

	@Override
	protected void onListItemClick(ListView l, View v, int position, long id)
	{
		super.onListItemClick(l, v, position, id);
		System.out.println("position="+position+",id="+id);
		//this.showDialog(0);
	}
	
	
}

 2.Main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
<TextView  
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:text="@string/hello"
    />
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
	android:orientation="horizontal"
	android:layout_width="wrap_content"
	android:layout_height="fill_parent"
	>
	<ListView
		android:id="@id/android:list"
		android:layout_width="fill_parent"
		android:layout_height="fill_parent"
		android:layout_marginRight="30dip"
		/> 
</LinearLayout>

</LinearLayout>

 3,显示ListView内容的样式文件

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
    
<TextView 
	android:id="@+id/textView1"
    android:layout_width="100dip" 
    android:layout_height="30dip" 
    android:paddingTop="5dip"
    android:layout_marginRight="30dip"
    />

<TextView  
	android:id="@+id/textView2"
    android:layout_width="100dip" 
    android:layout_height="30dip" 
    android:paddingTop="5dip"
    />
<TextView
	android:id="@+id/textView3"
	android:layout_width="100dip"
	android:layout_height="30dip" 
    android:paddingTop="5dip"
 	/>
</LinearLayout>
 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值