Android ListView控件基本用法

来源:互联网 发布:mysql 5.5 linux安装 编辑:程序博客网 时间:2024/05/16 09:10
  1. 下面的代码是mars老师视频教程里的代码,我把它注释了一下。
  2. 创建两个XML布局文件main.xml和user.xml。main.xml文件为系统自动创建
    main.xml布局文件代码:
    view plain
    1. <?xml version="1.0" encoding="utf-8"?>  
    2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
    3.     android:orientation="vertical"  
    4.     android:layout_width="fill_parent"  
    5.     android:layout_height="fill_parent" >  
    6.       
    7.     <LinearLayout android:id="@+id/listLinearLayout"   <!-- 设置LinearLayout的ID -->  
    8.                   android:layout_width="fill_parent"   <!-- 设置LinearLayout宽度为填满整个屏幕 -->  
    9.                   android:layout_height="wrap_content" <!-- 设置LinearLayout高度适应内部控件的高度 -->  
    10.                   android:orientation="vertical">       <!-- 设置LinearLayout的排列方式为纵向排列 -->  
    11.           
    12.         <!-- 在LinearLayout里嵌套一个ListView控件 -->  
    13.         <ListView android:id="@id/android:list"         <!-- 设置ListView控件的ID,这个ID为android系统内置ID -->  
    14.                   android:layout_width="fill_parent"    <!-- 设置ListView控件的宽度为填满整个屏幕 -->  
    15.                   android:layout_height="wrap_content"  <!-- 设置ListView控件的高度为自适应 -->  
    16.                   android:drawSelectorOnTop="false"     <!-- 设置ListView控件条目被按下时背景颜色在文字背后,设置成True时背景色会覆盖文字 -->  
    17.                   android:scrollbars="vertical"/>        <!-- 设置ListView控件滚动条的方向为纵向 -->  
    18.           
    19.     </LinearLayout>  
    20. </LinearLayout>  
     
    ListViw控件中的ID (android:id="@id/android:list") 是Android系统内置的ID,不能改为其他。
    android:drawSelectorOnTop="false" <!-- 当设置为false时条目被按下时背景颜色在文字背后,设置成True时背景色会覆盖文字 
    user.xml布局文件代码:
    view plain
    1. <?xml version="1.0" encoding="utf-8"?>  
    2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
    3.               android:orientation="horizontal"   
    4.               android:layout_width="fill_parent"   
    5.               android:layout_height="fill_parent">  
    6.                 
    7.     <TextView android:id="@+id/user_name"   
    8.               android:layout_width="180dip"   
    9.               android:layout_height="wrap_content"   
    10.               android:singleLine="true"   
    11.               android:textSize="10pt"  
    12.               android:paddingTop="2dip"  
    13.               android:paddingLeft="2dip"  
    14.                />  
    15.                 
    16.     <TextView android:id="@+id/user_ip"  
    17.               android:layout_width="180dip"  
    18.               android:layout_height="wrap_content"  
    19.               android:textSize="10pt"  
    20.               android:singleLine="true"   
    21.               android:paddingTop="2dip"  
    22.               android:paddingRight="2dip"  
    23.               />  
    24.                
    25. </LinearLayout>  
     
  3. 在Java源代码文件中写入如下代码:
    view plain
    1. package paj.ListView;  
    2. import java.util.ArrayList;  
    3. import java.util.HashMap;  
    4. import android.app.Activity;  
    5. import android.app.ListActivity;  
    6. import android.os.Bundle;  
    7. import android.view.View;  
    8. import android.widget.ListView;  
    9. import android.widget.SimpleAdapter;  
    10. public class ListViewMain extends ListActivity{  
    11.     /** Called when the activity is first created. */  
    12.     @Override  
    13.     public void onCreate(Bundle savedInstanceState) {  
    14.         super.onCreate(savedInstanceState);  
    15.         setContentView(R.layout.main);  
    16.           
    17.         //生成一个ArrayList类型的变量list  
    18.         ArrayList<HashMap<String, String>> list = new ArrayList<HashMap<String,String>>();  
    19.         //生成两个HashMap类型的变量map1 , map2  
    20.         //HashMpa为键值对类型。第一个参数为建,第二个参数为值  
    21.         HashMap<String, String> map1 = new HashMap<String, String>();  
    22.         HashMap<String, String> map2 = new HashMap<String, String>();  
    23.         //把数据填充到map1和map2中。  
    24.         map1.put("user_name""张三");  
    25.         map1.put("user_ip""192.168.1.52");  
    26.           
    27.         map2.put("user_name""李四");  
    28.         map2.put("user_ip""192.168.0.1");  
    29.         //把map1和map2添加到list中  
    30.         list.add(map1);  
    31.         list.add(map2);  
    32.         //生成一个SimpleAdapter类型的变量来填充数据  
    33.         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});  
    34.         //设置显示ListView  
    35.         setListAdapter(listAdapter);  
    36.           
    37.     }  
    38.     //重写onListItemClick但是ListView条目事件  
    39.     @Override  
    40.     protected void onListItemClick(ListView l, View v, int position, long id) {  
    41.         // TODO Auto-generated method stub  
    42.         super.onListItemClick(l, v, position, id);  
    43.         //显示单击条目ID号  
    44.         System.out.println("id = " + id);  
    45.         //显示所单击条目的位置数  
    46.         System.out.println("position = " + position);  
    47.     }  
    48.       
    49.       
    50.       
    51. }  
      

    //生成SimpleAdapter对象参数的解释
    view plain
    1. SimpleAdapter listAdapter = new SimpleAdapter(  
    2.                 this        //this是当前Activity的对象  
    3.                 , list      //list为填充数据后的ArrayList类型的list对象  
    4.                 , R.layout.user     //这个就是之前创建的第二个布局文件user.xml的id。  
    5.                 , new String[]{"user_name" , "user_ip"}     //这个String数组中的元素就是list对象中的列,list中有几这个数组中就要写几列。  
    6.                                                             //其中的元素必须是list中列的名。  
    7.                 , new int[]{R.id.user_name , R.id.user_ip}  //这个int型数组中的元素对应着上上一个参数String类型数组中的列名。  
    8.                                                             //它的值是对应user.xml布局文件中的TextView的id     
    9.         );  
      
原创粉丝点击