2010-08-10 59 views
2

我正在開發一個Android食譜程序,其中有食譜的數據庫。我已創建的數據庫中它稱爲它具有下列字段「配方」的表:Android自動完成綁定問題

1 - _id(簡稱KEY_ID)
2 - 配方標題(簡稱KEY_TITLE)
3 - 數成分(被稱爲KEY_INGREDIENT)
5 - - 發球(簡稱KEY_NOSERV)
4的過程(稱爲KEY_PROCEDURE)

我已經成功地實施了顯示操作即我已完成列表視圖,其示出了所有的食譜標題,當我點擊標題時,它會顯示剩餘的字段通過從數據庫提取數據。在這種方法中,我可以輕鬆地綁定數據表單數據庫。我用於此顯示操作的數據綁定邏輯如下,運行良好。

Cursor c = rDbHelper.fetchAllRecipes(); 
     startManagingCursor(c); 

     String[] from = new String[] { RecipeDbAdapter.KEY_TITLE }; 
     int[] to = new int[] { R.id.text1 }; 

     SimpleCursorAdapter recps = 
      new SimpleCursorAdapter(this, R.layout.recipe_row, c, from, to); 

     setListAdapter(recps); 

現在我正在使用AutoComplete Widget實現搜索操作。自動完成應該只顯示只顯示配方標題。但在這我不知道如何綁定自動完成框中的數據。我嘗試了與我在顯示操作中做的相同的事情,但是應用程序被強制關閉。我已經附加了如下所述的xml和代碼示例。請讓我知道如何在此操作中對數據綁定到AutoComplete Widget。

RES /佈局文件名:recipe_auto_list_item.xml

<?xml version="1.0" encoding="utf-8"?> 
<TextView xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/textautocomplete" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:padding="10dp" 
    android:textSize="16sp" 
    android:textColor="#000"> 
</TextView> 

RES /佈局文件名:recipe_autocomplete.xml

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="horizontal" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:padding="5dp"> 
    <TextView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="RECIPE NAME:" /> 
    <AutoCompleteTextView android:id="@+id/autocomplete_recipe" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:layout_marginLeft="5dp"/> 
</LinearLayout> 

數據綁定我在搜索中使用的邏輯活動如下:

super.onCreate(savedInstanceState); 
setContentView(R.layout.recipe_autocomplete); 

AutoCompleteTextView textView = (AutoCompleteTextView) findViewById(R.id.autocomplete_recipe); 

Cursor c = rDbHelper.fetchAllRecipes(); 
     startManagingCursor(c); 

     String[] from = new String[] { RecipeDbAdapter.KEY_TITLE }; 
     int[] to = new int[] { R.id.textautocomplete }; 

     SimpleCursorAdapter cur = new SimpleCursorAdapter(this, R.layout.recipe_auto_list_item, c, from, to); 
textView.setAdapter(cur); 

fetchAllRecipes()函數提取配方表的所有字段。從那我需要提取只有標題和在自動填充小部件中使用。

當我嘗試啓動搜索操作時,我的應用程序被強制關閉。請使用什麼邏輯來進行數據綁定。

回答