android开发之ListView实现

本文详细介绍了ListView控件的基本使用方法,包括源码解析、布局文件设计、数据适配及点击事件处理,通过实例展示了如何创建一个包含系统信息、硬件信息、软件信息等模块的ListView应用。

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

今天又初步学习了一下ListView控件,看看效果如下:

LisViewActivity.java源码:

package com.jinhoward.UI_listview;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


import android.os.Bundle;
import android.app.Activity;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.Toast;

public class LisViewActivity extends Activity {
	ListView listView=null;
	Toast toast;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_lis_view);
		
		listView =(ListView)findViewById(R.id.infolistview);
		
		//生成动态数组,加入数据 ,添加的信息有string,也有int类型(图片id),所以用Map<String, Object>
		List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
		
		//一个map相当于listview的一行.
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("name", "系统信息");
		map.put("notes", "系统版本,运营商及其系统信息.");
		map.put("img", R.drawable.system);//图片资源的id
		list.add(map);

		map = new HashMap<String, Object>();
		map.put("name", "硬件信息");
		map.put("notes", "CPU,硬盘,内存等硬件信息.");
		map.put("img", R.drawable.hardware);
		list.add(map);

		map = new HashMap<String, Object>();
		map.put("name", "软件信息");
		map.put("notes", "已经安装的软件信息.");
		map.put("img", R.drawable.software);
		list.add(map);

		map = new HashMap<String, Object>();
		map.put("name", "运行时信息");
		map.put("notes", "运行时的信息.");
		map.put("img", R.drawable.running);
		list.add(map);
		
		map = new HashMap<String, Object>();
		map.put("name", "文件浏览器");
		map.put("notes", "文件系统.");
		map.put("img", R.drawable.file_explorer);
		list.add(map);
		
	    //生成适配器的Item和动态数组对应的元素 	
		SimpleAdapter mySimpleAdapter = new SimpleAdapter(this, list, R.layout.item_row,
				new String[] { "name", "notes", "img" }, new int[] { R.id.name,
						R.id.notes, R.id.img });
	    //设置适配器 
		listView.setAdapter(mySimpleAdapter);
		
		//设置监听器
		listView.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
					long arg3) {
				// TODO Auto-generated method stub
				Toast.makeText(LisViewActivity.this, "您选择了第"+(arg2+1)+"项", Toast.LENGTH_SHORT).show();
			}
		});

	}
}

activity_lis_view.xml源码:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/LinearLayout1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:background="#D1EEEE"
 >

	<ListView 
	    android:layout_width="fill_parent"
		android:layout_height="fill_parent" 
		android:id="@+id/infolistview" />

</LinearLayout>

item_row.xml源码:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/vw1"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"

    android:orientation="horizontal">    
 
    <ImageView android:id="@+id/img"
        android:layout_width="24dp"
        android:layout_margin="4dp"
        android:layout_height="24dp"/>
 
   <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical">

        <TextView android:id="@+id/name"
            android:textSize="18sp"
            android:textStyle="bold"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"/>

        <TextView android:id="@+id/notes"
            android:textSize="12sp"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"/>

    </LinearLayout>

</LinearLayout>


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值