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

前言html

  本章內容是android.widget.SimpleCursorAdapter,版本爲Android 3.0 r1,附帶示例源碼,翻譯來自"深夜未眠",歡迎訪問它的博客:"http://chirs1012f.javaeye.com/",再次感謝"深夜未眠" !期待你一塊兒參與Android API的翻譯,聯繫我over140@gmail.com。
 java

 

聲明android

  歡迎轉載,但請保留文章原始出處:)
 數據庫

    農民伯伯:http://over140.blog.51cto.com/ide

    Android中文翻譯組:http://goo.gl/6vJQl
 函數

 

正文佈局

  1、結構

public class SimpleCusrorAdapter extends ResourceCusorAdpaterspa

 

java.lang.Object線程

android.widget.BaseAdapter翻譯

         android.widget.CursorAdapter

                            android.widget.ResourceCusorAdapter

                                     android.widget.SimpleCusorAdapter

 

  2、概述

  這是一個用起來很方便的適配器類,它主要將CursorTexiViewImageView進行映射。好比,你想設定要展現三列,那麼當作好綁定以後,視圖就會展現你設定好的那些列;固然了,視圖的外觀是定義在XML文件裏面的,你只需用這個類與視圖作好綁定就能夠了。與視圖綁定有兩個階段。第一階段:若是SimpleCursorAdapter.ViewBinder可用,將會調用setViewValue(android.view.View, android.database.Cursor, int)方法。該方法返回true就說明綁定成功,不然返回false ,這就到了第二階段,SimpleCursorAdapter內部開始自行綁定,過程是這樣的,若綁定到TextView上,調用setViewText();若綁定到ImageView上,調用setViewImage();若是視圖不是TextViewImageView則拋出IllegalStateException異常。當使用帶有過濾器的適配器時,例如,在APIDemo中有個AutoCompleteTextView的例子,咱們能使用SimpleCursorAdapter.CursorToStringConverter和接口         FilterQueryProvider來控制過濾過程。更多信息請參考convertToString(android.database.Cursor) runQueryOnBackgroundThread(CharSequence)

 

  3、內部類

    public interface SimpleCusorAdapter.ViewBinder        

           這個內部接口能夠在外部經過SimpleCursorAdapter.ViewBinder的方式進行Cursor      View的綁定。

 

    public interface SimpleCusorAdapter.CursorToStringConverter  

            這個內部接口能夠在外部經過 SimpleCursorAdapter.CursorToStringConverter 的方式    定義怎樣將 Cursor 轉換成字符串。

 

  4、構造函數

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

           構造函數啓動自動從新查詢(auto-requery)

           這個構造器已被標記爲棄用(@Deprecated)

  該方法不推薦使用,Cursor查詢操做是執行在應用程序的UI線程當中,那麼會致使無響應的狀況。另外一種方式是使用LoaderManagerCursorLoader來進行。

         (譯者注: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 的數據是否有更變。 )

 

  4、公共方法

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

           綁定全部構造函數中的參數from(字段名)一一綁定到參數to(視圖資源ID)。與視圖綁定有兩個階段。第一階段:若是SimpleCursorAdapter.ViewBinder可用,將會調用setViewValue(android.view.View, android.database.Cursor, int)方法。該方法返回true就說明綁定成功,不然返回false ,這就到了第二階段,SimpleCursorAdapter內部開始自行綁定,過程是這樣的,若綁定到TextView上,調用setViewText();若綁定到ImageView上,調用setViewImage();若是視圖不是TextViewImageView則拋出IllegalStateException異常。

  參數

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

                            context          應用程序上下文。

                            cursor            數據庫遊標。該遊標已經移動到指定位置上。

                  異常

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

                   參見

                            bindView(android.view.View, android.content.Context, android.database.Cursor)

                            getViewBinder()

                            setViewBinder(android.widget.SimpleCursorAdapter.ViewBinder)

                            setViewImage(ImageView, String)

                            setViewText(TextView, String)

 

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

           同時更改CursorView的映射關係。

  參數

                       c                 數據庫遊標,若是遊標不可用則爲null 

  from            列名字列表,表示着你要綁定到UI上的列。若是遊標不可用則爲null

  to               展現參數「from」中的列,也就是說ListView中的視圖顯示的是參數「from」的列值,這些視圖應該都是TextView。若是遊標不可用則爲null

                           

  public CharSequence convertToString (Cursor cursor)

  經過CursorToStringConverter接口實現並返回一個CharSequence 類型的值,以表示指定的Cursor。若是沒有設置CursorToStringConverter,那麼就會用另外的方式進行轉換。若是列數爲-1,或者 cursornull返回空串,不然返回cursor.toString()

                  參數

                            cursor               轉換爲CharSequence的數據庫遊標。

          返回值

                   返回一個不爲nullCharSequence類型來表示參數cursor

 

  public SimpleCursorAdapter.CursorToStringConverter getCursorToStringConverter ()

           返回自定義的SimpleCursorAdapter.CursorToStringConverter的實現。

  返回值

                   若是沒有設置SimpleCursorAdapter.CursorToStringConverter,則爲null

  參考

           setCursorToStringConverter(android.widget.SimpleCursorAdapter.CursorToStringConverter)

           getStringConversionColumn()

           setStringConversionColumn(int)

           convertToString(android.database.Cursor)

 

  public int getStringConversionColumn ()

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

  返回值

                   返回列位置,若是沒有則返回-1

  參考

                            convertToString(android.database.Cursor)

                            setStringConversionColumn(int)

                 setCursorToStringConverter(android.widget.SimpleCursorAdapter.CursorToStringConverter)

                            getCursorToStringConverter()

 

  public SimpleCursorAdapter.ViewBinder getViewBinder ()

  返回SimpleCursorAdapter.ViewBinder引用,這個ViewBinder用來將數據綁定到視圖上的。

  返回值

                   若是ViewBinder不存在,則返回null

  參考

           bindView(android.view.View, android.content.Context, android.database.Cursor)

           setViewBinder(android.widget.SimpleCursorAdapter.ViewBinder)

 

  public void setCursorToStringConverter (SimpleCursorAdapter.CursorToStringConverter cursorToStringConverter)

  設置String轉換器。

                   參數

                            cursorToStringConverter String轉換器,設置爲null就意味着移除。

                   參考

           setCursorToStringConverter(android.widget.SimpleCursorAdapter.CursorToStringConverter)

           getStringConversionColumn()

           setStringConversionColumn(int)

           convertToString(android.database.Cursor)

 

  public void setStringConversionColumn (int stringConversionColumn)

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

                   參數

                            stringConversionColumn 列位置,若是參數爲-1,則使用默認轉換機制。

                   參考

           convertToString(android.database.Cursor)

           getStringConversionColumn()

           setCursorToStringConverter(android.widget.SimpleCursorAdapter.CursorToStringConverter)

           getCursorToStringConverter()

 

  public void setViewBinder (SimpleCursorAdapter.ViewBinder viewBinder)

  設置視圖綁定器。

                   參數

                            viewBinder 視圖綁定器,可用爲null刪除現有的綁定器。

                   參考

           bindView(android.view.View, android.content.Context, android.database.Cursor)

           getViewBinder()

 

  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)

  交換兩個Cursor的列以及它們的數據,並最終返回的仍是舊的Cursor。不一樣於changeCursor(Cursor)的是,舊的Cursor非但不會關閉,並且還會返回出去。(譯者注:       3.0新添的方法)

                   參數

                            c 新的Cursor對象。

                   返回值

          返回舊的 Cursor 引用,若是參數 cursor 不可用,則返回 null 。若是參數 cursor 與原來的 Cursor 引用相同,那麼也返回 null

 

  5、補充

    文章精選

      ArrayAdapterSimpleCursorAdapter例子

      Android API : SimpleCursorAdapter()

    示例代碼

      Demo_SimpleCursorAdapter(深夜未眠).rar

 

 

 

SimpleCursorAdapter.CursorToStringConverter

譯者署名: 深夜未眠

譯者連接:http://chris1012f.javaeye.com/

版本:Android 3.0 r1

 

 

正文

  1、結構

 

public static interface SimpleCusrorAdapter.CursorToStringConverter

 

java.lang.Object

android.widget.SimpleCursorAdapter.CursorToStringConverter

 

  2、概述
 

         這個內部接口能夠在外部經過SimpleCursorAdapter.CursorToStringConverter的方式定義怎樣將Cursor轉換成字符串。

         參見

                   convertToString(android.database.Cursor)

 

  3、公共方法
 

  public abstract CharSequence convertToString (Cursor cursor)

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

                   參數

                            cusor   須要轉換成CharSequence類型的數據庫遊標。

                   返回值

                            返回表明參數cursor的非空字符串。

 

 

 

SimpleCursorAdapter.ViewBinder

譯者署名: 深夜未眠

譯者連接:http://chris1012f.javaeye.com/

版本:Android 3.0 r1

 

 

正文

  1、結構

 

public static interface SimpleCusrorAdapter.ViewBinder

 

java.lang.Object

android.widget.SimpleCursorAdapter.ViewBinder

 

 

  2、概述 

 

         這個內部接口能夠在外部經過SimpleCursorAdapter.ViewBinder的方式進行Cursor      View的綁定。Android推薦咱們採用這種方式進行綁定操做,而不是沿用SimpleCursorAdapter內部的方式。

           參見

               bindView(android.view.View, android.content.Context, android.database.Cursor)

                   setViewImage(ImageView, String)

                   setViewText(TextView, String)

 

 

  3、公共方法

 

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

  將指定的列數據綁定到指定的視圖上。當ViewBinder處理時,這個方法必須返回true。若是這個方法返回false SimpleCursorAdapter將用本身的方式進行綁定操做。

                   參數

                            view                         被綁定的視圖。

                            cursor                      數據庫遊標,綁定數據從它這裏獲取

                            columnIndex          列位置,可以在數據庫遊標中尋找到。

                   返回值

                            返回true意味着數據與視圖已經綁定上,不然爲未綁定上。

相關文章
相關標籤/搜索