listview使用SimpleAdapter显示文字

本文通过一个具体的例子展示了如何在Android应用中使用ListView组件显示用户列表。包括如何创建适配器、填充数据以及处理列表项点击事件。

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

package com.example.listview_4;

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

import android.app.Activity;
import android.app.ListActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.Toast;

public class MainActivity extends ListActivity {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_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("userAddr", "合肥市");
		map2.put("userName", "李四");
		map2.put("userAddr", "肥西县");
		map3.put("userName", "王五");
		map3.put("userAddr", "肥东县");

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

		SimpleAdapter listAdapter = new SimpleAdapter(this, list,
				R.layout.user, new String[] { "userName", "userAddr" },
				new int[] { R.id.textUsrName, R.id.textUsrAddr });
		setListAdapter(listAdapter);

	}// onCreate

	@Override
	protected void onListItemClick(ListView l, View v, int position, long id) {
		// TODO 自动生成的方法存根
		super.onListItemClick(l, v, position, id);
    ///id和position都是从0开始计数
		System.out.println("id--------------" + (id + 1));
		System.out.println("position--------------" + position);
		Toast.makeText(getApplicationContext(), "你点击了第" + (id + 1) + "个单位",
				Toast.LENGTH_SHORT).show();

	}
}

------------------

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        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" />
    </LinearLayout>

</LinearLayout>
------------------------------

user.xml

<?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"
    android:padding="10dip"
    >
    <TextView
        android:id="@+id/textUsrName"
        android:layout_width="180dip"
        android:layout_height="30dip"
        android:text="@string/user_name"
        android:textSize="10pt"
        android:singleLine="true"
        android:gravity="left"
        
    />
    <TextView
        android:id="@+id/textUsrAddr"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:text="@string/user_addr"
        android:textSize="10pt"
        android:gravity="right"
        
    />
</LinearLayout>
------------

strings.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>

    <string name="app_name">ListView_4</string>
    <string name="hello_world">Hello world!</string>
    <string name="user_name">用户名</string>
    <string name="user_addr">ip地址</string>

</resources>




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

二十六画生的博客

你的鼓励是我创作最大的动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值