I was struggling with the same problem.   Thanks for your code
example.  I had to do a few tweaks to get it to work.

I don't think that "ASC" is a valid orderby.   It should be the name
of a field and then the ASC.  For example my table has a field called
"modified" which contains the time the record was written to.  My
order by is "modified DESC"..

I also had to specify a field position for theCursor.getString(), with
theCursor.getString(1), since in my projection it's column 1 that
contains the relevant field.

You'll notice that I specify "null" for the SimpleCursorAdapter()
cursor.  It doesn't seem to really need it since it's not even
filtering yet.

        public static final String DEFAULT_SORT_ORDER = "modified
DESC";

        private static final String[] PROJECTION = new String[] {
                History._ID, // 0
                History.IP, // 1
                History.BITS, // 2
        };

    public void onCreate(Bundle icicle) {
        super.onCreate(icicle);

        SimpleCursorAdapter adapter2 = new SimpleCursorAdapter(this,
                        android.R.layout.simple_dropdown_item_1line, null,
                new String[] { History.IP },
                new int[] { android.R.id.text1 });
        adapter2.setCursorToStringConverter(new HistoryCursorConverter
());
        adapter2.setFilterQueryProvider(new FilterQueryProvider() {
                public Cursor runQuery(CharSequence constraint) {
                        StringBuilder buffer = null;
                        String[] args = null;
                        if (constraint != null) {
                                buffer = new StringBuilder();
                                buffer.append("UPPER(");
                                buffer.append(PROJECTION[1]);
                                buffer.append(") GLOB ?");
                                String filter = 
constraint.toString().toUpperCase() + "*";
                                args = new String[] { filter };
                        }
                        return getContentResolver().query(mUri, PROJECTION,
                                buffer == null ? null : buffer.toString(),
                                args, History.DEFAULT_SORT_ORDER);
                }
        });
        AutoCompleteTextView ipField = (AutoCompleteTextView)
findViewById(R.id.ipaddress);
        ipField.setAdapter(adapter2);
    }

    public class HistoryCursorConverter implements
        CursorToStringConverter
    {
        public CharSequence convertToString(Cursor theCursor)
        {
                if (debug) Log.d(TAG,"convertToString()");
                // Return the first column of the database cursor
                String aColumnString = theCursor.getString(1);
                return aColumnString;
        }
    }

Randy

On Apr 14, 6:07 am, ppmoore <[email protected]> wrote:
> Hello,
>
> I am trying to connect a AutoCompleteTextView with aSimpleCursorAdapter, 
> which narrows down the selection of a list of
> words, when the user starts typing.
>
> Following some other posts here, I've come up with the following code,
> but it doesn't work. I defined a FilterQueryProvider runQuery() method
> for the SimpleCursor Adapter, but when I use the debugger in the code,
> the response from the ContentResolver.query((0 method call is null.
>
> I think the problem is that there is no relationship between the
> CONTENT_URI and the SQL database, but I can't be sure.
>
> Can someone please help?
>
> Paul
>
>                public static final String AUTHORITY =
> "com.A.android.examples";
>                public static final Uri CONTENT_URI = Uri.parse
> ("content://" + AUTHORITY +  "/sqlautocomplete");
>
>                <snip> code to fill the SQL Cursor from a list of words
> </snip>
>
>                 mContentResolver = this.getContentResolver();
>                 AutoCompleteTextView textView = (AutoCompleteTextView)
> findViewById(R.id.edit);
>
>                 mFrom = new String[] { DictionaryDbAdapter.KEY_ROWID};
>                 mTo = new int[] { android.R.id.text1 };
>                SimpleCursorAdapteradapter = newSimpleCursorAdapter
> ( this,
>                                         
> android.R.layout.simple_dropdown_item_1line,
>                                         aCursor, mFrom, mTo );
>
>                 // Define the CursorToString class
>                 adapter.setCursorToStringConverter(new
> DictionaryCursorConverter());
>
>                 // Define the SQL query
>                 adapter.setFilterQueryProvider(new FilterQueryProvider
> ()
>                 {
>                         @Override
>                         public Cursor runQuery(CharSequence
> constraint)
>                         {
>                           StringBuilder buffer = null;
>                           String[] args = null;
>                           if (constraint != null)
>                           {
>                             buffer = new StringBuilder();
>                             buffer.append("UPPER(");
>                             buffer.append(mFrom[0]);
>                             buffer.append(") GLOB ?");
>                             String filter = constraint.toString
> ().toUpperCase() + "*";
>                             args = new String[] { filter };
>                           }
>
>                           Cursor aResult = mContentResolver.query
> (CONTENT_URI,
>                                                        mFrom,
>                                                         buffer == null ? null 
> :
>                                                         buffer.toString(), 
> args,
>                                                         "ASC");
>                           return aResult;
>                         }
>                 } );
>
>                 // Connect the adapter to the auto-complete text view
>                 textView.setAdapter(adapter);
>
> I implemented the CursorToStringConverter class as follows:
>
>                 public class DictionaryCursorConverter implements
> CursorToStringConverter
>                 {
>                   @Override
>                  public CharSequence convertToString(Cursor
> theCursor)
>                  {
>                    // Return the first column of the database cursor
>                    String aColumnString = theCursor.getString();
>                    return aColumnString;
>                  }
>
> }
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to