Android学习笔记06:ListView的使用

本文通过一个具体实例展示了如何在Android应用中使用ListView控件。该示例包括定义布局文件、创建适配器以及设置ListView的数据源等步骤。通过此教程,读者可以了解如何将数据集合显示为列表,并掌握SimpleAdapter的基本用法。

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

[Android开发视频教学].01_13_Android常见控件(三)之二 学习使用ListView

list.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <ListView
        android:id="@id/android:list"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:drawSelectorOnTop="false"
        android:scrollbars="vertical" >
    </ListView>

</LinearLayout>

user.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal" >

    <TextView
        android:id="@+id/user_ip"
        android:layout_width="180dp"
        android:layout_height="20dp"
        android:gravity="left" />

    <TextView
        android:id="@+id/user_name"
        android:layout_width="fill_parent"
        android:gravity="right"
        android:layout_height="20dp" />

</LinearLayout>

java

package fengda.android09;

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 Activity09 extends ListActivity {


	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_activity09);

		ArrayList<HashMap<String, String>> mapList = 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("usr_ip", "192.168.0.1");
		map1.put("user_name", "一号");
		map2.put("user_ip", "192.168.0.2");
		map2.put("user_name", "二号");
		map3.put("user_ip", "192.168.0.3");
		map3.put("user_name", "三号");

		mapList.add(map1);
		mapList.add(map2);
		mapList.add(map3);

		SimpleAdapter ad = new SimpleAdapter(this, mapList, R.layout.user,
				new String[] { "user_ip", "user_name" }, new int[] {
						R.id.user_ip, R.id.user_name });
		this.setListAdapter(ad);
	}

	@Override
	protected void onListItemClick(ListView l, View v, int position, long id) {
		// TODO Auto-generated method stub
		super.onListItemClick(l, v, position, id);
	}

	
}

为什么我做出来的结果是这样的?




评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值