Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/200.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/cmake/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
实现搜索活动android的自定义建议_Android_Searchactivity - Fatal编程技术网

实现搜索活动android的自定义建议

实现搜索活动android的自定义建议,android,searchactivity,Android,Searchactivity,我正在doSearchQuery中实现此博客中搜索活动的自定义建议列表。我无法理解他做了什么。另一件事是,当我通过键入例如月份名称并以“A”开头进行搜索时,它会将其进行比较并返回8月、4月等的匹配结果。但在建议列表中,仅显示“A”,其中包含1月、6月、7月的所有匹配结果,如“j”。但在建议列表中得到的j,j,j只不过不是这个月的名字。我错在哪里,或者错过了一些我不理解的东西 这是我的搜索活动代码 @Override public void onCreate(Bundle savedInstanc

我正在doSearchQuery中实现此博客中搜索活动的自定义建议列表。我无法理解他做了什么。另一件事是,当我通过键入例如月份名称并以“A”开头进行搜索时,它会将其进行比较并返回8月、4月等的匹配结果。但在建议列表中,仅显示“A”,其中包含1月、6月、7月的所有匹配结果,如“j”。但在建议列表中得到的j,j,j只不过不是这个月的名字。我错在哪里,或者错过了一些我不理解的东西

这是我的搜索活动代码

@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;
    }
}
公共类MySuggestionProvider扩展了SearchRecentSuggestionProvider{
public final static String AUTHORITY=MySuggestionProvider.class.getName();
公共最终静态int模式=数据库模式查询;
private final static String TAG=MySuggestionProvider.class.getSimpleName();
私有静态最终字符串[]列={
“\u id”,//必须包含此列
SearchManager.SUGGEST\u COLUMN\u TEXT\u 1,
//SearchManager.SUGGEST\u COLUMN\u TEXT\u 2,
SearchManager.SUGGEST\u COLUMN\u INTENT\u数据,
SearchManager.SUGGEST\u COLUMN\u INTENT\u ACTION,
SearchManager.SUGGEST\u COLUMN\u SHORTCUT\u ID};
公共MySuggestionProvider(){
建议(权限、方式);
}
@凌驾
公共游标查询(Uri、字符串[]投影、字符串选择、,
字符串[]selectionArgs,字符串排序器){
字符串查询=selectionArgs[0];
if(query==null | | query.length()==0){
返回null;
}
MatrixCursor=新的MatrixCursor(列);
试一试{
List List=callmyservice(查询);
int n=0;
用于(字符串对象:列表){
addRow(createRow(Integer.valueOf(n),query,obj));
n++;
}
}捕获(例外e){
Log.e(标记“查找失败”+查询,e);
}
返回光标;
}
@凌驾
公共Uri插入(Uri、ContentValues){
抛出新的UnsupportedOperationException();
}
@凌驾
public int delete(Uri、字符串选择、字符串[]selectionArgs){
抛出新的UnsupportedOperationException();
}
@凌驾
公共int更新(Uri、ContentValues、字符串选择、,
字符串[]selectionArgs){
抛出新的UnsupportedOperationException();
}
私有对象[]createRow(整数id,字符串text1,
字符串名称){
返回新对象[]{id,//\u id
text1,//text1
//text2,//text2
text1,“android.intent.action.SEARCH”//action
SearchManager.SUGGEST\u NEVER\u MAKE\u SHORTCUT};
}
私有静态最终字符串[]月={“一月”、“二月”、“三月”、“四月”、“五月”、“六月”、“七月”、“八月”、“九月”、“十月”,
“11月”、“12月”};
List ls2=新的ArrayList();
私有列表callmyservice(字符串查询){
List ls=新的ArrayList();

对于(inti=0;i我找到了如何实现自定义建议的方法。在这方面稍作改动,您可以在建议列表中实现搜索建议单行或双线

对于这一条建议,我先解释一下

从问题代码开始,我做了一些修改,并实现了针对单行建议列表的修改

从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()时间中,只需在query()方法中传递对象,一个是ID,另一个是建议列表值

好的,这是一个快照,当你实现它时,它是什么样子的

现在确定两行搜索建议列表,创建如下列对象

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(查询); int n=0; 用于(字符串对象:列表){ //这里的查询是您当前键入的文本,用于搜索文本\u 1 //obj是通过此查询找到的匹配列表,作为文本_2的结果和显示 addRow(createRow(Integer.valueOf(n),query,obj)); n++; }

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


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

对不起,这里的“callmyservice”是什么,我无法回答understand@kaplanfat这意味着数据来自于您实现的东西,不管上面的代码如何工作,唯一重要的部分是您从某处获取一个
列表。
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};
}
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 };
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 };
}