我自己解決了它:
我創建了自定義CursorAdapter。使用我的Custom CursorAdapter的對象,我得到了期望的結果。
public static class MessageAdapter extends CursorAdapter implements Filterable
{
DatabaseHelper dbHelper;
public MessageAdapter(Context context, Cursor c)
{
super(context, c);
dbHelper=new DatabaseHelper(context);
dbHelper.open();
mContent = context.getContentResolver();
}
@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);
view.setText(cursor.getString(cursor.getColumnIndex(DatabaseHelper.KEY_MEANING)));
return view;
}
@Override
public void bindView(View view, Context context, Cursor cursor)
{
((TextView) view).setText(cursor.getString(cursor.getColumnIndex(DatabaseHelper.KEY_MEANING)));
}
@Override
public String convertToString(Cursor cursor) {
return cursor.getString(cursor.getColumnIndex(DatabaseHelper.KEY_MEANING));
}
@Override
public Cursor runQueryOnBackgroundThread(CharSequence constraint)
{
if (getFilterQueryProvider() != null)
{
return getFilterQueryProvider().runQuery(constraint);
}
StringBuilder buffer = null;
String args = null;
if (constraint != null)
{
args=constraint.toString();
}
Cursor newCursor=dbHelper.getMeaning(args);
//You have to return results based on the above cursor results. So query whatver you want and return this cursor back.
return newCursor;
}
private ContentResolver mContent;
}
對於我已經創建getMeaning()方法在我DatabaseHelper類返回MultiAutoCompleteTextView基於字符串所需的結果簡單性:
在DBHelper類,
public Cursor getMeaning(String searchString)
{
Cursor cursor=sqdb.query(MY_DB, new String[]{KEY_ID,KEY_MEANING}, KEY_SEARCH+" like '"+searchString.toUpperCase()+"%'", null,null, null, null);
return cursor;
}
參考:http://hello-android.blogspot.com/2011/06/using-autocompletetextview-with-sqlite.html
感謝您的快速響應。我已經知道你的邏輯。但我不想這樣走。因爲它會不必要地識別其他具有「ty」的項目,例如/// –