Android的搜索词典样品code错了吗? [英] Android searchable dictionary sample code wrong?

查看:176
本文介绍了Android的搜索词典样品code错了吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我发现有趣的bug的 Android的检索字典示例应用程序

按照以下步骤重现:


  • 在搜索框中键入博

  • 点击进入

  • 单击从结果假

  • 当你看到结果,并定义,再次调用搜索

  • 键入COA,这应该给你的联盟

  • 相反的是,没有任何反应...

但这种神奇的作品:


  • 在搜索框中键入博

  • 选择从定制的建议名单假

  • 当你看到结果,并定义,再次调用搜索

  • 键入COA,这应该给你的联盟

  • 联合是真的找到了。

我想问你,为什么会出现这种情况,如何修复它,所以它会正常工作。我很厌倦了,因为我试图整天来解决这个问题。

在此先感谢,
彼得。

下面是关于主题的一些code,我宁愿发布更多...

 公共类SearchableDictionary延伸活动{私人TextView的mTextView;
私人的ListView mListView;@覆盖
公共无效的onCreate(捆绑savedInstanceState){
    super.onCreate(savedInstanceState);
    的setContentView(R.layout.main);    mTextView =(的TextView)findViewById(R.id.text);
    mListView =(ListView控件)findViewById(R.id.list);    handleIntent(getIntent());
}@覆盖
保护无效onNewIntent(意向意图){
    //因为这一活动已经设置launchMode =singleTop,系统调用这个方法
    //传递的意图,如果这个活动是目前前景活动时
    //再次调用(当用户执行从这一活动的搜索,我们不创建
    //这个活动的一个新实例,因此系统在这里提供的搜索意图)
    handleIntent(意向);
}私人无效handleIntent(意向意图){
    如果(Intent.ACTION_VIEW.equals(intent.getAction())){
        //处理上搜索建议的点击;推出活动以显示字
        意图wordIntent =新意图(这一点,WordActivity.class);
        wordIntent.setData(intent.getData());
        startActivity(wordIntent);
    }否则如果(Intent.ACTION_SEARCH.equals(intent.getAction())){
        //处理搜索查询
        查询字符串= intent.getStringExtra(SearchManager.QUERY);
        showResults(查询);
    }
}/ **
 *搜索给定的查询字典,并显示结果。
 * @参数查询的搜索查询
 * /
私人无效showResults(查询字符串){    光标光标= managedQuery(DictionaryProvider.CONTENT_URI,NULL,NULL,
                            新的String [] {查询},NULL);    如果(光标== NULL){
        //没有结果
        mTextView.setText(的getString(R.string.no_results,新的对象[] {查询}));
    }其他{
        //显示的结果数
        诠释计数= cursor.getCount();
        字符串countString = getResources()。getQuantityString(R.plurals.search_results,
                                算,新的对象[] {算,查询});
        mTextView.setText(countString);        //指定列,我们希望在结果中显示
        的String [] =由新的String [] {DictionaryDatabase.KEY_WORD,
                                       DictionaryDatabase.KEY_DEFINITION};        //指定我们想要的列去相应的布局元素
        INT []为= INT新[] {R.id.word,
                               R.id.definition};        //创建用于定义一个简单的游标适配器,并将其应用到ListView
        SimpleCursorAdapter字=新SimpleCursorAdapter(这一点,
                                      R.layout.result,光标,从,到);
        mListView.setAdapter(字);        //定义上单击侦听器列表项
        mListView.setOnItemClickListener(新OnItemClickListener(){            @覆盖
            公共无效onItemClick(适配器视图<>母公司,观景,INT位置,长的id){
                //建立用于打开WordActivity与特定词的URI意向
                意图wordIntent =新意图(getApplicationContext(),WordActivity.class);
                乌里数据= Uri.withAppendedPath(DictionaryProvider.CONTENT_URI,
                                                将String.valueOf(ID));
                wordIntent.setData(数据);
                startActivity(wordIntent);
            }
        });
    }
}@覆盖
公共布尔onCreateOptionsMenu(菜单菜单){
    MenuInflater吹气= getMenuInflater();
    inflater.inflate(R.menu.options_menu,菜单);    如果(Build.VERSION.SDK_INT> = Build.VERSION_ codeS.HONEYCOMB){
        SearchManager的SearchManager的=(SearchManager的)getSystemService(Context.SEARCH_SERVICE);
        搜索查看搜索查看=(搜索查看)menu.findItem(R.id.search).getActionView();
        sea​​rchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
        sea​​rchView.setIconifiedByDefault(假);
    }    返回true;
}@覆盖
公共布尔onOptionsItemSelected(菜单项项){
    开关(item.getItemId()){
        案例R.id.search:
            onSearchRequested();
            返回true;
        默认:
            返回false;
    }
}
}

和本次活动presents结果:

 公共类WordActivity延伸活动{
@覆盖
保护无效的onCreate(捆绑savedInstanceState){
    super.onCreate(savedInstanceState);
    的setContentView(R.layout.word);    如果(Build.VERSION.SDK_INT> = Build.VERSION_ codeS.HONEYCOMB){
        动作条动作条= getActionBar();
        actionBar.setDisplayHomeAsUpEnabled(真);
    }    。URI URI = getIntent()的getData();
    光标光标= managedQuery(URI,NULL,NULL,NULL,NULL);    如果(光标== NULL){
        完();
    }其他{
        cursor.moveToFirst();        TextView中字=(的TextView)findViewById(R.id.word);
        TextView的定义=(的TextView)findViewById(R.id.definition);        INT WINDEX = cursor.getColumnIndexOrThrow(DictionaryDatabase.KEY_WORD);
        INT dIndex = cursor.getColumnIndexOrThrow(DictionaryDatabase.KEY_DEFINITION);        word.setText(cursor.getString(WINDEX));
        definition.setText(cursor.getString(dIndex));
    }
}@覆盖
公共布尔onCreateOptionsMenu(菜单菜单){
    MenuInflater吹气= getMenuInflater();
    inflater.inflate(R.menu.options_menu,菜单);    如果(Build.VERSION.SDK_INT> = Build.VERSION_ codeS.HONEYCOMB){
        SearchManager的SearchManager的=(SearchManager的)getSystemService(Context.SEARCH_SERVICE);
        搜索查看搜索查看=(搜索查看)menu.findItem(R.id.search).getActionView();
        sea​​rchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
        sea​​rchView.setIconifiedByDefault(假);
    }    返回true;
}@覆盖
公共布尔onOptionsItemSelected(菜单项项){
    开关(item.getItemId()){
        案例R.id.search:
            onSearchRequested();
            返回true;
        案例android.R.id.home:
            意向意图=新意图(这一点,SearchableDictionary.class);
            intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
            startActivity(意向);
            返回true;
        默认:
            返回false;
    }
}
}


解决方案

我想我已经想通了这个错误。嗯,这不一定是一个错误,但实现它只是一种不同的方式。此实现使得它,所以你只能做在SearchableActivity ACTION_SEARCH (非建议)。我花了很多天摸不着头脑太,因为它很难复制。它有singleTop launchMode为SearchableActivity做。问题是,当你与 ACTION_SEARCH 搜索(非建议)在WordActivity,它不设置 FLAG_ACTIVITY_CLEAR_TOP 通过 ACTION_SEARCH 意图并将其发送给SearchableActivity onHandleIntent。这使你得到像错误ActivityManager START SearchableActivity ,但从来没有 ActivityManager DISPLAY SearchableActivity 。它从未进入的onCreate()时,这种错误发生,这使得它一个谎言找出如何解决呢
为了解决这个问题,我认为你要么必须删除singleTop使其创建另一个活动,每次ACTION_SEARCH做(放 handleIntent(意向)方法调用中的onCreate()),或覆盖 onSearchRequested()开始搜索(... ...,...,...)方法获取的意图,并设置标志的意图。当然,如果你使用CLEAR_TOP时击中了后退按钮虽然,你不能回去的活动虽然。我还没有搞清楚怎么做后者,但因为我不知道如何重写开始搜索并获得意图:X

I found interesting bug in Android Searchable dictionary sample application.

Follow these steps to reproduce it:

  • type "bo" in a search box
  • click go
  • click "bogus" from the results
  • when you see result and a definition, invoke search again
  • type "coa" which should give you "coalition"
  • Instead of that, nothing happens...

but this magically works :

  • type "bo" in a search box
  • select bogus from custom suggestion list
  • when you see result and a definition, invoke search again
  • type "coa" which should give you "coalition"
  • "coalition" is really found.

I'd like to ask you, why is this happening and how to repair it, so it'll function normally. I am very tired of it as I was trying to solve this problem whole day.

Thanks in advance, Peter.

Here is some code regarding topic, I rather post more...

public class SearchableDictionary extends Activity {

private TextView mTextView;
private ListView mListView;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    mTextView = (TextView) findViewById(R.id.text);
    mListView = (ListView) findViewById(R.id.list);

    handleIntent(getIntent());
}

@Override
protected void onNewIntent(Intent intent) {
    // Because this activity has set launchMode="singleTop", the system calls this method
    // to deliver the intent if this activity is currently the foreground activity when
    // invoked again (when the user executes a search from this activity, we don't create
    // a new instance of this activity, so the system delivers the search intent here)
    handleIntent(intent);
}

private void handleIntent(Intent intent) {
    if (Intent.ACTION_VIEW.equals(intent.getAction())) {
        // handles a click on a search suggestion; launches activity to show word
        Intent wordIntent = new Intent(this, WordActivity.class);
        wordIntent.setData(intent.getData());
        startActivity(wordIntent);
    } else if (Intent.ACTION_SEARCH.equals(intent.getAction())) {
        // handles a search query
        String query = intent.getStringExtra(SearchManager.QUERY);
        showResults(query);
    }
}

/**
 * Searches the dictionary and displays results for the given query.
 * @param query The search query
 */
private void showResults(String query) {

    Cursor cursor = managedQuery(DictionaryProvider.CONTENT_URI, null, null,
                            new String[] {query}, null);

    if (cursor == null) {
        // There are no results
        mTextView.setText(getString(R.string.no_results, new Object[] {query}));
    } else {
        // Display the number of results
        int count = cursor.getCount();
        String countString = getResources().getQuantityString(R.plurals.search_results,
                                count, new Object[] {count, query});
        mTextView.setText(countString);

        // Specify the columns we want to display in the result
        String[] from = new String[] { DictionaryDatabase.KEY_WORD,
                                       DictionaryDatabase.KEY_DEFINITION };

        // Specify the corresponding layout elements where we want the columns to go
        int[] to = new int[] { R.id.word,
                               R.id.definition };

        // Create a simple cursor adapter for the definitions and apply them to the ListView
        SimpleCursorAdapter words = new SimpleCursorAdapter(this,
                                      R.layout.result, cursor, from, to);
        mListView.setAdapter(words);

        // Define the on-click listener for the list items
        mListView.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                // Build the Intent used to open WordActivity with a specific word Uri
                Intent wordIntent = new Intent(getApplicationContext(), WordActivity.class);
                Uri data = Uri.withAppendedPath(DictionaryProvider.CONTENT_URI,
                                                String.valueOf(id));
                wordIntent.setData(data);
                startActivity(wordIntent);
            }
        });
    }
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.options_menu, menu);

    if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB){
        SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
        SearchView searchView = (SearchView) menu.findItem(R.id.search).getActionView();
        searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
        searchView.setIconifiedByDefault(false);
    }

    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
        case R.id.search:
            onSearchRequested();
            return true;
        default:
            return false;
    }
}
}

And this activity presents results:

public class WordActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.word);

    if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB){
        ActionBar actionBar = getActionBar();
        actionBar.setDisplayHomeAsUpEnabled(true);
    }

    Uri uri = getIntent().getData();
    Cursor cursor = managedQuery(uri, null, null, null, null);

    if (cursor == null) {
        finish();
    } else {
        cursor.moveToFirst();

        TextView word = (TextView) findViewById(R.id.word);
        TextView definition = (TextView) findViewById(R.id.definition);

        int wIndex = cursor.getColumnIndexOrThrow(DictionaryDatabase.KEY_WORD);
        int dIndex = cursor.getColumnIndexOrThrow(DictionaryDatabase.KEY_DEFINITION);

        word.setText(cursor.getString(wIndex));
        definition.setText(cursor.getString(dIndex));
    }
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.options_menu, menu);

    if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB){
        SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
        SearchView searchView = (SearchView) menu.findItem(R.id.search).getActionView();
        searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
        searchView.setIconifiedByDefault(false);
    }

    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
        case R.id.search:
            onSearchRequested();
            return true;
        case android.R.id.home:
            Intent intent = new Intent(this, SearchableDictionary.class);
            intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
            startActivity(intent);
            return true;
        default:
            return false;
    }
}
}

解决方案

I think I have figured out the error. Well, it's not necessarily an error, but just a different way of implementing it. This implementation makes it so you can only do ACTION_SEARCH (non-suggestions) in the SearchableActivity. It took me many days to figure this out too because it was hard to replicate. It has to do with singleTop launchMode for SearchableActivity. The problem is that when you search with ACTION_SEARCH (non-suggestions) in WordActivity, it does not set FLAG_ACTIVITY_CLEAR_TOP with the ACTION_SEARCH intent that it sends to SearchableActivity onHandleIntent. This makes you get the error that is like ActivityManager START SearchableActivity but never ActivityManager DISPLAY SearchableActivity. It never goes into the onCreate() when this error happens, which makes it a doozy to figure out how to solve it. In order to solve this problem, I think you would either have to delete singleTop to make it create another activity every time ACTION_SEARCH is done (and put handleIntent(Intent) method call in onCreate()), or override onSearchRequested()'s startSearch(..., ..., ..., ...) method to get the intent and set the intent flag. Of course, if you hit the back button though when using CLEAR_TOP, you can't go back to the activity though. I have yet to figure out how to do the latter though because I dunno how to override startSearch and get the intent :X

这篇关于Android的搜索词典样品code错了吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆