Android ListView控件基本用法

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:id ="@+id/listLinearLayout" <!-- 设置LinearLayout的ID -- >
android:layout_width="fill_parent" <!-- 设置LinearLayout宽度为填满整个屏幕 -->
android:layout_height="wrap_content" <!-- 设置LinearLayout高度适应内部控件的高度 -->
android:orientation="vertical"> <!-- 设置LinearLayout的排列方式为纵向排列 -->

<!-- 在LinearLayout里嵌套一个ListView控件 -->
< ListView android:id ="@id/android:list" <!-- 设置ListView控件的ID,这个ID为android系统内置ID -- >
android:layout_width="fill_parent" <!-- 设置ListView控件的宽度为填满整个屏幕 -->
android:layout_height="wrap_content" <!-- 设置ListView控件的高度为自适应 -->
android:drawSelectorOnTop="false" <!-- 设置ListView控件条目被按下时背景颜色在文字背后,设置成True时背景色会覆盖文字 -->
android:scrollbars="vertical"/> <!-- 设置ListView控件滚动条的方向为纵向 -->

</ LinearLayout >
</ LinearLayout >

<? 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:id ="@+id/listLinearLayout" <!-- 设置LinearLayout的ID -- >
android:layout_width="fill_parent" <!-- 设置LinearLayout宽度为填满整个屏幕 -->
android:layout_height="wrap_content" <!-- 设置LinearLayout高度适应内部控件的高度 -->
android:orientation="vertical"> <!-- 设置LinearLayout的排列方式为纵向排列 -->

<!-- 在LinearLayout里嵌套一个ListView控件 -->
< ListView android:id ="@id/android:list" <!-- 设置ListView控件的ID,这个ID为android系统内置ID -- >
android:layout_width="fill_parent" <!-- 设置ListView控件的宽度为填满整个屏幕 -->
android:layout_height="wrap_content" <!-- 设置ListView控件的高度为自适应 -->
android:drawSelectorOnTop="false" <!-- 设置ListView控件条目被按下时背景颜色在文字背后,设置成True时背景色会覆盖文字 -->
android:scrollbars="vertical"/> <!-- 设置ListView控件滚动条的方向为纵向 -->

</ LinearLayout >
</ LinearLayout > 
复制代码

ListViw控件中的ID (android:id="@id/android:list") 是Android系统内置的ID,不能改为其他。android:drawSelectorOnTop="false" <!-- 当设置为false时条目被按下时背景颜色在文字背后,
设置成True时背景色会覆盖文字 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/user_name"
android:layout_width
="180dip"
android:layout_height
="wrap_content"
android:singleLine
="true"
android:textSize
="10pt"
android:paddingTop
="2dip"
android:paddingLeft
="2dip"
/>

< TextView android:id ="@+id/user_ip"
android:layout_width
="180dip"
android:layout_height
="wrap_content"
android:textSize
="10pt"
android:singleLine
="true"
android:paddingTop
="2dip"
android:paddingRight
="2dip"
/>
</ LinearLayout >

<? 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/user_name"
android:layout_width
="180dip"
android:layout_height
="wrap_content"
android:singleLine
="true"
android:textSize
="10pt"
android:paddingTop
="2dip"
android:paddingLeft
="2dip"
/>

< TextView android:id ="@+id/user_ip"
android:layout_width
="180dip"
android:layout_height
="wrap_content"
android:textSize
="10pt"
android:singleLine
="true"
android:paddingTop
="2dip"
android:paddingRight
="2dip"
/>

</ LinearLayout >
复制代码

3.在Java源代码文件中写入如下代码:

复制代码
package paj.ListView;
import java.util.ArrayList;
import java.util.HashMap;
import android.app.Activity;
import android.app.ListActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ListView;
import android.widget.SimpleAdapter;
public class ListViewMain extends ListActivity{
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

// 生成一个ArrayList类型的变量list
ArrayList<HashMap<String, String>> list = new ArrayList<HashMap<String,String>>();
// 生成两个HashMap类型的变量map1 , map2
// HashMpa为键值对类型。第一个参数为建,第二个参数为值
HashMap<String, String> map1 = new HashMap<String, String>();
HashMap<String, String> map2 = new HashMap<String, String>();
// 把数据填充到map1和map2中。
map1.put("user_name", "张三");
map1.put("user_ip", "192.168.1.52");

map2.put("user_name", "李四");
map2.put("user_ip", "192.168.0.1");
// 把map1和map2添加到list中
list.add(map1);
list.add(map2);
// 生成一个SimpleAdapter类型的变量来填充数据
SimpleAdapter listAdapter = new SimpleAdapter( this, list, R.layout.user, new String[]{"user_name" , "user_ip"}, new int[]{R.id.user_name , R.id.user_ip});
// 设置显示ListView
setListAdapter(listAdapter);

}
// 重写onListItemClick但是ListView条目事件
@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
// TODO Auto-generated method stub
super.onListItemClick(l, v, position, id);
// 显示单击条目ID号
System.out.println("id = "+ id);
// 显示所单击条目的位置数
System.out.println("position = "+ position);
}



}
package paj.ListView;
import java.util.ArrayList;
import java.util.HashMap;
import android.app.Activity;
import android.app.ListActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ListView;
import android.widget.SimpleAdapter;
public class ListViewMain extends ListActivity{
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

// 生成一个ArrayList类型的变量list
ArrayList<HashMap<String, String>> list = new ArrayList<HashMap<String,String>>();
// 生成两个HashMap类型的变量map1 , map2
// HashMpa为键值对类型。第一个参数为建,第二个参数为值
HashMap<String, String> map1 = new HashMap<String, String>();
HashMap<String, String> map2 = new HashMap<String, String>();
// 把数据填充到map1和map2中。
map1.put("user_name", "张三");
map1.put("user_ip", "192.168.1.52");

map2.put("user_name", "李四");
map2.put("user_ip", "192.168.0.1");
// 把map1和map2添加到list中
list.add(map1);
list.add(map2);
// 生成一个SimpleAdapter类型的变量来填充数据
SimpleAdapter listAdapter = new SimpleAdapter( this, list, R.layout.user, new String[]{"user_name" , "user_ip"}, new int[]{R.id.user_name , R.id.user_ip});
// 设置显示ListView
setListAdapter(listAdapter);

}
// 重写onListItemClick但是ListView条目事件
@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
// TODO Auto-generated method stub
super.onListItemClick(l, v, position, id);
// 显示单击条目ID号
System.out.println("id = "+ id);
// 显示所单击条目的位置数
System.out.println("position = "+ position);
}



}
// 生成SimpleAdapter对象参数的解释
view plaincopy to clipboardprint?
SimpleAdapter listAdapter = new SimpleAdapter(
this // this是当前Activity的对象
, list // list为填充数据后的ArrayList类型的list对象
, R.layout.user // 这个就是之前创建的第二个布局文件user.xml的id。
, new String[]{"user_name" , "user_ip"} // 这个String数组中的元素就是list对象中的列,list中有几这个数组中就要写几列。
// 其中的元素必须是list中列的名。
, new int[]{R.id.user_name , R.id.user_ip} // 这个int型数组中的元素对应着上上一个参数String类型数组中的列名。
// 它的值是对应user.xml布局文件中的TextView的id
); 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值