ListView 的使用 —— Android 发现之旅 2011/03/28

这里同样通过一个例子来认识ListView 的使用。

首先在 main.xml 布局文件中定义一个 <ListView></ListView>

具体代码为:

 

<? 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  xmlns:android ="http://schemas.android.com/apk/res/android"
        android:id
="@+id/listLinearLayout"
        android:orientation
="vertical"
        android:layout_width
="fill_parent"
        android:layout_height
="wrap_content"   >
        
< ListView 
            
android:id ="@id/android:list"
            android:layout_width
="fill_parent"
            android:layout_height
="wrap_content"
            android:drawSelectorOnTop
="false"
            android:scrollbars
="vertical" >
                
        
</ ListView >
    
</ LinearLayout >
</ LinearLayout >

 

android:id ="@id/android:list"   这个是ListView的特定的id。 不能改哦。

 

 

android:drawSelectorOnTop ="false"  这个属性表示 是否把第一行选中,覆盖。  具体可以通过后面的 显示结果来发现它的作用。

然后再新建一个 布局文件  listview.xml  来布局 一个list 里面显示的东西。  这里呢, 我添加两个 TextView。

 

 

<? 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"
    android:paddingLeft
="10dip"
    android:paddingRight
="10dip"
    
>
    
    
< TextView
        
android:id ="@+id/list_UserName"  
        android:layout_width
="120dip"
        android:layout_height
="35dip"
        android:textSize
="11pt"
        android:singleLine
="true" />
    
    
< TextView 
        
android:id ="@+id/list_UserIP"  
        android:layout_width
="fill_parent"
        android:layout_height
="fill_parent"
        android:gravity
="right"
        android:textSize
="10pt"
        android:singleLine
="true" />

    
</ LinearLayout >

 

 这个就不多解释了。

然后看看代码部分:

 

package  com.android.ListViewDemo;

import  java.util. * ;
import  android.app.ListActivity;
import  android.os.Bundle;
import  android.view.View;
import  android.widget.ListView;
import  android.widget.SimpleAdapter;

public   class  ListViewDemo  extends  ListActivity {
    
/**  Called when the activity is first created.  */
    @Override
    
public   void  onCreate(Bundle savedInstanceState) {
        
super .onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        ArrayList
< HashMap < String, String >>  list  =   new  ArrayList < HashMap < String,String >> ();
        HashMap
< String, String >  map1  =   new  HashMap < String, String > ();
        HashMap
< String, String >  map2  =   new  HashMap < String, String > ();
        HashMap
< String, String >  map3  =   new  HashMap < String, String > ();
        
        map1.put(
" User_Name " " Jammse " );
        map1.put(
" User_IP " " 172.28.3.48 " );        
        map2.put(
" User_Name " " Anmina " );
        map2.put(
" User_IP " " 172.28.3.47 " );        
        map3.put(
" User_Name " " Joson " );
        map3.put(
" User_IP " " 172.28.3.46 " );
        
        list.add(map1);
        list.add(map2);
        list.add(map3);
        
        SimpleAdapter listAdapter 
=   new  SimpleAdapter( this , list, R.layout.listview, 
                
new  String[] { " User_Name " " User_IP " }, 
                
new   int [] {R.id.list_UserName, R.id.list_UserIP});
        setListAdapter(listAdapter);
    }

    @Override
    
protected   void  onListItemClick(ListView l, View v,  int  position,  long  id) {
        
//  TODO Auto-generated method stub
         super .onListItemClick(l, v, position, id);
        
        System.out.println(
" Id -----------  "   +  id);
        System.out.println(
" Postion -----------  "   +  position);        
    }
    
    
}

 

public  class  ListViewDemo  extends  ListActivity {}  这里的Activity 继承的是 ListActivity 类。 不是Activity。
ListActivity是 Activity的一个子类。

 这里通过 ArrayList  和 HashMap 来把要显示的内容 绑定到 ListView 中。

 关键代码 就是这个:

SimpleAdapter listAdapter  =  new  SimpleAdapter( this , list, R.layout.listview, 
                
new  String[] { " User_Name " " User_IP " }, 
                
new  int [] {R.id.list_UserName, R.id.list_UserIP});
setListAdapter(listAdapter);

 

运行结果:


 

 

 上面说了 这样的属性: 

 

android:drawSelectorOnTop="false"  如果将其改为 true。  运行结果将会是如下:

 


 

以上就是 ListView 的一个小的演示。 记录于 2011-03-29 凌晨。

 

 

转载于:https://www.cnblogs.com/jamsewang/archive/2011/03/29/1998269.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值