天天看點

Android 3.0 r1 API中文文檔(106) —— SimpleCursorAdapter

一、結構

public class SimpleCusrorAdapter extends ResourceCusorAdpater

java.lang.Object

android.widget.BaseAdapter

         android.widget.CursorAdapter

                            android.widget.ResourceCusorAdapter

                                     android.widget.SimpleCusorAdapter

二、概述

三、内部類

    public interface SimpleCusorAdapter.ViewBinder        

           這個内部接口可以在外部通過SimpleCursorAdapter.ViewBinder的方式進行Cursor      與View的綁定。

    public interface SimpleCusorAdapter.CursorToStringConverter  

           這個内部接口可以在外部通過SimpleCursorAdapter.CursorToStringConverter的方式   定義怎樣将Cursor轉換成字元串。

四、構造函數

    public SimpleCursorAdapter (Context context, int layout, Cursor c, String[] from, int[] to)

           構造函數啟動自動重新查詢(auto-requery)。

           這個構造器已被标記為棄用(@Deprecated) 。

  該方法不推薦使用,Cursor查詢操作是執行在應用程式的UI線程當中,那麼會導緻無響應的情況。另一種方式是使用LoaderManager和CursorLoader來進行。

         (譯者注:3.0已不推薦使用該構造方法)

                  參數

                       context  應用程式上下文,具體來說就是ListView所在的上下文當中。

   layout    布局檔案的資源定位辨別符,也就是說辨別了ListView中的item。那麼這個布局檔案至少包含了參數“to”中的傳進來值。

                       c            資料庫遊标,如果遊标不可用則為null。

  from       列名字清單,表示着你要綁定到UI上的列。如果遊标不可用則為null。

  to           展示參數“from”中的列,也就是說ListView中的視圖顯示的是參數“from”的列值,這些視圖應該都是TextView。如果遊标不可用則為null。

    public SimpleCursorAdapter (Context context, int layout, Cursor c, String[] from, int[]      to,int flags)     

           該擴充卡類标準的構造函數。(譯者注:3.0新添的構造方法)

                    context  應用程式上下文,具體來說就是ListView所在的上下文當中。

   layout     布局檔案的資源定位辨別符,也就是說辨別了ListView中的item。那麼這個布局檔案至少包含了參數“to”中的傳進來值。

                        c            資料庫遊标,如果遊标不可用則為null。

   from       列名字清單,表示着你要綁定到UI上的列。如果遊标不可用則為null。

   to           展示參數“from”中的列,也就是說ListView中的視圖顯示的是參數“from”的列值,這些視圖應該都是TextView。如果遊标不可用則為null。

 flags               這個标志用來決定該擴充卡的行為。(譯者注:Android3.0推薦我們傳遞CursorAdapter.FLAG_REGISTER_CONTENT_OBSERVER。設定标志用來添加一個監聽器,監聽着參數cursor的資料是否有更變。)

四、公共方法

  public void bindView (View view, Context context, Cursor cursor)

  參數

                            view               已存在的視圖(View)對象, 也就是早先new出來的。

                            context          應用程式上下文。

                            cursor            資料庫遊标。該遊标已經移動到指定位置上。

                  異常

                            IllegalStateException 如果綁定的視圖中不是TextView或是ImageView則會                           抛出這個異常。

                   參見

  public void changeCursorAndColumns(Cursor c, String[] from, int[] to)

           同時更改Cursor與View的映射關系。

                       c                 資料庫遊标,如果遊标不可用則為null。 

  from            列名字清單,表示着你要綁定到UI上的列。如果遊标不可用則為null。

  to               展示參數“from”中的列,也就是說ListView中的視圖顯示的是參數“from”的列值,這些視圖應該都是TextView。如果遊标不可用則為null。               

  public CharSequence convertToString (Cursor cursor)

  通過CursorToStringConverter接口實作并傳回一個CharSequence 類型的值,以表示指定的Cursor。如果沒有設定CursorToStringConverter,那麼就會用另外的方式進行轉換。如果列數為-1,或者 cursor為null傳回空串,否則傳回cursor.toString()。

                            cursor               轉換為CharSequence的資料庫遊标。

          傳回值

                   傳回一個不為null的CharSequence類型來表示參數cursor。

  public SimpleCursorAdapter.CursorToStringConverter getCursorToStringConverter ()

           傳回自定義的SimpleCursorAdapter.CursorToStringConverter的實作。

  傳回值

                   如果沒有設定SimpleCursorAdapter.CursorToStringConverter,則為null。

  參考

  public int getStringConversionColumn ()

           傳回轉換成String類型的列位置。

                   傳回列位置,如果沒有則傳回-1。

  public SimpleCursorAdapter.ViewBinder getViewBinder ()

  傳回SimpleCursorAdapter.ViewBinder引用,這個ViewBinder用來将資料綁定到視圖上的。

                   如果ViewBinder不存在,則傳回null。

  public void setCursorToStringConverter (SimpleCursorAdapter.CursorToStringConverter cursorToStringConverter)

  設定String轉換器。

                   參數

                            cursorToStringConverter String轉換器,設定為null就意味着移除。

                   參考

  public void setStringConversionColumn (int stringConversionColumn)

  設定Cursor中的列要轉換成String類型的位置。不過僅當未設定CursorToStringConverter時,這個列才會進行預設轉換。

                            stringConversionColumn 列位置,如果參數為-1,則使用預設轉換機制。

  public void setViewBinder (SimpleCursorAdapter.ViewBinder viewBinder)

  設定視圖綁定器。

                            viewBinder 視圖綁定器,可用為null删除現有的綁定器。

  public void setViewImage (ImageView v, String value)

  僅當ViewBinder不存在或是當ViewBinder不為ImageView綁定時(也就是setViewValue()傳回false),則這個方法會被bindView()調用,以便為ImageView設定圖檔。預設情況下,參數value作為圖檔資源ID來看待,否則,會視為圖檔的Uri。         另外還可以通過過濾器來獲得更靈活的設定。

                            v                 圖檔控件引用

                            value         圖檔資源ID,是從Cursor擷取到的。

  public void setViewText (TextView v, String text)

  僅當ViewBinder不存在或是當ViewBinder不為TextView綁定時(也就是setViewValue()傳回false),則這個方法會被bindView()調用,以便為TextView設定文本。可用重寫擴充卡從資料庫中檢索過濾字元串。

                            v                 文本控件引用

                            value         為文本控件設定的文本資訊(譯者注:是從Cursor擷取到的)。

  public Cursor swapCursor (Cursor c)

                            c 新的Cursor對象。

                   傳回值

  傳回舊的Cursor引用,如果參數cursor不可用,則傳回null。如果參數cursor與原來的Cursor引用相同,那麼也傳回null。

五、補充

    文章精選

    示例代碼

SimpleCursorAdapter.CursorToStringConverter

譯者署名: 深夜未眠

版本:Android 3.0 r1 

正文

public static interface SimpleCusrorAdapter.CursorToStringConverter

android.widget.SimpleCursorAdapter.CursorToStringConverter 

  二、概述 

         這個内部接口可以在外部通過SimpleCursorAdapter.CursorToStringConverter的方式定義怎樣将Cursor轉換成字元串。

         參見

  三、公共方法 

  public abstract CharSequence convertToString (Cursor cursor)

          傳回CharSequence類型的值,用來表示參數cursor。

                            cusor   需要轉換成CharSequence類型的資料庫遊标。

                            傳回代表參數cursor的非空字元串。

SimpleCursorAdapter.ViewBinder

public static interface SimpleCusrorAdapter.ViewBinder

android.widget.SimpleCursorAdapter.ViewBinder

         這個内部接口可以在外部通過SimpleCursorAdapter.ViewBinder的方式進行Cursor      與View的綁定。Android推薦我們采用這種方式進行綁定操作,而不是沿用SimpleCursorAdapter内部的方式。

           參見

  三、公共方法

  public abstract boolean setViewValue (View view, Cursor cursor, int columnIndex)

  将指定的列資料綁定到指定的視圖上。當ViewBinder處理時,這個方法必須傳回true。如果這個方法傳回false, SimpleCursorAdapter将用自己的方式進行綁定操作。

                            view                         被綁定的視圖。

                            cursor                      資料庫遊标,綁定資料從它這裡擷取

                            columnIndex          列位置,能夠在資料庫遊标中尋找到。

                            傳回true意味着資料與視圖已經綁定上,否則為未綁定上。