好吧,我花了我的时间。我从 中提出自己的简单建议实现。SQLiteDatabase
我们将创建 3 个类,如下所示
-
MainActivity
- 用于测试数据库中的建议SearchView
-
SuggestionDatabase
- 这将存储您最近的搜索关键字。
-
SuggestionSimpleCursorAdapter
- 这是 的子类。我将解释为什么我创建这个类而不是使用.SimpleCursorAdapter
SimpleCursorAdapter
代码
// MainActivity.java
public class MainActivity
extends Activity
implements SearchView.OnQueryTextListener,
SearchView.OnSuggestionListener
{
private SuggestionsDatabase database;
private SearchView searchView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
database = new SuggestionsDatabase(this);
searchView = (SearchView) findViewById(R.id.searchView1);
searchView.setOnQueryTextListener(this);
searchView.setOnSuggestionListener(this);
}
@Override
public boolean onSuggestionSelect(int position) {
return false;
}
@Override
public boolean onSuggestionClick(int position) {
SQLiteCursor cursor = (SQLiteCursor) searchView.getSuggestionsAdapter().getItem(position);
int indexColumnSuggestion = cursor.getColumnIndex( SuggestionsDatabase.FIELD_SUGGESTION);
searchView.setQuery(cursor.getString(indexColumnSuggestion), false);
return true;
}
@Override
public boolean onQueryTextSubmit(String query) {
long result = database.insertSuggestion(query);
return result != -1;
}
@Override
public boolean onQueryTextChange(String newText) {
Cursor cursor = database.getSuggestions(newText);
if(cursor.getCount() != 0)
{
String[] columns = new String[] {SuggestionsDatabase.FIELD_SUGGESTION };
int[] columnTextId = new int[] { android.R.id.text1};
SuggestionSimpleCursorAdapter simple = new SuggestionSimpleCursorAdapter(getBaseContext(),
android.R.layout.simple_list_item_1, cursor,
columns , columnTextId
, 0);
searchView.setSuggestionsAdapter(simple);
return true;
}
else
{
return false;
}
}
}
它是如何运作的
- 当用户点击搜索按钮时,将触发,然后搜索关键字将保存在我们的数据库中。假设我们提交了关键字“Hello”
onQueryTextSubmit()
- 如果用户在中写入一个字符串,例如“Hel”或“H”将被调用,然后我们在()中搜索此关键字。如果“Hel”或“H”与“Hello”匹配,则通过在 中设置返回的光标,然后在 中设置此适配器来显示查询结果。这是图片。
SearchView
onQueryTextChange()
SQLiteDatabase
SuggestionDatabase
SuggestionSimpleCursorAdapter
SearchView
3.当然,我们将点击“你好”的建议,将被调用。我们从 的适配器 () 获取对象并从中获取建议文本,在对象中设置建议文本onSuggestionClick(int position)
SQLiteCursor
SearchView
SuggestionSimpleCursorAdapter
SearchView
SQLiteCursor cursor = (SQLiteCursor) searchView.getSuggestionsAdapter().getItem(position);
int indexColumnSuggestion = cursor.getColumnIndex( SuggestionsDatabase.FIELD_SUGGESTION);
searchView.setQuery(cursor.getString(indexColumnSuggestion), false);
如果我们使用它也可以正常工作,但假设我们有这种情况SimpleCursorAdapter
- 如果我们在智能手机中运行此程序并键入关键字“Hel”,则建议将正确显示。
- 如果我们以横向方式旋转屏幕怎么办?它将在“全屏”模式下切换,您仍然可以键入关键字。
建议中会发生什么?让我们来看看。
看到奇怪的建议了吗?我们如何解决这个问题?通过覆盖 what 返回convertToString(Cursor cursor)
CharSequence
// SuggestionSimpleCursorAdapter.java
public class SuggestionSimpleCursorAdapter
extends SimpleCursorAdapter
{
public SuggestionSimpleCursorAdapter(Context context, int layout, Cursor c,
String[] from, int[] to) {
super(context, layout, c, from, to);
}
public SuggestionSimpleCursorAdapter(Context context, int layout, Cursor c,
String[] from, int[] to, int flags) {
super(context, layout, c, from, to, flags);
}
@Override
public CharSequence convertToString(Cursor cursor) {
int indexColumnSuggestion = cursor.getColumnIndex(SuggestionsDatabase.FIELD_SUGGESTION);
return cursor.getString(indexColumnSuggestion);
}
}
通过覆盖,这是结果convertToString(Cursor cursor)
这是数据库
// SuggestionDatabase.java
public class SuggestionsDatabase {
public static final String DB_SUGGESTION = "SUGGESTION_DB";
public final static String TABLE_SUGGESTION = "SUGGESTION_TB";
public final static String FIELD_ID = "_id";
public final static String FIELD_SUGGESTION = "suggestion";
private SQLiteDatabase db;
private Helper helper;
public SuggestionsDatabase(Context context) {
helper = new Helper(context, DB_SUGGESTION, null, 1);
db = helper.getWritableDatabase();
}
public long insertSuggestion(String text)
{
ContentValues values = new ContentValues();
values.put(FIELD_SUGGESTION, text);
return db.insert(TABLE_SUGGESTION, null, values);
}
public Cursor getSuggestions(String text)
{
return db.query(TABLE_SUGGESTION, new String[] {FIELD_ID, FIELD_SUGGESTION},
FIELD_SUGGESTION+" LIKE '"+ text +"%'", null, null, null, null);
}
private class Helper extends SQLiteOpenHelper
{
public Helper(Context context, String name, CursorFactory factory,
int version) {
super(context, name, factory, version);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("CREATE TABLE "+TABLE_SUGGESTION+" ("+
FIELD_ID+" integer primary key autoincrement, "+FIELD_SUGGESTION+" text);");
Log.d("SUGGESTION", "DB CREATED");
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}
}
}
我希望这个答案对其他程序员很有用。:)