我想问一下,是否有某种方法可以让 ParseObjects 在 Android 中搜索建议框。我已经成功创建了最近的查询建议并且效果很好。但是现在我需要在用户输入时从 Parse.com 放一个字符串集合 -show results,其中包含正在输入的查询。我有点绝望,因为我一直在寻找它一个星期,但没有结果。
你有什么想法,如何做到这一点?
我在 SearchActivity 中的普通 ParseQuery:
ParseQuery<Animal> squery = ParseQuery.getQuery(Animal.class);
squery.whereMatches("animal", query, "i");
squery.findInBackground(new FindCallback<Animal>() {
@Override
public void done(List<Animal> animals, ParseException error) {
if(animals != null){
nAdapter.clear();
for (int i = 0; i < animals.size(); i++) {
mProgressBar = (ProgressBar) findViewById(R.id.search_loading_animals);
mProgressBar.setVisibility(View.INVISIBLE);
nAdapter.add(animals.get(i));
}
}
}
});
我需要实现 ParseQuery 的 SuggestionAdapter:
public class SuggestionProvider extends ContentProvider {
private static final String TAG = "SuggestionProvider";
private static final int SEARCH_SUGGESTIONS = 1;
private static final UriMatcher sURLMatcher = new UriMatcher(
UriMatcher.NO_MATCH);
static {
sURLMatcher.addURI("*", SearchManager.SUGGEST_URI_PATH_QUERY,
SEARCH_SUGGESTIONS);
sURLMatcher.addURI("*", SearchManager.SUGGEST_URI_PATH_QUERY + "/*",
SEARCH_SUGGESTIONS);
}
private static final String[] COLUMNS = new String[] {
"_id",
SearchManager.SUGGEST_COLUMN_TEXT_1,
SearchManager.SUGGEST_COLUMN_INTENT_ACTION,
SearchManager.SUGGEST_COLUMN_QUERY
};
public SuggestionProvider() {
}
@Override
public boolean onCreate() {
return true;
}
@Override
public Cursor query(Uri url, String[] projectionIn, String selection,
String[] selectionArgs, String sort) {
int match = sURLMatcher.match(url);
switch (match) {
case SEARCH_SUGGESTIONS:
String query = url.getLastPathSegment();
MatrixCursor cursor = new MatrixCursor(COLUMNS);
String[] suffixes = { "", "a", " foo", "XXXXXXXXXXXXXXXXX" };
for (String suffix : suffixes) {
Toast.makeText(getContext(), "", Toast.LENGTH_SHORT).show();
addRow(cursor, query + suffix);
}
return cursor;
default:
throw new IllegalArgumentException("Unknown URL: " + url);
}
}
private void addRow(MatrixCursor cursor, String string) {
long id = cursor.getCount();
cursor.newRow().add(id).add(string).add(Intent.ACTION_SEARCH).add(string);
}
@Override
public String getType(Uri url) {
int match = sURLMatcher.match(url);
switch (match) {
case SEARCH_SUGGESTIONS:
return SearchManager.SUGGEST_MIME_TYPE;
default:
throw new IllegalArgumentException("Unknown URL: " + url);
}
}
@Override
public int update(Uri url, ContentValues values, String where, String[] whereArgs) {
throw new UnsupportedOperationException("update not supported");
}
@Override
public Uri insert(Uri url, ContentValues initialValues) {
throw new UnsupportedOperationException("insert not supported");
}
@Override
public int delete(Uri url, String where, String[] whereArgs) {
throw new UnsupportedOperationException("delete not supported");
}
}