package test.nan.autocomplete;
import android.app.Activity;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AutoCompleteTextView;
import android.widget.CursorAdapter;
import android.widget.Filterable;
import android.widget.TextView;
public class AutocompleteTest extends Activity {
private SQLiteDatabase sqlite = null;
private static final String database = "yourDb";
private String[] columns;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
AutoCompleteTextView actv = (AutoCompleteTextView)findViewById(R.id.actv);
actv.setThreshold(2);
//Make sure to create a new database everytime
this.deleteDatabase(database);
sqlite = this.openOrCreateDatabase(database, 0, null);
this.createDatas();
columns = new String[]{"name", "_id"};
MyCursorAdpter myCursorAdapter = new MyCursorAdpter(this, getCursor(), 0);
actv.setAdapter(myCursorAdapter);
}
private void createDatas() {
String createTable = "create table test (name varchar(255), _id varchar(255))";
sqlite.execSQL(createTable);
ContentValues values = new ContentValues();
values.put("name", "nicole");
values.put("_id", "0");
sqlite.insert("test", null, values);
values.put("name", "nicolas");
values.put("_id", "1");
sqlite.insert("test", null, values);
values.put("name", "jean");
values.put("_id", "2");
sqlite.insert("test", null, values);
values.put("name", "jennyfer");
values.put("_id", "3");
sqlite.insert("test", null, values);
}
public Cursor getCursor() {
return sqlite.query("test", columns, null, null, null, null, null);
}
private class MyCursorAdpter extends CursorAdapter {
private int columnIndex;
public MyCursorAdpter(Context context, Cursor c, int col) {
super(context, c);
this.columnIndex = col;
}
@Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
final LayoutInflater inflater = LayoutInflater.from(context);
final TextView view = (TextView) inflater.inflate(
android.R.layout.simple_dropdown_item_1line, parent, false);
return view;
}
@Override
public void bindView(View view, Context context, Cursor cursor) {
((TextView) view).setText(cursor.getString(columnIndex));
}
@Override
public String convertToString(Cursor cursor) {
Log.i("info", " convertToString ");
return cursor.getString(columnIndex);
}
@Override
public Cursor runQueryOnBackgroundThread(CharSequence constraint) {
if (constraint != null) {
String selection = "name like \'" + constraint.toString() +"%\'";
return sqlite.query("test", columns, selection, null, null, null, null);
}
else {
return null;
}
}
}
}
没有合适的资源?快使用搜索试试~ 我知道了~
Android--开发--AutocompleteTest.rar
共23个文件
class:8个
ds_store:4个
xml:3个
需积分: 5 0 下载量 107 浏览量
2024-04-10
20:16:15
上传
评论
收藏 32KB RAR 举报
温馨提示
Android--开发--AutocompleteTest
资源推荐
资源详情
资源评论
收起资源包目录
Android--开发--AutocompleteTest.rar (23个子文件)
Android--开发--AutocompleteTest
AutocompleteTest
AutocompleteTest
.DS_Store 6KB
.classpath 232B
assets
src
test
nan
autocomplete
AutocompleteTest.java 3KB
R.java 710B
res
values
strings.xml 182B
layout
main.xml 385B
drawable
icon.png 3KB
bin
.DS_Store 6KB
resources.ap_ 6KB
classes.dex 5KB
test
nan
autocomplete
R$string.class 436B
R$layout.class 403B
R$id.class 391B
AutocompleteTest$MyCursorAdpter.class 3KB
R$attr.class 349B
R$drawable.class 409B
R.class 552B
AutocompleteTest.class 3KB
AutocompleteTest.apk 10KB
.project 819B
AndroidManifest.xml 649B
__MACOSX
AutocompleteTest
bin
._.DS_Store 82B
._.DS_Store 82B
共 23 条
- 1
资源评论
温柔-的-女汉子
- 粉丝: 964
- 资源: 4009
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功