From the previous example (Android auto-completion tutorial), we can see that auto-completion is simple,
Let's go deeper today. The string provided by ArrayAdapter is queried from the database and the MultiAutoCompleteTextView control is used.
The biggest difference between this control and AutoCompleteTextView is that multiple words can be added and the name can be known.
The effect is as follows. Separate words with commas.
First
The layout is the same as in the previous example.
Create an XML file named list_item.xml and save it in the res/layout/folder. Edit the file as follows:
<?xml version="1.0" encoding="utf-8"?> <TextView xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:padding="10dp" android:textSize="16sp" android:textColor="#000"> </TextView>
This file defines a simple TextView to display each item in the prompt list.
Open the res/layout/main. xml file and add the following content:
<?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" > <TextView android:id="@+id/tv" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/hello" /><MultiAutoCompleteTextView android:id="@+id/mactv" android:layout_width="fill_parent" android:layout_height="wrap_content" /></LinearLayout>
Next we will design my database named "person". To create a person table, there are two fields: name and gender.
Create a new SQLiteHelper class inherited from SQLiteOpenHelper:
Package com. linc. autosqlite. dao; import android. content. context; import android. database. cursor; import android. database. sqlite. SQLiteDatabase; import android. database. sqlite. SQLiteOpenHelper;/*** create, update, and change a table column name * @ author lincyang **/public class SQLiteHelper extends SQLiteOpenHelper {public static final String DB_NAME = "person "; public static final int DB_VERSION = 1; protected static Context ctx; /// Constructor 1: Pass context // public SQLiteHelper (Context context) {super (context, DB_NAME, null, DB_VERSION); ctx = context ;} /// constructor 2 // public SQLiteHelper () {super (ctx, DB_NAME, null, DB_VERSION) ;}@ Overridepublic void onCreate (SQLiteDatabase db) {String SQL = "create table person (name varchar (20) not null," + "gender varchar (10) not null);" mongodb.exe cSQL (SQL );} @ Overridepublic void onUpgrade (SQLiteDatabase db , Int oldVersion, int newVersion) {// TODO Auto-generated method stub} protected void closeCursor (Cursor cursor) {if (cursor! = Null) {cursor. close ();}}}
In this way, the database is created during the construction, and the database table is also created during onCreate.
I have created another class AutoSqliteDAO to handle database-related logic and transactions:
Package com. linc. autosqlite. dao; import java. util. arrayList; import android. content. contentValues; import android. content. context; import android. database. cursor; import android. database. sqlite. SQLiteDatabase; import android. util. log; import com. linc. autosqlite. meta. person; public class AutoSqliteDAO extends SQLiteHelper {public AutoSqliteDAO (Context context) {super (context) ;}/ *** use contentValues to insert data * This is The unique method of ndroid. If you do not have the SQL experience to directly learn Android, * This method is suitable for you * @ param name * @ param gender */public void insertPerson (String name, string gender) {SQLiteDatabase db = getReadableDatabase (); ContentValues cv = new ContentValues (); cv. put ("name", name); cv. put ("gender", gender); db. insert ("person", null, cv); db. close ();}/*** use SQL statements to insert data. Pay attention to the placeholder usage. * In fact, both methods are good. If you are good at using SQL statements, if you have experience in this area, use this method * @ param name * @ param gend Er */public void insertPerson2 (String name, String gender) {SQLiteDatabase db = getReadableDatabase (); String SQL = "INSERT INTO person (name, gender) VALUES (?,?) "Mongodb.exe cSQL (SQL, new Object [] {name, gender}); db. close ();} public void deletePerson (String name) {SQLiteDatabase db = getReadableDatabase (); String whereClause = "name =? "; // The deleted condition String [] whereArgs = {name}; // The deleted condition parameter db. delete ("user", whereClause, whereArgs); db. close ();} public void deletePerson2 (String name) {SQLiteDatabase db = getReadableDatabase (); String SQL = "DELETE FROM person WHERE name =? "Mongodb.exe cSQL (SQL, new Object [] {name}); // execute the SQL statement db. close ();} public void updatePerson (String name, String gender) {SQLiteDatabase db = getReadableDatabase (); ContentValues cv = new ContentValues (); cv. put ("gender", gender); String whereClause = "name =? "; String [] whereArgs = {name}; db. update ("user", cv, whereClause, whereArgs); db. close ();} public void updatePerson2 (String name, String gender) {SQLiteDatabase db = getReadableDatabase (); String SQL = "UPDATE [person] SET gender =? WHERE name =? "Mongodb.exe cSQL (SQL, new Object [] {gender, name}); db. close ();}/*** there are many query parameters. What I listed below should be easy to understand: * table: the first is the table name * columns: the second is the name of the column to be queried, an array. * Selection: the third is the condition. Use? Placeholder * selectionArgs: the fourth is the condition parameter, array * groupBy: The fifth is the group * having: The sixth is having. filter the groups meeting the condition * orderBy: the seventh option is to sort by increment or decrease * limit: the eighth option is to restrict the region of the returned record * @ param name * @ return */public ArrayList <Person> queryPerson (String name) {Cursor cursor = null; ArrayList <Person> personList = new ArrayList <Person> (); try {SQLiteDatabase db = getReadableDatabase (); cursor = db. query ("person", new String [] {"name", "gender"}, "name =? ", New String [] {name}, null, null); Person person; while (cursor. moveToNext () {String personName = cursor. getString (0); String personGender = cursor. getString (1); person = new Person (personName, personGender); personList. add (person);} db. close ();} catch (Exception ex) {Log. I ("linc", "queryPerson ------ ex is" + ex. getMessage ();} finally {closeCursor (cursor);} return personList;} public RrayList <String> queryPerson () {Cursor cursor = null; ArrayList <String> nameList = new ArrayList <String> (); String SQL = "SELECT * FROM person "; try {SQLiteDatabase db = getReadableDatabase (); cursor = db. rawQuery (SQL, null); while (cursor. moveToNext () {String personName = cursor. getString (0); nameList. add (personName);} db. close ();} catch (Exception ex) {Log. I ("linc", "queryPerson ------ ex is "+ Ex. getMessage ();} finally {closeCursor (cursor);} return nameList;} public ArrayList <Person> queryPerson2 (String name) {Cursor cursor = null; arrayList <Person> personList = new ArrayList <Person> (); String SQL = "SELECT * FROM person WHERE name =? "; Try {SQLiteDatabase db = getReadableDatabase (); cursor = db. rawQuery (SQL, new String [] {name}); Person person Person; while (cursor. moveToNext () {String personName = cursor. getString (0); String personGender = cursor. getString (1); person = new Person (personName, personGender); personList. add (person);} db. close ();} catch (Exception ex) {Log. I ("linc", "queryPerson2 ------ ex is" + ex. getMessage ();} fin Ally {closeCursor (cursor);} return personList;} public ArrayList <Person> queryPerson3 (String name) {Cursor cursor = null; arrayList <Person> personList = new ArrayList <Person> (); String SQL = "SELECT * FROM person WHERE name LIKE? "; Try {SQLiteDatabase db = getReadableDatabase (); cursor = db. rawQuery (SQL, new String [] {name}); Person person Person; while (cursor. moveToNext () {String personName = cursor. getString (0); String personGender = cursor. getString (1); person = new Person (personName, personGender); personList. add (person);} db. close ();} catch (Exception ex) {Log. I ("linc", "queryPerson3 ------ ex is" + ex. getMessage () ;}finally {closeCursor (cursor) ;}return personList ;}}
Finally, when I insert several pieces of data at the beginning of the Activity, it will be automatically completed when I input the data in the text box, just like.
The Activity code is as follows:
Package com. linc. autosqlite; import java. util. arrayList; import android. app. activity; import android. OS. bundle; import android. util. log; import android. widget. arrayAdapter; import android. widget. multiAutoCompleteTextView; import com. linc. autosqlite. dao. autoSqliteDAO; public class AutoSqliteTestActivity extends Activity {private MultiAutoCompleteTextView mactv; AutoSqliteDAO dao;/** Called when the activity is first created. * // @ Override public void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. main); mactv = (MultiAutoCompleteTextView) findViewById (R. id. mactv); dao = new AutoSqliteDAO (this); init (); ArrayList <String> nameList = dao. queryPerson (); Log. I ("linc", "list count is" + nameList. size (); ArrayAdapter <String> adapter = new ArrayAdapter <String> (this, android. r. layout. simple_dropdown_item_1line, nameList); mactv. setAdapter (adapter); mactv. setThreshold (1); // starts from the first character and is optional. setTokenizer (new MultiAutoCompleteTextView. commaTokenizer (); // required delimiter} private void init () {dao. insertPerson ("Zhang Fei", "male"); dao. insertPerson ("Liu Bei", "male"); dao. insertPerson ("Sun shangxiang", "female"); dao. insertPerson ("Guan Yu", "male"); dao. insertPerson ("liubei", "male"); dao. insertPerson ("dongzhuo", "male"); dao. insertPerson ("yuanshao", "male ");}}
How is it? Is it easy?