【问题标题】:Execute search from action bar从操作栏执行搜索
【发布时间】:2013-05-30 23:28:07
【问题描述】:

我已成功让我的操作栏显示在 android 中。我现在正在尝试实现搜索。我正在尝试从小处着手,至少在尝试对搜索词进行任何操作之前先看看我是否可以获得搜索词。

我在这里学习了这个教程:

http://developer.android.com/training/search/setup.html

并创建了一个看起来像这样的 SearchResultsActivity 类来处理搜索。

import android.app.Activity;
import android.app.SearchManager;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.widget.Toast;


public class SearchResultsActivity extends Activity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        //possible more code
        handleIntent(getIntent());
    }

    @Override
    protected void onNewIntent(Intent intent) {
        //possible more code
        handleIntent(intent);
    }

    private void handleIntent(Intent intent) {

        if (Intent.ACTION_SEARCH.equals(intent.getAction())) {
            String query = intent.getStringExtra(SearchManager.QUERY);
            //use the query to search your data somehow

            //toast error test
            Context context = getApplicationContext();
            CharSequence text = "Hello toast!";
            int duration = Toast.LENGTH_SHORT;

            Toast toast = Toast.makeText(context, query, duration);
            toast.show();

        }
    }

    ///more code

}

我尝试添加 toast 以查看当我输入文本然后单击搜索时是否会弹出 toast。现在没有吐司出现。

【问题讨论】:

    标签: android toast android-search


    【解决方案1】:

    我正在做一个类似的项目,在操作栏中有一个搜索小部件。这仅与 API 11+ 兼容,因此没有姜饼,但看起来更好。您仍然需要设置一个 searchable.xml 并在清单中添加适当的元数据。我不确定您将如何实现这一点,但下面的代码会加载一个 ListView,然后添加字符串。 onCreateOptionsMenu() 部分配置搜索小部件并根据估算的文本过滤 ListView。希望对您有所帮助!

        public class ListOfMathCourses extends ListActivity  {
    
        ArrayAdapter<String> adapter;
        ListView list;
        String[] math = new String[] {"Pre-Algebra", "Algebra I", "Algebra II", "Geometry", "Calculus"};
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    
        setContentView(R.layout.startingpoint);
        list = (ListView) findViewById(android.R.id.list);
        adapter = new ArrayAdapter<String>(this, R.layout.listviewrow, math);
        list.setAdapter(adapter);
        getListView().setTextFilterEnabled(true);
    
    
        }
    
    
        @Override
        public boolean onCreateOptionsMenu(Menu menu) {        
    
            getMenuInflater().inflate(R.menu.menu_search, menu);
            //getMenuInflater().inflate(R.menu.action, menu);   
    
            SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
            SearchView searchView = (SearchView) menu.findItem(R.id.menu_search).getActionView();
    
                searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
                searchView.setIconifiedByDefault(false);   
    
            SearchView.OnQueryTextListener queryTextListener = new SearchView.OnQueryTextListener() 
            {
                @Override
                public boolean onQueryTextChange(String newText) 
                {
                    // this is your adapter that will be filtered
                    adapter.getFilter().filter(newText);
                    return true;
                }
                @Override
                public boolean onQueryTextSubmit(String query) 
                {
                    // this is your adapter that will be filtered
                    adapter.getFilter().filter(query);
                    return true;
                }
            };
            searchView.setOnQueryTextListener(queryTextListener);
    
            return super.onCreateOptionsMenu(menu);
    
        }
    

    }

    【讨论】:

    • 我将通过 api 搜索在线数据库。所以我需要从搜索框中检索该术语并将其发送到附加到一个 url 以访问一个 api,然后返回我需要解析并插入到列表视图中的 json。我猜你的代码中输入的实际搜索字符串在哪里?
    • 我不是这方面的专家,但我认为 onQueryTextChange() 会根据用户输入刷新列表。与 onQueryTextSubmit() 相同。他们都根据您在搜索小部件中输入的内容刷新列表。字符串是 newText 和查询。他们告诉过滤列表的内容。因此,如果您通过 url 发送查询,那可能会起作用。带上一粒盐。希望对您有所帮助。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-11-05
    • 2014-05-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多