Android---UI篇---ListView之SampleAdapter(列表)---1

ListView是列表組件,這個ListView是我接觸的目前所有Android UI控件中最爲麻煩的控件,之所以麻煩就是因爲它的各種的適配器Adapter特別麻煩,Adapter的組織結構圖如下

 
在ListView中,以內不同的Adapter不同,所以也會有不同的效果,其中比較常用的是SampleAdapter,SimpleCursorAdapter,ArrayAdapter,BaseAdapter等,
萬事開頭難,還是從最簡單的SimpleAdapter說起,以後再一點點學習

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

先看看一個實例,是由SimpleAdapter與ListView綁定後的一個小例子。
ListViewone.java文件

Java代碼 
  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.     /** Called when the activity is first created. */
  12.         private Toast toast;
  13.     @Override
  14.     public void onCreate(Bundle savedInstanceState) {
  15.         super.onCreate(savedInstanceState);
  16.         setContentView(R.layout.main);
  17.         ArrayList<HashMap<String,String>> list=new ArrayList<HashMap<String,String>>();
  18.         HashMap<String,String> map1=new HashMap<String,String>();
  19.         HashMap<String,String> map2=new HashMap<String,String>();
  20.         HashMap<String,String> map3=new HashMap<String,String>();
  21.         map1.put("name", "凝墨");
  22.         map1.put("phone", "13699452790");
  23.         map2.put("name", "小棕");
  24.         map2.put("phone", "15827980910");
  25.         map3.put("name", "花郎");
  26.         map3.put("phone", "18678091166");
  27.         
  28.         list.add(map1);
  29.         list.add(map2);
  30.         list.add(map3);
  31.         SimpleAdapter listAdapter=new SimpleAdapter(this,
  32.                         list,
  33.                         R.layout.info,
  34.                         new String[] {"name","phone"},
  35.                         new int[] {R.id.name,R.id.phone});
  36.         setListAdapter(listAdapter);
  37.     }
  38.     protected void onListItemClick(ListView l,View v,int position,long id)
  39.     {
  40.             super.onListItemClick(l,v,position,id);
  41.             if(l.getItemIdAtPosition(position)==0)
  42.             {
  43.                     toast.makeText(getApplicationContext(), "我是凝墨", Toast.LENGTH_SHORT).show();
  44.             }else if(l.getItemIdAtPosition(position)==1)
  45.             {
  46.                     toast.makeText(getApplicationContext(), "我是小棕", Toast.LENGTH_SHORT).show();
  47.             }else if(l.getItemIdAtPosition(position)==2)
  48.             {
  49.                     toast.makeText(getApplicationContext(), "我是花郎", Toast.LENGTH_SHORT).show();
  50.             }
  51.             
  52.     }
  53.     
  54. }
複製代碼


main.xml文件

Java代碼
  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代碼
  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的各元素上,完成適配。

運行結果如下:

 

當點擊了第一行

 


實例2:顯示一個帶圖片的ListView,使用適配器SampleAdapter
ListViewone.java

Java代碼 
  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.                 
  36.                 return list;
  37.         }
  38. }
複製代碼


info.xml

Java代碼
  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>
複製代碼


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

 
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章