|
小结下android中的autocomplete和sqlite的知识点,顺便复习下。
1 SQLITE帮助类
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 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.execSQL(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 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 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, null, null);
if (cursor.getCount() > 0) {
String[] str = new String[cursor.getCount()];
int i = 0;
while (cursor.moveToNext()) {
str = cursor.getString(cursor
.getColumnIndex(DB_COLUMN_1_NAME));
i++;
}
return str;
} else {
return new String[] {};
}
}
复制代码2 使用的autocomplete类
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);
}
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, R.layout.list_item, countries);
textView.setAdapter(adapter);
}
public void onDestroy()
{
super.onDestroy();
sqlliteCountryAssistant.close();
}
}
复制代码 |
|