天天看點

玩轉Android---UI篇---ListView之SampleAdapter(清單)---1

原址:http://hualang.iteye.com/category/143855

ListView是清單元件,這個ListView是我接觸的目前所有Android UI控件中最為麻煩的控件,之是以麻煩就是因為它的各種的擴充卡Adapter特别麻煩,Adapter的組織結構圖如下

玩轉Android---UI篇---ListView之SampleAdapter(清單)---1

 在ListView中,以内不同的Adapter不同,是以也會有不同的效果,其中比較常用的是SampleAdapter,SimpleCursorAdapter,ArrayAdapter,BaseAdapter等,

萬事開頭難,還是從最簡單的SimpleAdapter說起,以後再一點點學習

simpleAdapter的擴充性最好,可以定義各種各樣的布局出來,可以放上ImageView(圖檔),還可以放上Button(按鈕),CheckBox(複選框)等等。下面的代碼都直接繼承了ListActivity,ListActivity和普通的Activity沒有太大的差别,不同就是對顯示ListView做了許多優化,方面顯示而已。

先看看一個執行個體,是由SimpleAdapter與ListView綁定後的一個小例子。

ListViewone.java檔案

Java代碼  

玩轉Android---UI篇---ListView之SampleAdapter(清單)---1
  1. package org.hualang.simpleadapter;  
  2. import java.util.ArrayList;  
  3. import java.util.HashMap;  
  4. import android.app.ListActivity;  
  5. import android.os.Bundle;  
  6. import android.view.View;  
  7. import android.widget.ListView;  
  8. import android.widget.SimpleAdapter;  
  9. import android.widget.Toast;  
  10. public class ListViewone extends ListActivity {  
  11.     private Toast toast;  
  12.     @Override  
  13.     public void onCreate(Bundle savedInstanceState) {  
  14.         super.onCreate(savedInstanceState);  
  15.         setContentView(R.layout.main);  
  16.         ArrayList<HashMap<String,String>> list=new ArrayList<HashMap<String,String>>();  
  17.         HashMap<String,String> map1=new HashMap<String,String>();  
  18.         HashMap<String,String> map2=new HashMap<String,String>();  
  19.         HashMap<String,String> map3=new HashMap<String,String>();  
  20.         map1.put("name", "凝墨");  
  21.         map1.put("phone", "13699452790");  
  22.         map2.put("name", "小棕");  
  23.         map2.put("phone", "15827980910");  
  24.         map3.put("name", "花郎");  
  25.         map3.put("phone", "18678091166");  
  26.         list.add(map1);  
  27.         list.add(map2);  
  28.         list.add(map3);  
  29.         SimpleAdapter listAdapter=new SimpleAdapter(this,  
  30.                 list,  
  31.                 R.layout.info,  
  32.                 new String[] {"name","phone"},  
  33.                 new int[] {R.id.name,R.id.phone});  
  34.         setListAdapter(listAdapter);  
  35.     }  
  36.     protected void onListItemClick(ListView l,View v,int position,long id)  
  37.     {  
  38.         super.onListItemClick(l,v,position,id);  
  39.         if(l.getItemIdAtPosition(position)==0)  
  40.         {  
  41.             toast.makeText(getApplicationContext(), "我是凝墨", Toast.LENGTH_SHORT).show();  
  42.         }else if(l.getItemIdAtPosition(position)==1)  
  43.         {  
  44.             toast.makeText(getApplicationContext(), "我是小棕", Toast.LENGTH_SHORT).show();  
  45.         }else if(l.getItemIdAtPosition(position)==2)  
  46.         {  
  47.             toast.makeText(getApplicationContext(), "我是花郎", Toast.LENGTH_SHORT).show();  
  48.         }  
  49.     }  
  50. }  

 main.xml檔案

Java代碼  

玩轉Android---UI篇---ListView之SampleAdapter(清單)---1
  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  
  8.         android:layout_width="fill_parent"  
  9.         android:layout_height="wrap_content"  
  10.         android:id="@+id/linearlayout"  
  11.         android:orientation="vertical"  
  12.     >  
  13.         <ListView  
  14.             android:id="@id/android:list"  
  15.             android:layout_width="fill_parent"  
  16.             android:layout_height="wrap_content"  
  17.             android:drawSelectorOnTop="false"  
  18.             android:scrollbars="vertical"  
  19.         />  
  20.     </LinearLayout>  
  21. </LinearLayout>  

 info.xml

Java代碼  

玩轉Android---UI篇---ListView之SampleAdapter(清單)---1
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout  
  3.   xmlns:android="http://schemas.android.com/apk/res/android"  
  4.   android:layout_width="wrap_content"  
  5.   android:layout_height="wrap_content"  
  6.   android:paddingLeft="10dip"  
  7.   android:paddingRight="10dip"  
  8.   android:paddingTop="1dip"  
  9.   android:paddingBottom="1dip"  
  10.   >  
  11.   <TextView  
  12.     android:id="@+id/name"  
  13.     android:layout_width="180dip"  
  14.     android:layout_height="30dip"  
  15.     android:textSize="10pt"  
  16.     android:singleLine="true"  
  17.   />  
  18.   <TextView  
  19.     android:id="@+id/phone"  
  20.     android:layout_width="fill_parent"  
  21.     android:layout_height="fill_parent"  
  22.     android:gravity="right"  
  23.     android:textSize="10pt"  
  24.   />  
  25. </LinearLayout>  

 使用simpleAdapter的資料用一般都是HashMap構成的List,list的每一節對應ListView的每一行。HashMap的每個鍵值資料映射到布局檔案中對應id的元件上。因為系統沒有對應的布局檔案可用,我們可以自己定義一個布局info.xml。下面做适配,new一個SimpleAdapter參數一次是:this,布局檔案(info.xml)。布局檔案的元件name,phone。布局檔案的各元件分别映射到HashMap的各元素上,完成适配。

運作結果如下:

玩轉Android---UI篇---ListView之SampleAdapter(清單)---1

 當點選了第一行

玩轉Android---UI篇---ListView之SampleAdapter(清單)---1

執行個體2:顯示一個帶圖檔的ListView,使用擴充卡SampleAdapter

ListViewone.java

Java代碼  

玩轉Android---UI篇---ListView之SampleAdapter(清單)---1
  1. package org.hualang.simpleadapter;  
  2. import java.util.ArrayList;  
  3. import java.util.HashMap;  
  4. import java.util.List;  
  5. import java.util.Map;  
  6. import android.app.ListActivity;  
  7. import android.os.Bundle;  
  8. import android.widget.SimpleAdapter;  
  9. public class ListViewone extends ListActivity {  
  10.     @Override  
  11.     public void onCreate(Bundle savedInstanceState) {  
  12.         super.onCreate(savedInstanceState);  
  13.         SimpleAdapter adapter = new SimpleAdapter(this,getData(),R.layout.info,  
  14.                 new String[]{"name","phone","img"},  
  15.                 new int[]{R.id.name,R.id.phone,R.id.img});  
  16.         setListAdapter(adapter);  
  17.     }  
  18.     private List<Map<String, Object>> getData() {  
  19.         List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();  
  20.         Map<String, Object> map = new HashMap<String, Object>();  
  21.         map.put("name", "凝墨");  
  22.         map.put("phone", "13699782346");  
  23.         map.put("img", R.drawable.pic1);  
  24.         list.add(map);  
  25.         map = new HashMap<String, Object>();  
  26.         map.put("name", "小棕");  
  27.         map.put("phone", "15899034671");  
  28.         map.put("img", R.drawable.pic2);  
  29.         list.add(map);  
  30.         map = new HashMap<String, Object>();  
  31.         map.put("name", "花郎");  
  32.         map.put("phone", "18677656526");  
  33.         map.put("img", R.drawable.pic3);  
  34.         list.add(map);  
  35.         return list;  
  36.     }  
  37. }  

 info.xml

Java代碼  

玩轉Android---UI篇---ListView之SampleAdapter(清單)---1
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:orientation="horizontal" android:layout_width="fill_parent"  
  4.     android:layout_height="fill_parent">  
  5.     <ImageView android:id="@+id/img"   
  6.         android:layout_width="wrap_content"  
  7.         android:layout_height="wrap_content"   
  8.         android:layout_margin="5px"/>  
  9.     <LinearLayout android:orientation="vertical"  
  10.         android:layout_width="wrap_content"   
  11.         android:layout_height="wrap_content">  
  12.         <TextView android:id="@+id/name"   
  13.             android:layout_width="wrap_content"  
  14.             android:layout_height="wrap_content"   
  15.             android:textColor="#FFFFFFFF"  
  16.             android:textSize="22px" />  
  17.         <TextView android:id="@+id/phone"   
  18.             android:layout_width="wrap_content"  
  19.             android:layout_height="wrap_content"   
  20.             android:textColor="#FFFFFFFF"  
  21.             android:textSize="13px" />  
  22.     </LinearLayout>  
  23. </LinearLayout>  

 這裡,就不做事件處理了,運作結果如下:

玩轉Android---UI篇---ListView之SampleAdapter(清單)---1

繼續閱讀