搭配 Xamarin.Android 使用 CursorAdapters
Android 提供特別用來顯示 SQLite 資料庫查詢資料的配接器類別:
SimpleCursorAdapter – 類似於 ArrayAdapter
,因為它可以不使用子類別。 只要在建構函式中提供必要的參數(例如游標和版面配置資訊),然後指派給 ListView
。
CursorAdapter – 當您需要更多資料值系結至版面配置控件的控制權時,您可以繼承的基類(例如隱藏/顯示控件或變更其屬性)。
數據指標配接器提供高效能的方式,可捲動儲存在 SQLite 中的數據長清單。 取用程式代碼必須在 物件中 Cursor
定義 SQL 查詢,然後描述如何建立和填入每個數據列的檢視。
建立 SQLite 資料庫
若要示範數據指標配接器,需要簡單的 SQLite 資料庫實作。 SimpleCursorTableAdapter/VegetableDatabase.cs中的程式碼包含程式碼和 SQL,以建立數據表,並填入一些數據。
完整 VegetableDatabase
類別如下所示:
class VegetableDatabase : SQLiteOpenHelper {
public static readonly string create_table_sql =
"CREATE TABLE [vegetables] ([_id] INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL UNIQUE, [name] TEXT NOT NULL UNIQUE)";
public static readonly string DatabaseName = "vegetables.db";
public static readonly int DatabaseVersion = 1;
public VegetableDatabase(Context context) : base(context, DatabaseName, null, DatabaseVersion) { }
public override void OnCreate(SQLiteDatabase db)
{
db.ExecSQL(create_table_sql);
// seed with data
db.ExecSQL("INSERT INTO vegetables (name) VALUES ('Vegetables')");
db.ExecSQL("INSERT INTO vegetables (name) VALUES ('Fruits')");
db.ExecSQL("INSERT INTO vegetables (name) VALUES ('Flower Buds')");
db.ExecSQL("INSERT INTO vegetables (name) VALUES ('Legumes')");
db.ExecSQL("INSERT INTO vegetables (name) VALUES ('Bulbs')");
db.ExecSQL("INSERT INTO vegetables (name) VALUES ('Tubers')");
}
public override void OnUpgrade(SQLiteDatabase db, int oldVersion, int newVersion)
{ // not required until second version :)
throw new NotImplementedException();
}
}
類別VegetableDatabase
將會在 活動的 方法HomeScreen
中OnCreate
具現化。 SQLiteOpenHelper
基類會管理資料庫檔案的設定,並確保其 OnCreate
方法中的 SQL 只會執行一次。 在和的下列兩個範例SimpleCursorAdapter
CursorAdapter
中,會使用此類別。
數據指標查詢 必須 有整數數據行 _id
,才能 CursorAdapter
運作。 如果基礎表沒有名為 _id
的整數數據行,請在 組成數據指標的 中 RawQuery
,針對另一個唯一整數使用數據行別名。 如需詳細資訊, 請參閱 Android 檔 。
建立數據指標
這些範例會使用 RawQuery
將 SQL 查詢轉換成 Cursor
物件。 從數據指標傳回的數據行清單會定義可用於顯示在數據指標配接器中的數據行。 在 SimpleCursorTableAdapter/HomeScreen.cs OnCreate
方法中建立資料庫的程序代碼如下所示:
vdb = new VegetableDatabase(this);
cursor = vdb.ReadableDatabase.RawQuery("SELECT * FROM vegetables", null); // cursor query
StartManagingCursor(cursor);
// use either SimpleCursorAdapter or CursorAdapter subclass here!
撥話的任何程式 StartManagingCursor
代碼也應該呼叫 StopManagingCursor
。 範例會使用 OnCreate
來啟動,並 OnDestroy
關閉游標。 方法 OnDestroy
包含下列程式代碼:
StopManagingCursor(cursor);
cursor.Close();
一旦應用程式有 SQLite 資料庫可用且已建立數據指針對象,即可利用 SimpleCursorAdapter
或 子類別 CusorAdapter
,在 中顯示數據 ListView
列。
使用 SimpleCursorAdapter
SimpleCursorAdapter
就像 是 ArrayAdapter
,但特製化來與 SQLite 搭配使用。 它不需要子類別化 – 只要在建立 物件時設定一些簡單的參數,然後將它指派給 ListView
的 Adapter
屬性。
SimpleCursorAdapter 建構函式的參數如下:
Context – 包含活動的參考。
配置 – 要使用的數據列檢視資源識別碼。
ICursor – 資料指標,其中包含要顯示之數據的 SQLite 查詢。
從 字串陣列 – 對應至資料指標中資料行名稱的字串陣列。
整數 數位 – 對應至數據列配置中控制元件的版面配置識別碼陣列。 陣列中指定之數據行的值將會系結至這個陣列中 from
指定之相同索引的ControlID。
from
和 to
數位必須有相同的項目數目,因為它們會形成從數據源到檢視中版面配置控制件的對應。
SimpleCursorTableAdapter/HomeScreen.cs範例程式代碼會連接SimpleCursorAdapter
如下:
// which columns map to which layout controls
string[] fromColumns = new string[] {"name"};
int[] toControlIDs = new int[] {Android.Resource.Id.Text1};
// use a SimpleCursorAdapter
listView.Adapter = new SimpleCursorAdapter (this, Android.Resource.Layout.SimpleListItem1, cursor,
fromColumns,
toControlIDs);
SimpleCursorAdapter
是在中 ListView
顯示 SQLite 資料的快速簡單方式。 主要限制是它只能系結數據行值來顯示控件,它不允許您變更數據列配置的其他層面(例如,顯示/隱藏控件或變更屬性)。
子類別化 CursorAdapter
子 CursorAdapter
類別具有與 SimpleCursorAdapter
顯示 SQLite 數據相同的效能優點,但它也可讓您完全控制每個數據列檢視的建立和配置。 實作CursorAdapter
與子類別BaseAdapter
化非常不同,因為它不會覆寫 GetView
、 GetItemId
Count
或this[]
索引器。
假設有一個可運作的 SQLite 資料庫,您只需要覆寫兩種方法來建立 CursorAdapter
子類別:
BindView – 指定檢視時,更新它以顯示所提供數據指標中的數據。
NewView – 當 需要顯示新檢視時
ListView
呼叫。CursorAdapter
將負責回收檢視(不同於GetView
一般配接器上的方法)。
先前範例中的配接器子類別具有傳回數據列數目和擷取目前專案的方法 – CursorAdapter
不需要這些方法,因為可以從數據指標本身擷取該資訊。 藉由將每個檢視的建立和母體分割成這兩種方法,強制執行 CursorAdapter
檢視重複使用。 這與一般配接器形成鮮明對比,因為可以忽略 convertView
方法的參數 BaseAdapter.GetView
。
實作 CursorAdapter
CursorTableAdapter/HomeScreenCursorAdapter.cs中的程式代碼包含CursorAdapter
子類別。 它會儲存傳遞至建構函式的內容參考,讓它可以在 方法中NewView
存取 LayoutInflater
。 完整的類別看起來像這樣:
public class HomeScreenCursorAdapter : CursorAdapter {
Activity context;
public HomeScreenCursorAdapter(Activity context, ICursor c)
: base(context, c)
{
this.context = context;
}
public override void BindView(View view, Context context, ICursor cursor)
{
var textView = view.FindViewById<TextView>(Android.Resource.Id.Text1);
textView.Text = cursor.GetString(1); // 'name' is column 1 in the cursor query
}
public override View NewView(Context context, ICursor cursor, ViewGroup parent)
{
return this.context.LayoutInflater.Inflate(Android.Resource.Layout.SimpleListItem1, parent, false);
}
}
指派 CursorAdapter
Activity
在 中,ListView
建立數據指標,然後將CursorAdapter
它指派給清單檢視。
在 CursorTableAdapter/HomeScreen.cs OnCreate
方法中執行此動作的程序代碼如下所示:
// create the cursor
vdb = new VegetableDatabase(this);
cursor = vdb.ReadableDatabase.RawQuery("SELECT * FROM vegetables", null);
StartManagingCursor(cursor);
// create the CursorAdapter
listView.Adapter = (IListAdapter)new HomeScreenCursorAdapter(this, cursor, false);
方法 OnDestroy
包含 StopManagingCursor
先前所述的方法呼叫。