2010-10-19 41 views
1

我剛剛開始與Android和我嘗試​​我的第一個測試應用程序,但我有點卡住了。Android從SQLite綁定到圖庫

我有一個SQLite數據庫,並且用戶正在選擇一張圖片和一個對此的引用,並且一個簡短的描述被存儲在數據庫中。 這一切都工作正常,現在我想要將圖像從數據庫綁定到一個畫廊,但我有點困惑,我如何做到這一點。

我的佈局XML如下:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout 
xmlns:android="http://schemas.android.com/apk/res/android" 
android:orientation="vertical" 
android:layout_width="fill_parent" 
android:layout_height="fill_parent"> 

    <Gallery 
     xmlns:android="http://schemas.android.com/apk/res/android" 
     android:id="@+id/Weeksgallery"  
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content"/> 

<Button android:id="@+id/ok"  
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_below="@+id/text1"  
     android:layout_marginLeft="10dip"  
     android:text="Take a Picture" 
     android:onClick="TakePicture" /> 
</LinearLayout> 

我想用一個按鈕下方

我在走廊上創建功能我有以下幾點:

public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 

    mDbHelper = new DbAdapter(this); 
    mDbHelper.open(); 
    Cursor cur = mDbHelper.fetchDaysImages(); 

    //array of fields to display 
    String[] from = new String[] { mDbHelper.KEY_ROWID,mDbHelper.KEY_PICREF}; 

    int[] to = new int[] { R.id.Weeksgallery }; 

    // Now create a simple cursor adapter and set it to display 
    SimpleCursorAdapter images = new SimpleCursorAdapter(this, 
    R.layout.main, cur, from, to); 
    setListAdapter(images); 

    mDbHelper.close(); 
} 

我現在用的是記事本教程爲例,我的課程擴展了ListActivity。

當我運行它,我得到的錯誤

您的內容必須有一個ListView其id屬性爲「android.R.id.list」

我假設我使用錯誤的綁定類型,但我似乎無法找到任何告訴我如何從sqlite數據庫綁定到圖庫的地方。 我也想在圖庫中添加對rowId的引用,所以當圖片被點擊時它會打開相關頁面(所以它可以顯示消息)

任何人都可以幫忙嗎?

感謝

貝克斯

+0

這些CAPS真的讓我覺得更傾向於幫助.... – 2010-10-19 09:39:34

+0

對不起,他們在那裏使它突出,所以有人會看到它,也許應該用粗體代替..現在已經刪除它們擺弄,並得到文本格式正確 – Bex 2010-10-19 09:40:34

回答

0

你的活動繼承ListActivity,對不對?當你使用ListActivity的時候,你必須確保在Activity的佈局中有一個ListView的id是「android.R.id.list」,這就是日誌告訴你的。

如果Activity中沒有ListView,則不要使用ListActivity。如果您要將適配器設置爲圖庫,請使用圖庫的setAdapter方法。

+0

我已經更新了類,現在我得到了一個不同的錯誤「android.widget.Gallery不是可以由此SimpleCursorAdapter綁定的視圖」錯誤是否意味着我無法使用simplecursoradapter綁定它,還是我在simplecursoradapter中做錯了什麼?我也需要以某種方式使用hello圖庫教程中使用的imageadapter類嗎? – Bex 2010-10-19 12:39:18

+0

我不認爲你可以在這裏使用SimpleCursorAdapter。因爲SimpleCursorAdapter是一個將遊標的列映射到TextViews或ImageViews的適配器。你可以實現你自己的適配器來存檔這個。試試吧,不會太難。 – Tony 2010-10-19 14:00:37

+0

嗯...我會給它之前,你有什麼指針,因爲我對Android(和Java)非常陌生,所以我仍然在與最基本的東西苦苦掙扎!我原以爲會有一個教程在某處,因爲我曾想過,我正在嘗試的是很常見的。 – Bex 2010-10-19 14:37:01