How to use AutoCompleteTextView with SimpleCursorAdapter

by Paul Moore » Thu, 09 Apr 2009 05:02:54 GMT


Sponsored Links
 Hello,

I have an application that involves reading a text dictionary file with
about 7000 entries, and allowing the user to choose one of these. My first
approach was to allow the user to select one of these by using a
AutoCompleteTextView together with ArrayAdapter<String>. This works, but is
very slow (~10 seconds to update the word list each time the user enters a
character to narrow down the choice).

I was wondering how to rework it so that I could use a database using a
SimpleCursorAdapter instead. In other words, to hope that a SQL database
could provide a more efficient ordering and access for the elements.
Is this the best way to do it? I've also seen several references to
SQLiteDatabase, but don't know which one to use.

I found this code fragmant consisting of an extended SimpleAdapter.
 http://sacoskun.blogspot.com/2008/08...leadapter.html< ; http://sacoskun.blogspot.com/2008/08/autocompletetextview-with-simpleadapter.html> ;

Unfortunately, this is not a complete worked out example. Could someone
forward me a more complete example? I've found lots of code fragments, but
no code that shows how to initialise the SimpleCursorAdapter contents, or
how it works together with the AutoCompleteTextView.

Many thanks,
Paul

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



How to use AutoCompleteTextView with SimpleCursorAdapter

by Paul Moore » Fri, 10 Apr 2009 19:51:44 GMT


 lt;<Unfortunately, this is not a complete worked out example. Could someone
forward me a more complete example? I've found lots of code fragments, but
no code that shows how to initialise the SimpleCursorAdapter contents, or
how it works together with the AutoCompleteTextView. >>

<There is also an example in the APi demos with the
contacts.~/android-sdk-windows-1.1_r1/samples/ApiDemos/src/com/example/android/apis/view/AutoComplete4.java>

Many thanks.



That was very helpful.



I still have a question. I have now progressed to the point where list of
entries is being displayed on the auto-complete box. However, when I try to
narrow down the displayed choice by typing text into the auto-complete text
box, the displayed list remains the same. For example, if the list initially
contains aaa1 aaa2 abb1 abb2 and acc1and acc2
then when I type in ab I expect to see only abb1and abb2
Instead all entries are still displayed.



I found this very helpful message explaining that the problem is that
SimpleCursorAdapter does not implement filtering:

http://www.mail-archive.com/android-developers@googlegroups.com/msg00546.html



The runQuery() method of the FilterQueryProvider class has to be defined to
allow the class to filter what is displayed on the screen.



@Override

protected void onCreate(Bundle icicle) {

super.onCreate(icicle);

m_adapter = new SimpleCursorAdapter(this,

android.R.layout.simple_list_item_1,

m_cursor,

PROJECTION,

new int[] { android.R.id.text1 },

CONTENT_URI,

DEFAULT_SORT_ORDER);



m_adapter.setFilterQueryProvider(new

SimpleCursorAdapater.FilterQueryProvider()

{

@Override

protected Cursor runQuery(CharSequence constraint) {

StringBuilder buffer = null;

String[] args = null;

if (constraint != null) {

buffer = new StringBuilder();

buffer.append("UPPER(");

buffer.append(m_projection[0]);

buffer.append(") GLOB ?");

String filter =
constraint.toString().toUpperCase() + "*";

if (m_filterMode == FILTERMODE_CONTAINS) {

filter = "*" + filter;

}

args = new String[] { filter };

}



return m_contentResolver.query(CONTENT_URI,

PROJECTION,

buffer == null ? null :
buffer.toString(),

args,

m_sortOrder);

}

m_box = (AutoCompleteTextView)

findViewById(R.id.autocomplete);

m_box.setAdapter(m_adapter);



There are two problems:



1. I using the v1.1 DSK, and th


Sponsored Links


Other Threads

1. How to mute the mobile phone and how to let its speaker work?

Hi Al

I have searched the two problems in the google, but nothing
surprising.

Pls help me

thanks
Daniel

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

2. Bitmap recycle/createBitmap freeing memory properly?

Hi,

I am getting this exception in the beta SD which I never got on the m5
SDK - I have changed some logic so I am not 100% sure that it's not my
code that is causing this, but would you mind checking whether or not
Android properly frees all memory when Bitmap.recycle() is called and
also when a bitmap is cropped using the Bitmap.createBitmap() API and
later goes out of scope (i.e. can be garbage collected)?

Here is the stack trace for my issue:

D/dalvikvm(  414): GC freed 15290 objects / 1630400 bytes in 90ms
E/dalvikvm-heap(  414): 194184-byte external allocation too large for
this process.
E/        (  414): VM won't let us allocate 194184 bytes
D/AndroidRuntime(  414): Shutting down VM
W/dalvikvm(  414): threadid=3: thread exiting with uncaught exception
(group=0x40010e28)
E/AndroidRuntime(  414): Uncaught handler: thread main exiting due to
uncaught exception
E/AndroidRuntime(  414): java.lang.RuntimeException: can't alloc
pixels
E/AndroidRuntime(  414):        at
android.graphics.Bitmap.nativeCreate(Native Method)
E/AndroidRuntime(  414):        at
android.graphics.Bitmap.createBitmap(Bitmap.java:343)
E/AndroidRuntime(  414):        at
android.graphics.Bitmap.createBitmap(Bitmap.java:303)
E/AndroidRuntime(  414):        at
android.graphics.Bitmap.createBitmap(Bitmap.java:253)
E/AndroidRuntime(  414):        at
com.frogdesign.VideoHelper.cropBitmap(VideoHelper.java:580)

Thanks so much,
Cheryl
--~--~---------~--~----~------------~-------~--~----~

3. OpenIntents for SDK 0.9 - control API demos with the Sensor Simulator!

4. Abstract Classes and Layouts

5. porting from m5 to 0.9: app fails to start Activity

6. flash memory

7. Opportunity Collaboration