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. Content Management Application in android

HI all,
I need to an application with following .
The application should be Generic one.ie.,
By supplying data,layout,images and all resources in a single XML
file,then, the application should dynamically change its layout and
functionality and it should display according to the layout specified
in the xml file.

It should generate the application to the supplied xml 's data
dynamically even its layout also ie.,header,footer,data everything.

Typically i need to develop a  GENERIC/ COMMON SKELETON for all
android applications.
ie.,as we see Blogging in GOOGLE or IGOOGLE where User customizes his
site and layout and content as he likes.I need to develop an
application ,similar to the  kind of application in android.
i want to know ,
How to start the application and What to do intially for this
application.
can anybody knows about this concept,please let me know your ideas.




-- 

2. ExpandableListView indicator over my text

I'm using an ExpandableListView in one of my activity in a
RelativeLayout

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/
android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
        ...
        <Button
                android:id="@+id/searchNearBtnSearch"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/search"
                android:layout_centerHorizontal="true"
            android:layout_below="@id/searchNearSpinner"
            android:background="@drawable/btn_and_showtime"
                />
        <ExpandableListView
       android:id="@+id/searchNearListResult"
       android:layout_width="fill_parent"
       android:layout_height="fill_parent"
            android:layout_below="@id/searchNearBtnSearch"
       />
</RelativeLayout>

and I'm using a custom expandableListAdaptaer which group view is :

public TextView getGenericView() {
                // Layout parameters for the ExpandableListView
                AbsListView.LayoutParams lp = new AbsListView.LayoutParams( //
                                ViewGroup.LayoutParams.FILL_PARENT, 64);

                TextView textView = new TextView(mainContext);
                textView.setLayoutParams(lp);
                // Center the text vertically
                textView.setGravity(Gravity.CENTER_VERTICAL | Gravity.LEFT);
                // Set the text starting position
                textView.setPadding(36, 0, 0, 0);
                return textView;
        }

(Found on examples of API_Demos, ExpandableList1)

A user of my application report me this photo
http://binomed-android-project.googlecode.com/issues/attachment?aid=-2990980965246303896&name=AndroidShowtime.jpg
As you can see, the indicator of group items is over the text of group
item despite the padding of 36...

And if I'm running emulator WVGA800 or an emulator with "abstracted
LCD density" to 240 I have the same problem, but with my phone (HTC
G1), I don't have any problem.

What is strange is that I try the application API_Demo with an
emulator WVGA800 or an emulator with "abstracted LCD density" to 240
and I go to ExpandableList1 activity and all is alright !

I also try to copy the inner class available in ExpandableList1 for
adapter and I launch it into my activity and I also have the problem
see on photo


What I am doing wrong... ? I am forced to manage density into my
adapters in order to adjust the padding ?

-- 

3. mohon bantuan, pusing soal DREAM

4. Is there an explorer for all contentProviders?

5. what is envsetup.sh

6. internal compiler error: Segmentation fault (Eclair + 4.3.1 toolchain)

7. Accents in strings