这次不再是简单的数据,而是从数据库中取出数据,如果数据库中没有你要找的信息,会将用户输入的数据插入到数据库中,然后当用户再次输入的时候会出现刚刚输入的数据。
activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<AutoCompleteTextView
android:id="@+id/auto_complete"
android:layout_width="0dp"
android:layout_height="48dp"
android:layout_weight="1"
android:hint="请输入搜索内容"
android:textColorHint="#52EF5350"
android:textSize="18sp"
/>
<Button
android:id="@+id/search"
android:layout_width="wrap_content"
android:layout_height="45dp"
android:text="搜索"
android:textSize="18sp"
android:layout_marginLeft="5dp"
android:textColor="#ffffff"
android:background="#8BC34A"
/>
</LinearLayout>
MainActivity.java
package com.example.autocompletetest;
import java.util.ArrayList;
import java.util.List;
import android.app.Activity;
import android.app.AlertDialog;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;
import android.widget.Button;
public class MainActivity extends Activity {
private AutoCompleteTextView mAutoComplete;
private Button mSearch;
private MyOpenHelper mOpenHelper;
private SQLiteDatabase mDb;
private List<String> mData;// 存放数据的集合
private ArrayAdapter<String> mAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_main);
init();
initEvent();
}
private void init() {
mAutoComplete = (AutoCompleteTextView) findViewById(R.id.auto_complete);
mSearch = (Button) findViewById(R.id.search);
mOpenHelper = new MyOpenHelper(this, "book.db", null, 1);// 实例化数据库辅助类
mDb = mOpenHelper.getReadableDatabase();// 打开数据库
loadDataInAdapter();//
}
private void loadDataInAdapter() {
// 从数据库中读取数据
mData = readData();
// 加载适配器
mAdapter = new ArrayAdapter<String>(MainActivity.this,
android.R.layout.simple_list_item_1, mData);
// 为自动提示框设置适配器
mAutoComplete.setAdapter(mAdapter);
}
private List<String> readData() {
List<String> list = new ArrayList<String>();
String sql = "select * from book ";
Cursor cursor = mDb.rawQuery(sql, null);
while (cursor.moveToNext()) {
String word = cursor.getString(cursor.getColumnIndex("word"));// 从数据库中取出数据
list.add(word);// 将取出的数据添加到list集合中
}
return list;
}
// 后面button的点击事件,如果输入的没有你要找的数据,就把这条数据插到数据库中,下次在输入这个内容时,会自动提示
private void initEvent() {
mSearch.setOnClickListener(new OnClickListener() {
@SuppressWarnings("null")
@Override
public void onClick(View arg0) {
String inputString = mAutoComplete.getText().toString();// 获取输入的内容
// 如果为空,则提示用户输入关键字
if (inputString == null || "".equals(inputString.trim())) {
AlertDialog.Builder builder = new AlertDialog.Builder(
MainActivity.this);
builder.setTitle("温馨提示").setMessage("请输入关键字").create()
.show();
} else {
if (!mData.contains(inputString)) {// 如果数据库没有关键字,则把输入的信息存到数据中
mDb.execSQL("insert into book(word) values(?)",
new String[] { inputString });
loadDataInAdapter();
}
}
}
});
}
@Override
protected void onDestroy() {
if (mDb != null) {
mDb.close();// 关闭数据库
}
super.onDestroy();
}
}
MyOpenHelper.java
package com.example.autocompletetest;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteOpenHelper;
public class MyOpenHelper extends SQLiteOpenHelper {
private static final String BOOK_TABLE = "create table if not exists book"
+ " (_id integer primary key autoincrement, word text )";
private static final String sql = "insert into book (word) values(?)";
public MyOpenHelper(Context context, String name, CursorFactory factory,
int version) {
super(context, name, factory, version);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL(BOOK_TABLE);
db.execSQL(sql, new String[] { "Android 群英传 " });
db.execSQL(sql, new String[] { "Android开发艺术" });
db.execSQL(sql, new String[] { "Android Weekly" });
}
@Override
public void onUpgrade(SQLiteDatabase db, int arg1, int arg2) {
db.execSQL("drop table if exist book");
onCreate(db);
}
}