6

我正在从这个博客http://weblog.plexobject.com/?p=1689在 doSearchQuery 中实现搜索活动的自定义建议列表,但无法理解他做了什么。另一件事是,当我通过输入例如月份名称进行搜索并以“A”开头时,它会比较它并返回这些匹配结果 August、April 等,但在建议列表中仅显示“a”以及所有匹配结果,如“j”包含在一月、六月、七月。但在得到 j、j、j 的建议列表中只没有这个月份的名称。我错了或遗漏了我不理解的地方

这是我的 SearchActivity 代码

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    //setContentView(R.layout.search_activity);
    this.setDefaultKeyMode(Activity.DEFAULT_KEYS_SEARCH_LOCAL);

    final Intent queryIntent = getIntent();

    final String queryAction = queryIntent.getAction();
    if (Intent.ACTION_SEARCH.equals(queryAction)) {
        this.doSearchQuery(queryIntent);
    } else if (Intent.ACTION_VIEW.equals(queryAction)) {
        this.doView(queryIntent);
    } else {
        Log.d(TAG, "Create intent NOT from search");
    }

}

@Override
public void onNewIntent(final Intent queryIntent) {
    super.onNewIntent(queryIntent);
    final String queryAction = queryIntent.getAction();
    if (Intent.ACTION_SEARCH.equals(queryAction)) {
        this.doSearchQuery(queryIntent);
    } else if (Intent.ACTION_VIEW.equals(queryAction)) {
        this.doView(queryIntent);
    }
}
// here in this didn't under what he did one is getting intent and bundle but where he define it.
private void doSearchQuery(final Intent queryIntent) {
    String queryString = queryIntent.getDataString(); // from suggestions
    if (query == null) {
        query = intent.getStringExtra(SearchManager.QUERY); // from search-bar
    }

    // display results here
    bundle.putString("user_query", queryString);
    intent.setData(Uri.fromParts("", "", queryString));

    intent.setAction(Intent.ACTION_SEARCH);
    queryIntent.putExtras(bundle);
    startActivity(intent);
    Log.e("query string", "query string "+queryString);
}

private void doView(final Intent queryIntent) {
    Uri uri = queryIntent.getData();
    String action = queryIntent.getAction();
    Intent intent = new Intent(action);
    intent.setData(uri);
    startActivity(intent);
    this.finish();
}

这是我的提供者,我需要单行建议我评论第二列

public class MySuggestionProvider extends SearchRecentSuggestionsProvider {
    public final static String AUTHORITY = MySuggestionProvider.class.getName();
    public final static int MODE = DATABASE_MODE_QUERIES;
    private final static String TAG = MySuggestionProvider.class.getSimpleName();

    private static final String[] COLUMNS = {
            "_id", // must include this column
            SearchManager.SUGGEST_COLUMN_TEXT_1,
//          SearchManager.SUGGEST_COLUMN_TEXT_2,
            SearchManager.SUGGEST_COLUMN_INTENT_DATA,
            SearchManager.SUGGEST_COLUMN_INTENT_ACTION,
            SearchManager.SUGGEST_COLUMN_SHORTCUT_ID };

    public MySuggestionProvider() {
        setupSuggestions(AUTHORITY, MODE);
    }

    @Override
    public Cursor query(Uri uri, String[] projection, String selection,
            String[] selectionArgs, String sortOrder) {

        String query = selectionArgs[0];
        if (query == null || query.length() == 0) {
            return null;
        }

        MatrixCursor cursor = new MatrixCursor(COLUMNS);

        try {
            List<String> list = callmyservice(query);
            int n = 0;
            for (String obj : list) {
                cursor.addRow(createRow(Integer.valueOf(n), query, obj));
                n++;
            }
        } catch (Exception e) {
            Log.e(TAG, "Failed to lookup " + query, e);
        }
        return cursor;
    }

    @Override
    public Uri insert(Uri uri, ContentValues values) {
        throw new UnsupportedOperationException();
    }

    @Override
    public int delete(Uri uri, String selection, String[] selectionArgs) {
        throw new UnsupportedOperationException();
    }

    @Override
    public int update(Uri uri, ContentValues values, String selection,
            String[] selectionArgs) {
        throw new UnsupportedOperationException();
    }

    private Object[] createRow(Integer id, String text1,
            String name) {
        return new Object[] { id, // _id
                text1, // text1
                //text2, // text2
                text1, "android.intent.action.SEARCH", // action
                SearchManager.SUGGEST_NEVER_MAKE_SHORTCUT };
    }
    private static final String[] months = {"January", "February","march","April","may","june","july","August","September","octobor",
        "november","december"};
    List<String> ls2 = new ArrayList<String>();
    private List<String> callmyservice(String query){
        List<String> ls = new ArrayList<String>();

        for(int i=0;i<months.length;i++){
            if(months[i].toLowerCase().contains(query.toLowerCase())){
                //if(months[i].equalsIgnoreCase(query.toLowerCase())){
                ls.add(months[i]);
            }
        }

        ls2.clear();
        ls2.addAll(ls);

        return ls;
    }
}
4

1 回答 1

5

我找到了如何实施自定义建议的方法。对此进行一些更改,您可以在建议列表中实现搜索建议单行或双行。

对于单行建议,我先解释一下。

从问题代码中,我更改了一些并实现了单行建议列表。

从 COLUMN 对象更改此

private static final String[] COLUMNS = {
        "_id", // must include this column
        SearchManager.SUGGEST_COLUMN_TEXT_1,
        SearchManager.SUGGEST_COLUMN_INTENT_DATA,
        SearchManager.SUGGEST_COLUMN_INTENT_EXTRA_DATA,
        SearchManager.SUGGEST_COLUMN_SHORTCUT_ID };

现在在 createRow() 时间只需传递对象一个是 ID,另一个是 query() 方法中的建议列表值。

List<String> list = callmyservice(query);
int n = 0;
for (String obj : list) {
     cursor.addRow(createRow(Integer.valueOf(n),obj));
      n++;
}

现在像这样实现 createRow()。

private Object[] createRow(Integer id, String text1){
    return new Object[] { id, // _id
            text1, // text1
            text1, // data to sent back to activity and select
            text1, // data sent as extra data when select from suggestin list
            SearchManager.SUGGEST_NEVER_MAKE_SHORTCUT};
}

好的,这是实现此功能时的外观。

在此处输入图像描述

好的,现在为两行搜索建议列表创建这样的 COLUMN 对象

private static final String[] COLUMNS = {
        "_id", // must include this column
        SearchManager.SUGGEST_COLUMN_TEXT_1,
        SearchManager.SUGGEST_COLUMN_TEXT_2,
        SearchManager.SUGGEST_COLUMN_INTENT_DATA,
        SearchManager.SUGGEST_COLUMN_INTENT_EXTRA_DATA,
        SearchManager.SUGGEST_COLUMN_INTENT_ACTION,
        SearchManager.SUGGEST_COLUMN_SHORTCUT_ID };

现在实现 createRow() 方法并像这样从 query() 调用 // query() 方法 List list = callmyservice(query); 诠释 n = 0; for (String obj : list) { // 这里查询是您当前输入的文本 for search for text_1 // obj 是与此查询一起找到的匹配列表作为结果并显示为 text_2 cursor.addRow(createRow(Integer.valueOf(n) , 查询, obj)); n++; }

private Object[] createRow(Integer id, String text1, String name) {
    return new Object[] { id, // _id
            text1, // text1
            text2, // text2
            text1, // data to be sent when select from list as query
            text2, // data to be sent as extra string when select from list as result
            "android.intent.action.SEARCH", // action
            SearchManager.SUGGEST_NEVER_MAKE_SHORTCUT };
}

这是两行搜索建议列表的视图

在此处输入图像描述

好的,希望这对您有所帮助。

于 2012-09-28T10:21:15.110 回答