AutoComplete examples in Android

Source: Internet
Author: User
Summarize the knowledge points of AutoComplete and SQLite in Android.

1 SQLite help class

Public class sqlitecountryassistant extends sqliteopenhelper {

Private Static final string db_name = "usingsqlite. DB ";

Private Static final int db_version_number = 1;

Private Static final string db_table_name = "countries ";

Private Static final string db_column_1_name = "country_name ";

Private Static final string db_create_script = "create table"

+ Db_table_name

+ "(_ Id integer primary key autoincrement, country_name text not null );)";

Private sqlitedatabase sqlitedbinstance = NULL;

Public sqlitecountryassistant (context ){

Super (context, db_name, null, db_version_number );

}

@ Override

Public void onupgrade (sqlitedatabase dB, int oldversion, int newversion ){

// Todo: Implement onupgrade

}

@ Override

Public void oncreate (sqlitedatabase sqlitedbinstance ){

Log. I ("oncreate", "creating the database ...");

Sqlitedbinstance.exe csql (db_create_script );

}

Public void opendb () throws sqlexception {

Log. I ("opendb", "Checking sqlitedbinstance ...");

If (this. sqlitedbinstance = NULL ){

Log. I ("opendb", "creating sqlitedbinstance ...");

This. sqlitedbinstance = This. getwritabledatabase ();

}

}

Public void closedb (){

If (this. sqlitedbinstance! = NULL ){

If (this. sqlitedbinstance. isopen ())

This. sqlitedbinstance. Close ();

}

}

Public long insertcountry (string countryname ){

Contentvalues = new contentvalues ();

Contentvalues. Put (db_column_1_name, countryname );

Log

. I (this. tostring () + "-insertcountry", "inserting :"

+ Countryname );

Return this. sqlitedbinstance. insert (db_table_name, null, contentvalues );

}

Public Boolean removecountry (string countryname ){

Int result = This. sqlitedbinstance. Delete (db_table_name,

"Country_name = '" + countryname + "'", null );

If (result> 0)

Return true;

Else

Return false;

}

Public long updatecountry (string oldcountryname, string newcountryname ){

Contentvalues = new contentvalues ();

Contentvalues. Put (db_column_1_name, newcountryname );

Return this. sqlitedbinstance. Update (db_table_name, contentvalues,

"Country_name = '" + oldcountryname + "'", null );

}

Public String [] getallcountries (){

Cursor cursor = This. sqlitedbinstance

. Query (db_table_name, new string [] {db_column_1_name}, null,

Null, null );

If (cursor. getcount ()> 0 ){

String [] STR = new string [cursor. getcount ()];

Int I = 0;

While (cursor. movetonext ()){

STR [I] = cursor. getstring (cursor

. Getcolumnindex (db_column_1_name ));

I ++;

}

Return STR;

} Else {

Return New String [] {};

}

}

2 AutoComplete class used

Public class usingsqlite extends Activity

{

Private sqlitecountryassistant sqllitecountryassistant;

@ Override

Public void oncreate (bundle savedinstancestate)

{

Super. oncreate (savedinstancestate );

Setcontentview (R. layout. Main );

Final autocompletetextview textview = (autocompletetextview) findviewbyid (R. Id. autocompletecountry );

Sqllitecountryassistant = new sqlitecountryassistant (usingsqlite. This );

Sqllitecountryassistant. opendb ();

// Insert a few countries that begin with "C"

Sqllitecountryassistant. insertcountry ("Cambodia ");

Sqllitecountryassistant. insertcountry ("Cameroon ");

Sqllitecountryassistant. insertcountry ("Canada ");

Sqllitecountryassistant. insertcountry ("Cape Verde ");

Sqllitecountryassistant. insertcountry ("Cayman Islands ");

Sqllitecountryassistant. insertcountry ("Chad ");

Sqllitecountryassistant. insertcountry ("Chile ");

Sqllitecountryassistant. insertcountry ("China ");

// Sqllitecountryassistant. removecountry ("Chad ");

// Sqllitecountryassistant. updatecountry ("Canada", "Costa Rica ");

String [] countries = sqllitecountryassistant. getallcountries ();

// Print out the values to the log

For (INT I = 0; I <countries. length; I ++)

{

Log. I (this. tostring (), countries [I]);

}

Arrayadapter <string> adapter = new arrayadapter <string> (this, R. layout. list_item, countries );

Textview. setadapter (adapter );

}

Public void ondestroy ()

{

Super. ondestroy ();

Sqllitecountryassistant. Close ();

}

}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.