0
点赞
收藏
分享

微信扫一扫

Android ListView控件基本用法



  1. 下面的代码是mars老师视频教程里的代码,我把它注释了一下。
  2. 创建两个XML布局文件main.xml和user.xml。main.xml文件为系统自动创建

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>


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>


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



}


生成SimpleAdapter对象参数的解释

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 条评论