开发者

Android AutoComplete Binding Question

I'm developing an Android Recipe program which has database of food recipes. I have a created a database within it a table called "recipe" which has the following fields:

1 - _id(referred to as KEY_ID)

2 - recipe title(referred to as KEY_TITLE)

3 - number of serves(referred to as KEY_NOSERV)

4 - ingredients(referred to as KEY_INGREDIENT)

5 - procedure(referred to as KEY_PROCEDURE)

I have successfully implemented the display operation i.e I have done a listview which shows all the recipe titles and when I click on the title it shows me the remaining fields by extracting data from the database. In this method I could easily bind the data form database. The data binding logic which I used for this display operation is as follows which worked well.

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);

Right now I'm implementing the Search operation by using the AutoComplete Widget. The AutoComplete should show show only the recipe title. But in this I don't know how to bind the data in the AutoComplete box. I tried doing the same thing as I did in display operation but the app gets force closed. I have attached the xml and code samples as mentioned below. Please let me know as to how to do data binding to the AutoComplete Widget in this opearation.

res/layout File name : recipe_auto_list_item.xml

<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/开发者_Go百科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/layout File Name: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>

The data binding logic which I used in the search activity is as follows:

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);

The fetchAllRecipes() functions extracts all the fields of the recipe table. from that I need to extract only the title and use in the autocomplete widget.

When I try to start the search operation my app gets forced closed. Kindly what logic should I use for data binding.


Why don't you try using the Search feature withing Android?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜