Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> 關於Android編程 >> android ListView用法

android ListView用法

編輯:關於Android編程

print?<?xml version="1.0" encoding="utf-8"?>  
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
    android:layout_width="fill_parent"  
    android:layout_height="fill_parent"  
    android:orientation="vertical" >  
  
    <LinearLayout  
        android:id="@+id/listLinearlayout"  
        android:layout_width="fill_parent"  
        android:layout_height="wrap_content"  
        android:orientation="vertical" >  
          
        <ListView   
            android:id="@id/android:list"  
            android:layout_width="fill_parent"  
            android:layout_height="wrap_content"  
            >  
        </ListView>  
          
    </LinearLayout>  
      
</LinearLayout>  

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <LinearLayout
        android:id="@+id/listLinearlayout"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical" >
        
        <ListView 
            android:id="@id/android:list"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            >
        </ListView>
        
    </LinearLayout>
    
</LinearLayout>

      user.xml:      
?xml version="1.0" encoding="utf-8"?>  
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
    android:layout_width="fill_parent"  
    android:layout_height="fill_parent"  
    android:orientation="horizontal">  
  
    <TextView  
        android:id="@+id/user_name"  
        android:layout_width="160dip"  
        android:layout_height="wrap_content"  
        android:paddingLeft="2dip"   
        android:paddingTop="2dip"  
        android:textSize="10pt"/>  
  
    <TextView  
        android:id="@+id/user_ip"  
        android:layout_width="160dip"  
        android:layout_height="wrap_content"  
        android:paddingRight="2dip"  
        android:paddingTop="2dip"  
        android:textSize="10pt"/>  
  
</LinearLayout>  

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="horizontal">

    <TextView
        android:id="@+id/user_name"
        android:layout_width="160dip"
        android:layout_height="wrap_content"
        android:paddingLeft="2dip" 
        android:paddingTop="2dip"
        android:textSize="10pt"/>

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

</LinearLayout>

 

ListViewMain.java:    
package com.example.wenandroid;  
  
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;  
  
public class ListViewMain extends ListActivity {  
  
    @Override  
    protected void onCreate(Bundle savedInstanceState) {  
        // TODO Auto-generated method stub   
        super.onCreate(savedInstanceState);  
        setContentView(R.layout.listview);  
        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>();  
        map1.put("user_name", "張三");  
        map1.put("user_ip", "192.168.0.110");  
        map2.put("user_name", "李四");  
        map2.put("user_ip", "192.168.0.111");  
        list.add(map1);  
        list.add(map2);  
        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});  
        setListAdapter(listAdapter);  
    }  
    protected void onListItemClick(ListView l,View v,int position,long id){  
        super.onListItemClick(l, v, position, id);  
        System.out.println("id="+id);  
        System.out.println("position="+position);  
    }  
  
}  

package com.example.wenandroid;

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;

public class ListViewMain extends ListActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.listview);
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>();
map1.put("user_name", "張三");
map1.put("user_ip", "192.168.0.110");
map2.put("user_name", "李四");
map2.put("user_ip", "192.168.0.111");
list.add(map1);
list.add(map2);
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});
setListAdapter(listAdapter);
}
protected void onListItemClick(ListView l,View v,int position,long id){
super.onListItemClick(l, v, position, id);
System.out.println("id="+id);
System.out.println("position="+position);
}

}

 


  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved