Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
您现在的位置: Android教程網 >> Android系統教程 >  >> Android 技術 >> Android 開發實例

Android CursorTreeAdapter 完全解析

編輯:Android 開發實例

CursorTreeAdapter 是一個相當實用的抽象適配器類別。它能讓我們以連續的游標(Cursor)物件存取資料庫,並將查詢結果呈現在可伸縮列表檢視 ExpandableListView 上。

它的運作方式是:建構子中指定的頂層游標負責顯示全部的「群組(groups)」,而每個群組底下對應的子元素游標,則由 getChildrenCursor(Cursor) 方法取得並呈現。這裡有個至關重要的前提——游標所攜帶的結果集中,必須包含一個名為「_id」的欄位,否則這個類別將無法正常運作。

類別結構

public abstract class CursorTreeAdapter

     extends BaseExpandableListAdapter implements Filterable

其繼承階層如下:

java.lang.Object
android.widget.BaseExpandableListAdapter
     android.widget.CursorTreeAdapter

直接子類為 ResourceCursorTreeAdapter,而間接子類則是大家耳熟能詳的 SimpleCursorTreeAdapter。

建構子

public CursorTreeAdapter (Cursor cursor, Context context)

每當資料庫中的資料發生變動時,適配器會自動呼叫 requery() 重新查詢,以顯示最新的資料。

  • cursor:為群組(groups)提供資料的游標。
  • context:應用程式的上下文環境。

public CursorTreeAdapter (Cursor cursor, Context context, boolean autoRequery)

  • cursor:為群組提供資料的游標。
  • context:應用程式的上下文環境。
  • autoRequery:設為 true 時,資料庫變動會觸發 requery() 重新查詢以更新顯示。

抽象方法

protected abstract void bindChildView (View view, Context context, Cursor cursor, boolean isLastChild)

以游標方式將子元素資料綁定到既有的檢視物件上。

  • view:既有的檢視物件(先前 new 出來的)。
  • context:應用程式上下文。
  • cursor:已取得資料且已移動到正確位置的游標。
  • isLastChild:子元素是否為群組中的最後一項。

protected abstract void bindGroupView (View view, Context context, Cursor cursor, boolean isExpanded)

以游標方式將群組資料綁定到既有的檢視物件上。

  • view:既有的群組檢視物件。
  • context:應用程式上下文。
  • cursor:已取得資料且已移動到正確位置的游標。
  • isExpanded:該群組處於展開或收合狀態。

protected abstract Cursor getChildrenCursor (Cursor groupCursor)

取得指定群組中的子元素游標。子類別必須實作此方法以回傳子元素資料。若想以異步查詢避免 UI 阻塞,這個方法可以回傳 null,或稍後再呼叫 setChildrenCursor(int, Cursor)。你有責任在 Activity 生命週期中妥善管理這個游標,一個好做法是使用 managedQuery(...) 來統一管理。某些情況下,適配器本身會讓游標失效,但這並非必然,因此務必確保游標被正確管理。

protected abstract View newChildView (Context context, Cursor cursor, boolean isLastChild, ViewGroup parent)

建立一個新的子元素檢視,並持有指向資料的游標。

protected abstract View newGroupView (Context context, Cursor cursor, boolean isExpanded, ViewGroup parent)

建立一個新的群組檢視,並持有群組中指向資料的游標。

實作範例

使用時,主要就是實作上述這幾個抽象方法。以下以聯絡人清單為例:

  1. public class CursorTreeAdapterExample extends CursorTreeAdapter {  
  2.         private int mGroupIdColumnIndex;  
  3.         private LayoutInflater mInflater;  
  4.         //注意這裡的游標是一級項的  
  5.         public CursorTreeAdapterExample(Cursor cursor, Context context) {  
  6.             super(cursor, context);  
  7.             
  8.             mGroupIdColumnIndex = cursor.getColumnIndexOrThrow(Phone._ID);  
  9.             mInflater = (LayoutInflater) context.getSystemService(LAYOUT_INFLATER_SERVICE);  
  10.         }  
  11.         //注意這裡的游標是二級項的  
  12.         @Override 
  13.         protected void bindChildView(View view, Context context, Cursor cursor, boolean isExpanded) {  
  14.             // Bind the related data with this child view  
  15.             ((TextView)view).setText(cursor.getString(cursor.getColumnIndex(Phone.NUMBER)));  
  16.         }  
  17.         @Override 
  18.         protected void bindGroupView(View view, Context context, Cursor cursor, boolean isExpanded) {  
  19.             // Bind the related data with this group view  
  20.             ((TextView)view).setText(cursor.getString(cursor.getColumnIndex(Phone.DISPLAY_NAME)));  
  21.         }  
  22.         //注意這裡通過一次數據庫查詢才得到了二級項的數據  
  23.         @Override 
  24.         protected Cursor getChildrenCursor(Cursor groupCursor) {  
  25.             Uri.Builder builder = Phone.CONTENT_URI.buildUpon();  
  26.             ContentUris.appendId(builder, groupCursor.getLong(mGroupIdColumnIndex));  
  27.             Uri phoneNumbersUri = builder.build();  
  28.             // The returned Cursor MUST be managed by us, so we use Activity's helper  
  29.             // functionality to manage it for us.  
  30.             return managedQuery(phoneNumbersUri, new String[] {Phone._ID, Phone.NUMBER}, null, null, null);  
  31.         }  
  32.         @Override 
  33.         protected View newChildView(Context context, Cursor cursor, boolean isExpanded, ViewGroup parent) {  
  34.             Log.d(TAG, "newChildView");  
  35.             
  36.             TextView view = (TextView) mInflater.inflate(android.R.layout.simple_expandable_list_item_1, parent, false);  
  37.             
  38.             view.setText("  (" + cursor.getPosition() + ") " + cursor.getString(cursor.getColumnIndex(Phone.NUMBER)));  
  39.             
  40.             return view;  
  41.         }  
  42.         @Override 
  43.         protected View newGroupView(Context context, Cursor cursor, boolean isExpanded, ViewGroup parent) {  
  44.             Log.d(TAG, "newGroupView");  
  45.         TextView view = (TextView) mInflater.inflate(android.R.layout.simple_expandable_list_item_1, parent, false);  
  46.         view.setText("  (" + cursor.getPosition() + ") " + cursor.getString(cursor.getColumnIndex(Phone.DISPLAY_NAME)));  
  47.         
  48.             return view;  
  49.         }  
  50.     }

透過這個範例可以清楚看到:getChildrenCursor 會依據點選的群組游標,另行查詢該聯絡人底下的電話號碼作為子元素資料,並用 managedQuery 交由 Activity 統一管理游標生命週期,這正是實務上相當推薦的做法。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © Android教程網 All Rights Reserved