Problem with AutoCompleteTextView and SimpleCursorAdapter

by class_java » Tue, 03 Mar 2009 06:19:52 GMT


Sponsored Links
 Hi All,

I have a AutoCompleteTextView and I wont in the drop down list that
appears while typing to contain data from a database through
SimpleCursorAdapter. The drop down list appears, but there is not text
in it. When I click in the drop down item seems to work fine, the Text
is correctly shown in the AutoCompleteTextView. Please help...

Code samples:
***************************************************************************************************************
Activity:
 final AutoCompleteTextView editTxt = (AutoCompleteTextView)
findViewById(R.id.foodedittext);

        mDbHelper = new DbHelper(this);

        Cursor cursor = mDbHelper.fetchAll();
        startManagingCursor(cursor);
        int[] to = new int[]{android.R.layout.select_dialog_item};
        SimpleCursorAdapter cursAdapt = new SimpleCursorAdapter
(this,android.R.layout.simple_dropdown_item_1line,cursor,new String[]
{DbHelper.FOOD_NAME},to);
        cursAdapt.setCursorToStringConverter(new CursorConverter());
        editTxt.setAdapter(cursAdapt);
**********************************************************************
auto_complete.xml:

<?xml version="1.0" encoding="utf-8"?>

<TextView  xmlns:android=" http://schemas.android.com/apk/res/android" ;
        android:id="@+id/auto_complete"
    android:layout_height="wrap_content"
    android:layout_width="wrap_content"
 />

*************************************************************************
public class CursorConverter implements CursorToStringConverter{

        @Override
        public CharSequence convertToString(Cursor cursor) {
         String a = cursor.getString(1);
         return a;
        }

}
****************************************************************************

Thanks in advance...
          Evelina




--~--~---------~--~----~------------~-------~--~----~



Problem with AutoCompleteTextView and SimpleCursorAdapter

by Will » Wed, 04 Mar 2009 02:03:05 GMT


 I haven't done one of these with a SimpleCurosrAdapter, but I've
worked through it with a Cursor/CursorAdapter.  I just uploaded to the
Developers group what I think is the minimum code for an AutoComplete
with a Cursor/CursorAdapter.

It's here:  


Sponsored Links


Problem with AutoCompleteTextView and SimpleCursorAdapter

by Tseng » Wed, 04 Mar 2009 06:24:21 GMT


 ou don't have to create your own XML file if you're just want to
display a one-liner dropdown menu. Instead better to use the already
defined one in the android resources. Basically you can leave the
auto_complete.xml, as it's not used anyways in your code (unless you
want to design your own dropdown list views).

Basically there are two reasons:
1. You haven't set the color states correctly so the text color =
background color
or
2. You have selected a wrong id in the "to" parameters

In your example, the latter seems to be the case.


you're using an layout id as "to". This is wrong. You have to use the
ID of the textfield in which your data belongs.

For example you have to data fields in your cursor, lets say

new String[] { DbHelper.FOOD_NAME, DbHelper.FOOD_PRICE }

Now if you have an layout file, with 2 TextView elements called
foodname and foodprice, you'd have to make the "to" part as follows
new int[] {R.id.foodname, R.id.foodid} in exactly this order. First
element of from (DbHelper.FOOD_NAME) will be placed in the view with
the first ID from "to" (in this case: R.id.foodname).

Since you're using the predefined android layout for the dropdown list
(android.R.layout.simple_dropdown_item_1line), the (only) textview
item is called android.R.id.text1.

So changing
int[] to = new int[]{android.R.layout.select_dialog_item};
SimpleCursorAdapter cursAdapt = new SimpleCursorAdapter
(this,android.R.layout.simple_dropdown_item_1line,cursor,new String[]
{DbHelper.FOOD_NAME},to);

to

int[] to = new int[]{android.R.id.text1};
SimpleCursorAdapter cursAdapt = new SimpleCursorAdapter
(this,android.R.layout.simple_dropdown_item_1line,cursor,new String[]
{DbHelper.FOOD_NAME},to);

should do the trick. You HAVE to use android.R.id.text1, because this
is the id which was set in android sdks \res\layout
\simple_dropdown_item_1line.xml file.

This is btw the case for all default android layout files. They have
no fixed names, instead it's always android.R.id.text1 to
android.R.id.text3 or android.R.id.button1 to android.R.id.button3 in
case of buttons (i.e. in default Dialog layouts)


On Mar 2, 10:49pm, class_java <class_j...@yahoo.gr> wrote:
--~--~---------~--~----~------------~-------~--~----~



Other Threads

1. This post to track Android availability in countries

India  - G2's expected in April.

take care,
Muthu Ramadoss.

http://linkedin.com/in/tellibitz +91-9840348914
http://androidrocks.in - Android Consulting.







--~--~---------~--~----~------------~-------~--~----~

2. Learning from the iPhone app store

http://furbo.org/2008/12/09/ring-tone-apps/

http://cyrilgodefroy.com/blog/index.php/2009/02/of-appstore-and-bad-ideas/

http://www.techcrunch.com/2009/02/19/pinch-media-data-shows-the-average-shelf-life-of-an-iphone-app-is-less-than-30-days/

http://arstechnica.com/apple/news/2008/10/app-store-lessons-creating-demos-for-fun-and-profit.ars

http://www.dragthing.com/blog/?p=103

http://appcubby.com/blog/files/financial_realities.html

http://www.iphonesavior.com/2009/01/iphone-developer-quits-day-job-after-ishoot-hits-number-one.html

Discuss. :-)

The conclusions I draw so far are:

1) Don't expect instant success

2) Free versions help sales of the full product (this may apply less
with the return policy in the Android Market)

3) It's better to be lucky than good :-)

--~--~---------~--~----~------------~-------~--~----~

3. Google Ads for Android?

4. I really need help with Android and I guess to some degree Java.

5. getProviders always return empty

6. Any Android developers in San Francisco area?

7. Development environment setup fails on Mac OS