鍍金池/ 教程/ Android/ 數(shù)據(jù)綁定 Data Binding
Dialog 顯示圖像
線程 Bezier 曲線
創(chuàng)建應(yīng)用程序框架
引路蜂二維圖形庫(kù)簡(jiǎn)介及顏色示例
Android 應(yīng)用基本概念
Intents 和 Intent Filters
安裝開(kāi)發(fā)環(huán)境
Option Menu 畫(huà)筆示例
自定義對(duì)話框 Transform
數(shù)據(jù)綁定 Data Binding
概述
Broadcast Receiver 短信觸發(fā)示例
發(fā)布應(yīng)用
自定義 Adapter 顯示列表
RadioButton 多邊形及路徑繪制
訪問(wèn) Internet 繪製在線地圖
第一個(gè)應(yīng)用 Hello World
Activities
Button 畫(huà)刷示例
使用資源 Resources
Context Menu 繪制幾何圖形
用戶界面設(shè)計(jì)
引路蜂二維圖形繪制實(shí)例功能定義

數(shù)據(jù)綁定 Data Binding

前面提到 AndroidGraphics2DTutorial 說(shuō)過(guò)它是 ListActivity 派生出來(lái)的。ListActivity 中顯示的是 ListView,ListView 和 Gallery ,Spinner 有一個(gè)共同點(diǎn):它們都是 AdapterView 的子類(lèi)。AdapterView 的顯示可以通過(guò)數(shù)據(jù)綁定來(lái)實(shí)現(xiàn),數(shù)據(jù)源可以是數(shù)組或是數(shù)據(jù)庫(kù)記錄,數(shù)據(jù)源和AdapterView 是通過(guò) Adapter 作為橋梁。通過(guò) Adapter,AdatperView 可以顯示數(shù)據(jù)源或處理用戶選取時(shí)間,如:選擇列表中某項(xiàng)。

http://wiki.jikexueyuan.com/project/android-development-tutorial/images/19.png" alt="" />

AndroidGraphics2DTutorial 讀取 AndroidManifest.xml中Intent-Filter為

的所有 Activity,以列表方式顯示。使用了 Android API 自帶的 SimpleAdapter。 來(lái)看看AndroidGraphics2DTutorial.java 中相關(guān)代碼:

public class AndroidGraphics2DTutorial extends ListActivity {

 private static final String SAMPLE_CATEGORY
          ="com.pstreets.graphics2d.SAMPLE_CODE";

 @Override
 public void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);

  setListAdapter(new SimpleAdapter(this, getData(),
    android.R.layout.simple_list_item_1, new String[] { "title" },
    new int[] { android.R.id.text1 }));
  getListView().setTextFilterEnabled(true);

 }

 protected List getData() {
  List<Map> myData = new ArrayList<Map>();

  Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
  mainIntent.addCategory(SAMPLE_CATEGORY);

  PackageManager pm = getPackageManager();
  List<ResolveInfo> list = pm.queryIntentActivities(mainIntent, 0);

  if (null == list)
   return myData;

  String[] prefixPath;

  prefixPath = null;

  int len = list.size();

  Map<String, Boolean> entries = new HashMap<String, Boolean>();

  for (int i = 0; i < len; i++) {
   ResolveInfo info = list.get(i);
   CharSequence labelSeq = info.loadLabel(pm);
   String label = labelSeq != null ? labelSeq.toString()
     : info.activityInfo.name;

   String[] labelPath = label.split("/");

   String nextLabel = prefixPath == null ? labelPath[0]
     : labelPath[prefixPath.length];

   if ((prefixPath != null ? prefixPath.length : 0) 
== labelPath.length - 1) {
    addItem(myData,
      nextLabel,
      activityIntent(
        info.activityInfo.applicationInfo.packageName,
        info.activityInfo.name));
   } else {
    if (entries.get(nextLabel) == null) {
     addItem(myData, nextLabel, browseIntent(nextLabel));
     entries.put(nextLabel, true);
    }
   }

  }

  Collections.sort(myData, sDisplayNameComparator);

  return myData;
 }

 private final static Comparator<Map> sDisplayNameComparator 
= new Comparator<Map>() {
  private final Collator collator = Collator.getInstance();

  public int compare(Map map1, Map map2) {
   return collator.compare(map1.get("title"), map2.get("title"));
  }
 };

 protected Intent activityIntent(String pkg, String componentName) {
  Intent result = new Intent();
  result.setClassName(pkg, componentName);
  return result;
 }

 protected Intent browseIntent(String path) {
  Intent result = new Intent();
  result.setClass(this, AndroidGraphics2DTutorial.class);
  return result;
 }

 protected void addItem(List<Map> data, String name, Intent intent) {
  Map<String, Object> temp = new HashMap<String, Object>();
  temp.put("title", name);
  temp.put("intent", intent);
  data.add(temp);
 }

 @Override
 protected void onListItemClick(ListView l, View v, 
    int position, long id) {
  Map map = (Map) l.getItemAtPosition(position);
  Intent intent = (Intent) map.get("intent");
  startActivity(intent);
 }
}

使用數(shù)據(jù)顯示 Layout,上面代碼中

setListAdapter(new SimpleAdapter(this, getData(), android.R.layout.simple_list_item_1, new String[] { “title” }, new int[] { android.R.id.text1 }));

為L(zhǎng)istActivity 中 ListView 指定 Adapter,這個(gè) Adapter 的數(shù)據(jù)源為 getData(),getData()從Manifest.xml 中查找出所有符合條件的示例 Activity 列表。 這里 DataSource 是靜態(tài)的從文件中讀取,如果 DataSource 為數(shù)組或是其它數(shù)據(jù)源,如果程序中修改數(shù)值的內(nèi)容,則你應(yīng)該notifyDataSetChanged()來(lái)通知 UI 數(shù)據(jù)有變動(dòng)。UI則會(huì)刷新顯示以反映數(shù)據(jù)變化。簡(jiǎn)單的說(shuō)Android 數(shù)據(jù)綁定和 .Net WinForm ,WPF 中數(shù)據(jù)綁定類(lèi)似。

處理用戶選取事件,AdapterView.OnItemClickListener()可以用來(lái)處理選取事件,對(duì)于ListActivity,可以用 protected void onListItemClick(ListView l, View v, int position, long id)。AndroidGraphics2DTutorial中的實(shí)現(xiàn)是用戶選取 Activity 名稱好,則啟動(dòng)對(duì)應(yīng)的 Activity。

上面代碼中使用 SimpleAdapter,并使用 Android 提供的android.R.layout.simple_list_item_1來(lái)顯示數(shù)據(jù),Andrid 也允許使用自定義的 Layout 來(lái)顯示數(shù)據(jù),對(duì)這個(gè)例子來(lái)說(shuō),可以使用圖片加說(shuō)明來(lái)顯示列表,將在后面介紹如果使用自定義 Adapter 和自定義 Layout 來(lái)顯示綁定的數(shù)據(jù)。

Tags: Android