首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 移动开发 > Android >

Android中ListView的运用

2012-08-27 
Android中ListView的使用user.xml?xml version1.0 encodingutf-8?LinearLayout xmlns:androidh

Android中ListView的使用

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);

?}

}

?

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

热点排行