在Android中将ParseObjects设置为自定义搜索建议的方法

时间:2014-02-13 11:24:58

标签: android android-actionbar android-contentprovider android-search search-suggestion

我想问一下,如果有一些方法可以在Android中将ParseObjects设置为搜索建议框。我已经成功创建了最近的查询建议,并且运行良好。但是现在我需要在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");
    }
}

0 个答案:

没有答案