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(安卓)- Activity - 启动模式
  4. 学习Android(安卓)必备 实例大集合
  5. 《Android/OPhone 开发完全讲义》已出版,现提供源代码下载
  6. Android(安卓)Activity四种加载方式
  7. Android开发9——Activity的启动模式
  8. android 动态控制状态栏显示和隐藏的方法实例
  9. Android(安卓)Activity四种加载方式

随机推荐

  1. Android日记之2012/02/04——ActivityGro
  2. android 获取视频第一帧作为缩略图
  3. android 拍照上传照片
  4. 浅析Android中ndk-build支持的参数
  5. android通过webservice验证用户 .
  6. Android native code的编译和调试【转htt
  7. android 中文 api (72) —— BluetoothSock
  8. Android Studio 2.3 正式起航
  9. 嗯,农民伯伯的2014就是这些
  10. NSF方式挂载Android分区