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">

<!-- 用户名文本 -->
<TextView android:id="@+id/userNameId" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:textSize="10pt"
android:gravity="left" />

<!-- IP文本 -->
<TextView android:id="@+id/ipId" android:layout_width="fill_parent"
android:layout_height="wrap_content" android:textSize="10pt"
android:gravity="right" />

</LinearLayout>

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">

<LinearLayout android:layout_width="fill_parent"
android:layout_height="wrap_content" android:orientation="vertical"
android:paddingLeft="10px" android:paddingRight="10px"
android:paddingTop="10px" android:paddingBottom="10px">

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

</LinearLayout>

</LinearLayout>

ListViewActivity.java

package com.duoguo.android;

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;

/**
* ListView的使用
*
* @author shyboy(
897948924@qq.com)
*
*/
public class ListViewActivity 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>>();// 实例化ArrayList链表,用来存放HashMap对象

// 实例化HashMap对象,用来存放用户名和IP
HashMap<String, String> map1 = new HashMap<String, String>();
map1.put("user_name", "shyboy");
map1.put("user_ip", "192.168.0.1");
HashMap<String, String> map2 = new HashMap<String, String>();
map2.put("user_name", "playboy");
map2.put("user_ip", "192.168.0.2");
HashMap<String, String> map3 = new HashMap<String, String>();
map3.put("user_name", "simplyboy");
map3.put("user_ip", "192.168.0.3");

// 添加HashMap集合到ArrayList链表中
list.add(map1);
list.add(map2);
list.add(map3);

SimpleAdapter listSimpleAdapter = new SimpleAdapter(this, list,
R.layout.user, new String[] { "user_name", "user_ip" },
new int[] { R.id.userNameId, R.id.ipId });// 实例化适配器
this.setListAdapter(listSimpleAdapter);

}

// 当点击ListView内容时触发该事件
@Override
protected void onListItemClick(ListView l, View v, int position, long id) {

super.onListItemClick(l, v, position, id);
System.out.println("id is:" + id);
System.out.println("position is:" + position);

}

}

希望对大家有所帮助,呵呵……

更多相关文章

  1. react-native开发实例之替换默认logo——android实现
  2. 实例教程四:采用Pull解析器解析和生成XML内容
  3. android 动态控制状态栏显示和隐藏的方法实例
  4. Android自动完成文本框
  5. Android异步更新UI-线程池-Future-Handler实例分析
  6. [Android实例] 【Kris专题】android 换肤

随机推荐

  1. Delphi XE5 for Android(安卓)(七)
  2. Android智能手机搜索不到路由器无线信号
  3. 关于android的广播接收器(1)—基础篇
  4. Android的消息机制之Handler源码解析
  5. mac 系统下android源码下载以及使用(总结)
  6. Android NDK开发学习(一)
  7. Android通过adb查看wifi密码
  8. Android应用程序内部启动Activity过程(sta
  9. Google 推出 Android Design 網站,教您如
  10. 手机或android设备连接电脑,但是adb devic