AutoCompleteTextView and SimpleCursorAdapter

Put your problem here if it does not fit any of the other categories.

AutoCompleteTextView and SimpleCursorAdapter

Postby ppmoore » Tue Apr 14, 2009 2:28 pm

Hello,

I am trying to connect a AutoCompleteTextView with a
SimpleCursorAdapter, 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?

Many thanks, Paul

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  
  2. public static final String AUTHORITY = "com.A.android.examples";
  3.  
  4. public static final Uri CONTENT_URI = Uri.parse("content://" + AUTHORITY +  "/sqlautocomplete");
  5.  
  6.  
  7.  
  8. <snip> code to fill the SQL Cursor from a list of words </snip>
  9.  
  10.  
  11.  
  12. mContentResolver = this.getContentResolver();
  13.  
  14. AutoCompleteTextView textView = (AutoCompleteTextView)
  15.  
  16. findViewById(R.id.edit);
  17.  
  18.  
  19.  
  20. mFrom = new String[] { DictionaryDbAdapter.KEY_ROWID};
  21.  
  22. mTo = new int[] { android.R.id.text1 };
  23.  
  24. SimpleCursorAdapter adapter = new SimpleCursorAdapter( this,
  25.  
  26.                                         android.R.layout.simple_dropdown_item_1line,
  27.  
  28.                                         aCursor, mFrom, mTo );
  29.  
  30.  
  31.  
  32. // Define the CursorToString class
  33.  
  34. adapter.setCursorToStringConverter(newDictionaryCursorConverter());
  35.  
  36.  
  37.  
  38. // Define the SQL query
  39.  
  40. adapter.setFilterQueryProvider(new FilterQueryProvider()
  41.  
  42. {
  43.  
  44.   @Override
  45.  
  46.   public Cursor runQuery(CharSequence constraint)
  47.  
  48.   {
  49.  
  50.     StringBuilder buffer = null;
  51.  
  52.     String[] args = null;
  53.  
  54.     if (constraint != null)
  55.  
  56.     {
  57.  
  58.       buffer = new StringBuilder();
  59.  
  60.       buffer.append("UPPER(");
  61.  
  62.       buffer.append(mFrom[0]);
  63.  
  64.       buffer.append(") GLOB ?");
  65.  
  66.       String filter = constraint.toString().toUpperCase() + "*";
  67.  
  68.       args = new String[] { filter };
  69.  
  70.     }
  71.  
  72.  
  73.  
  74.     Cursor aResult = mContentResolver.query(CONTENT_URI,
  75.  
  76.                                                        mFrom,
  77.  
  78.                                                         buffer == null ? null :
  79.  
  80.                                                         buffer.toString(), args,
  81.  
  82.                                                         "ASC");
  83.  
  84.     return aResult;
  85.  
  86.   }
  87.  
  88. } );
  89.  
  90.  
  91.  
  92. // Connect the adapter to the auto-complete text view
  93.  
  94. textView.setAdapter(adapter);
Parsed in 0.038 seconds, using GeSHi 1.0.8.4


I implemented the CursorToStringConverter class as follows:

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. public class DictionaryCursorConverter implements CursorToStringConverter
  2.  
  3. {
  4.  
  5.   @Override
  6.  
  7.   public CharSequence convertToString(Cursor theCursor)
  8.  
  9.   {
  10.  
  11.     // Return the first column of the database cursor
  12.  
  13.     String aColumnString = theCursor.getString();
  14.  
  15.     return aColumnString;
  16.  
  17.   }
  18.  
  19. }
Parsed in 0.031 seconds, using GeSHi 1.0.8.4
ppmoore
Freshman
Freshman
 
Posts: 9
Joined: Thu Apr 02, 2009 5:16 pm

Top

Return to Other Coding-Problems

Who is online

Users browsing this forum: Google [Bot] and 23 guests