This document describes code for an Android autocomplete text view that is populated from a database. It includes 5 code files: an XML layout file, a main activity class, a custom autocomplete view class, a database handler class, and a text changed listener class. The main activity initializes the autocomplete text view, inserts sample data into the database, and provides a method to retrieve autocomplete suggestions from the database based on the search term. The custom autocomplete view disables built-in filtering and captures selection changes. The database handler class handles creating the database and tables, inserting, updating, and querying data.
This document describes code for an Android autocomplete text view that is populated from a database. It includes 5 code files: an XML layout file, a main activity class, a custom autocomplete view class, a database handler class, and a text changed listener class. The main activity initializes the autocomplete text view, inserts sample data into the database, and provides a method to retrieve autocomplete suggestions from the database based on the search term. The custom autocomplete view disables built-in filtering and captures selection changes. The database handler class handles creating the database and tables, inserting, updating, and querying data.
This document describes code for an Android autocomplete text view that is populated from a database. It includes 5 code files: an XML layout file, a main activity class, a custom autocomplete view class, a database handler class, and a text changed listener class. The main activity initializes the autocomplete text view, inserts sample data into the database, and provides a method to retrieve autocomplete suggestions from the database based on the search term. The custom autocomplete view disables built-in filtering and captures selection changes. The database handler class handles creating the database and tables, inserting, updating, and querying data.
0 Android AutocompleteTextView with Database Video Demo
2.0 Android AutoComplete EditText Program Files 3.0 Android AutocompleteTextView with Database Example Codes 3.1 to 3.6 Main Files Description with Codes Explained via Comments 1.0 Android AutocompleteTextView with Database Video Demo Visualizing our code output is important so heres a video I shoot for you to show the codes final output. 2.0 Android AutoComplete EditText Program Files Our example project for today contains only 5 java files. Click the file names below to read its purpose and see the code inside. 1. activity_main.xml 2. MainActivity.java 3. CustomAutoCompleteView.java 4. DatabaseHandler.java 5. CustomAutoCompleteTextChangedListener.java 6. MyObject.java 3.0 Android AutocompleteTextView with Database Example Codes 3.1 activity_main.xml see how the AutoCompleteTextView widget was put in the XML layout. The tag looks like com.example.autocompletetextviewdb.CustomAutoCompleteView because it was referenced to our CustomAutoComputeView.java file. <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" >
</LinearLayout> 3.2 MainActivity.java this is where we initialize everything and insert sample data to the database. package com.example.autocompletetextviewdb;
/* * Change to type CustomAutoCompleteView instead of AutoCompleteTextView * since we are extending to customize the view and disable filter * The same with the XML view, type will be CustomAutoCompleteView */ CustomAutoCompleteView myAutoComplete;
// adapter for auto-complete ArrayAdapter<String> myAdapter;
// for database operations DatabaseHandler databaseH;
// just to add some initial value String[] item = new String[] {"Please search..."};
// instantiate database handler databaseH = new DatabaseHandler(MainActivity.this);
// put sample data to database insertSampleData();
// autocompletetextview is in activity_main.xml myAutoComplete = (CustomAutoCompleteView) findViewById(R.id.myautocomplete);
// add the listener so it will tries to suggest while the user types myAutoComplete.addTextChangedListener(new CustomAutoCompleteTextChangedListener(this));
// set our adapter myAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_dropdown_item_1line, item); myAutoComplete.setAdapter(myAdapter);
// CREATE databaseH.create( new MyObject("January") ); databaseH.create( new MyObject("February") ); databaseH.create( new MyObject("March") ); databaseH.create( new MyObject("April") ); databaseH.create( new MyObject("May") ); databaseH.create( new MyObject("June") ); databaseH.create( new MyObject("July") ); databaseH.create( new MyObject("August") ); databaseH.create( new MyObject("September") ); databaseH.create( new MyObject("October") ); databaseH.create( new MyObject("November") ); databaseH.create( new MyObject("December") ); databaseH.create( new MyObject("New Caledonia") ); databaseH.create( new MyObject("New Zealand") ); databaseH.create( new MyObject("Papua New Guinea") ); databaseH.create( new MyObject("COFFEE-1K") ); databaseH.create( new MyObject("coffee raw") ); databaseH.create( new MyObject("authentic COFFEE") ); databaseH.create( new MyObject("k12-coffee") ); databaseH.create( new MyObject("view coffee") ); databaseH.create( new MyObject("Indian-coffee-two") );
}
// this function is used in CustomAutoCompleteTextChangedListener.java public String[] getItemsFromDb(String searchTerm){
// add items on the array dynamically List<MyObject> products = databaseH.read(searchTerm); int rowCount = products.size();
String[] item = new String[rowCount]; int x = 0;
for (MyObject record : products) {
item[x] = record.objectName; x++; }
return item; }
} 3.3 CustomAutoCompleteView.java since we want the AutocompleteTextView to work with a database, we must customize by extending it. package com.example.autocompletetextviewdb;
public class CustomAutoCompleteView extends AutoCompleteTextView {
public CustomAutoCompleteView(Context context) { super(context); // TODO Auto-generated constructor stub }
public CustomAutoCompleteView(Context context, AttributeSet attrs) { super(context, attrs); // TODO Auto-generated constructor stub }
public CustomAutoCompleteView(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); // TODO Auto-generated constructor stub }
// this is how to disable AutoCompleteTextView filter @Override protected void performFiltering(final CharSequence text, final int keyCode) { String filterText = ""; super.performFiltering(filterText, keyCode); }
/* * after a selection we have to capture the new value and append to the existing text */ @Override protected void replaceText(final CharSequence text) { super.replaceText(text); }
} 3.4 DatabaseHandler.java as the name suggests, this is where we can create the database, tables, manipulate and query the data. package com.example.autocompletetextviewdb;
// When upgrading the database, it will drop the current table and recreate. @Override public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// Read records related to the search term public List<MyObject> read(String searchTerm) {
List<MyObject> recordsList = new ArrayList<MyObject>();
// select query String sql = ""; sql += "SELECT * FROM " + tableName; sql += " WHERE " + fieldObjectName + " LIKE '%" + searchTerm + "%'"; sql += " ORDER BY " + fieldObjectId + " DESC"; sql += " LIMIT 0,5";
SQLiteDatabase db = this.getWritableDatabase();
// execute the query Cursor cursor = db.rawQuery(sql, null);
// looping through all rows and adding to list if (cursor.moveToFirst()) { do {
// int productId = Integer.parseInt(cursor.getString(cursor.getColumnIndex(fieldProductId))); String objectName = cursor.getString(cursor.getColumnIndex(fieldObjectName)); MyObject myObject = new MyObject(objectName);
// add to list recordsList.add(myObject);
} while (cursor.moveToNext()); }
cursor.close(); db.close();
// return the list of records return recordsList; }
} 3.5 CustomAutoCompleteTextChangedListener.java this is where the program requeries the database each time a user types a character on the AutocompleteTextView. package com.example.autocompletetextviewdb;
// query the database based on the user input mainActivity.item = mainActivity.getItemsFromDb(userInput.toString());
// update the adapater mainActivity.myAdapter.notifyDataSetChanged(); mainActivity.myAdapter = new ArrayAdapter<String>(mainActivity, android.R.layout.simple_dropdown_item_1line, mainActivity.item); mainActivity.myAutoComplete.setAdapter(mainActivity.myAdapter);
}
} 3.6 MyObject.java this is used for inserting sample data and other operations related to the database. package com.example.autocompletetextviewdb;
public class MyObject {
public String objectName;
// constructor for adding sample data public MyObject(String objectName){
this.objectName = objectName; }
} On the next post, well have this code with a custom ArrayAdapter so you can style the drop down that appears, see you! Feel free to drop a comment on this Android AutocompleteTextView with database example.