AutocompleteTextView & CursorAdapter

Quickly share your Android Code Snippets here...

AutocompleteTextView & CursorAdapter

Postby ADNFX Mobile » Sun Mar 28, 2010 6:10 pm

Hope it will help some folks here.... I searched an eternity for this crazy thing! Populate an autocompletetextview with data from a sqlite database....

OnCreate:
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  
  2. final DBAdapter db = new DBAdapter(this);
  3.  
  4.  
  5.  
  6. final AutoCompleteTextView textView = (AutoCompleteTextView) findViewById(R.id.edit);
  7.  
  8. db.open();
  9.  
  10. Cursor suggestions = db.getSuggestions(capitalizeFirstLetter("%"));
  11.  
  12. mySuggestionsAdapter suggestionsList = new mySuggestionsAdapter(this, suggestions);
  13.  
  14. textView.setAdapter(suggestionsList);
  15.  
  16. db.close();
  17.  
  18.  
Parsed in 0.032 seconds, using GeSHi 1.0.8.4


Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  
  2.     class mySuggestionsAdapter extends CursorAdapter
  3.  
  4.     {
  5.  
  6.         public mySuggestionsAdapter(Context context, Cursor c)
  7.  
  8.         {
  9.  
  10.             super(context, c);
  11.  
  12.         }
  13.  
  14.  
  15.  
  16.         @Override
  17.  
  18.         public void bindView(View view, Context context, Cursor cursor)
  19.  
  20.         {
  21.  
  22.             int columnIndex = cursor.getColumnIndexOrThrow(DBAdapter.VILLE);
  23.  
  24.             ((TextView) view).setText(cursor.getString(columnIndex));
  25.  
  26.         }
  27.  
  28.  
  29.  
  30.         @Override
  31.  
  32.         public String convertToString(Cursor cursor)
  33.  
  34.         {
  35.  
  36.             int columnIndex = cursor.getColumnIndexOrThrow(DBAdapter.VILLE);
  37.  
  38.             return cursor.getString(columnIndex);
  39.  
  40.         }
  41.  
  42.  
  43.  
  44.         @Override
  45.  
  46.         public View newView(Context context, Cursor cursor, ViewGroup parent)
  47.  
  48.         {
  49.  
  50.             final LayoutInflater inflater = LayoutInflater.from(context);
  51.  
  52.             final TextView view = (TextView) inflater.inflate(android.R.layout.simple_dropdown_item_1line, parent, false);
  53.  
  54.             int columnIndex = cursor.getColumnIndexOrThrow(DBAdapter.VILLE);
  55.  
  56.             view.setText(cursor.getString(columnIndex));
  57.  
  58.             return view;
  59.  
  60.         }
  61.  
  62.  
  63.  
  64.         @Override
  65.  
  66.         public Cursor runQueryOnBackgroundThread(CharSequence constraint)
  67.  
  68.         {
  69.  
  70.                 final DBAdapter db = new DBAdapter(Distandroid.this);
  71.  
  72.                
  73.  
  74.                 if (constraint == null)
  75.  
  76.             {
  77.  
  78.                 db.open();    
  79.  
  80.                         Cursor cursorBackground = db.getSuggestions(capitalizeFirstLetter("%"));
  81.  
  82.                         db.close();
  83.  
  84.                        
  85.  
  86.                         return cursorBackground;
  87.  
  88.             }  
  89.  
  90.            
  91.  
  92.                 db.open();    
  93.  
  94.                 Cursor cursorBackground = db.getSuggestions(capitalizeFirstLetter(constraint.toString()));
  95.  
  96.                 db.close();
  97.  
  98.                
  99.  
  100.             return cursorBackground;
  101.  
  102.         }
  103.  
  104.     }
  105.  
  106.  
Parsed in 0.037 seconds, using GeSHi 1.0.8.4
ADNFX Mobile
Freshman
Freshman
 
Posts: 4
Joined: Sun Mar 28, 2010 5:57 pm

Top

Re: AutocompleteTextView & CursorAdapter

Postby clasie » Mon Dec 05, 2011 7:53 pm

God bless you, you've simply saved a bunch of my time! :)
clasie
Once Poster
Once Poster
 
Posts: 1
Joined: Mon Dec 05, 2011 7:51 pm

Re: AutocompleteTextView & CursorAdapter

Postby ADNFX Mobile » Mon Dec 05, 2011 7:58 pm

Glad to have been able to help you :)
ADNFX Mobile
Freshman
Freshman
 
Posts: 4
Joined: Sun Mar 28, 2010 5:57 pm

Re: AutocompleteTextView & CursorAdapter

Postby tejas » Sun Dec 18, 2011 6:41 pm

this is just to clarify.....
do this snippet work for the scenario such as if I've a database already created suppose example.db, so can I Access it in the same way after putting it autocompletetextview? and my example.db is in asset folder of my project.

will be grateful.
thank you!
tejas
Junior Developer
Junior Developer
 
Posts: 14
Joined: Fri Feb 18, 2011 6:33 pm

Top

Return to Code Snippets for Android

Who is online

Users browsing this forum: No registered users and 6 guests