Java学习者论坛

 找回密码
 立即注册

QQ登录

只需一步,快速开始

手机号码,快捷登录

恭喜Java学习者论坛(https://www.javaxxz.com)已经为数万Java学习者服务超过8年了!积累会员资料超过10000G+
成为本站VIP会员,下载本站10000G+会员资源,购买链接:点击进入购买VIP会员
JAVA高级面试进阶视频教程Java架构师系统进阶VIP课程

分布式高可用全栈开发微服务教程

Go语言视频零基础入门到精通

Java架构师3期(课件+源码)

Java开发全终端实战租房项目视频教程

SpringBoot2.X入门到高级使用教程

大数据培训第六期全套视频教程

深度学习(CNN RNN GAN)算法原理

Java亿级流量电商系统视频教程

互联网架构师视频教程

年薪50万Spark2.0从入门到精通

年薪50万!人工智能学习路线教程

年薪50万!大数据从入门到精通学习路线年薪50万!机器学习入门到精通视频教程
仿小米商城类app和小程序视频教程深度学习数据分析基础到实战最新黑马javaEE2.1就业课程从 0到JVM实战高手教程 MySQL入门到精通教程
查看: 527|回复: 0

开发交流:android中的autocomplete例子小结

[复制链接]

该用户从未签到

发表于 2011-10-24 09:16:30 | 显示全部楼层 |阅读模式
小结下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();

}

}
复制代码
回复

使用道具 举报

该用户从未签到

发表于 2011-10-24 09:16:33 | 显示全部楼层

Re:开发交

Android实现ListView异步加载图片
回复 支持 反对

使用道具 举报

该用户从未签到

发表于 2011-10-24 09:16:36 | 显示全部楼层

Re:开发交

Android实现ListView异步加载图片
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|手机版|Java学习者论坛 ( 声明:本站资料整理自互联网,用于Java学习者交流学习使用,对资料版权不负任何法律责任,若有侵权请及时联系客服屏蔽删除 )

GMT+8, 2025-1-10 18:53 , Processed in 0.367444 second(s), 33 queries .

Powered by Discuz! X3.4

© 2001-2017 Comsenz Inc.

快速回复 返回顶部 返回列表