Android 搜索界面未提交查询

2024-05-08

我按照官方教程实现了一个搜索界面(搜索小部件)搜索界面 http://developer.android.com/training/search/setup.html密切。一切看起来都不错,但我无法提交搜索查询。当我单击键盘上的“发送”按钮时,没有任何反应。

这是我所做的:

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<uses-sdk
    android:minSdkVersion="14"
    android:targetSdkVersion="18" />

<uses-permission android:name="android.permission.GET_ACCOUNTS" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />

<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name="com.cheatdatabase.ItemListActivity"
        android:label="@string/app_name" >
    </activity>
    <activity
        android:name="com.cheatdatabase.ItemDetailActivity"
        android:label="@string/title_item_detail"
        android:parentActivityName=".ItemListActivity" >
        <meta-data
            android:name="android.support.PARENT_ACTIVITY"
            android:value=".ItemListActivity" />
    </activity>
    <activity
        android:name="com.cheatdatabase.MainActivity"
        android:label="@string/title_activity_main" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>

        <meta-data
            android:name="android.app.searchable"
            android:resource="@xml/searchable" />
    </activity>
    <activity
        android:name=".SearchResultsActivity"
        android:label="@string/title_activity_search_result" >
        <intent-filter>
            <action android:name="android.intent.action.SEARCH" />
        </intent-filter>
    </activity>
</application>

res/xml/searchable.xml

<?xml version="1.0" encoding="utf-8"?>
<searchable xmlns:android="http://schemas.android.com/apk/res/android"
android:hint="@string/search_game"
android:label="@string/app_name" >

资源/菜单/options_menu.xml

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >

<item
    android:id="@+id/search"
    android:actionViewClass="android.widget.SearchView"
    android:icon="@android:drawable/ic_menu_search"
    android:showAsAction="collapseActionView|ifRoom"
    android:title="@string/search_game"/>

MainActivity.java

public class MainActivity extends FragmentActivity implements ActionBar.OnNavigationListener {

/**
 * The serialization (saved instance state) Bundle key representing the
 * current dropdown position.
 */
private static final String STATE_SELECTED_NAVIGATION_ITEM = "selected_navigation_item";

// more efficient than HashMap for mapping integers to objects
SparseArray<Group> groups = new SparseArray<Group>();

private ExpandableListView listView;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    // Set up the action bar to show a dropdown list.
    final ActionBar actionBar = getActionBar();
    actionBar.setDisplayShowTitleEnabled(false);
    actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_LIST);

    // Set up the dropdown list navigation in the action bar.
    String[] allSystems = Tools.getGameSystemsFromXml(this);
    // Specify a SpinnerAdapter to populate the dropdown list.
    actionBar.setListNavigationCallbacks(new ArrayAdapter<String>(getActionBarThemedContextCompat(), android.R.layout.simple_list_item_1, android.R.id.text1, allSystems), this);

    listView = (ExpandableListView) findViewById(R.id.listView);

    SearchresultExpandableListAdapter adapter = new SearchresultExpandableListAdapter(this, groups);
    listView.setAdapter(adapter);
    createData();
    for (int i = 0; i < groups.size(); i++) {
        listView.expandGroup(i, false);
    }
}

public void createData() {
    for (int j = 0; j < 5; j++) {
        Group group = new Group("Test " + j);
        for (int i = 0; i < 5; i++) {
            group.children.add("Sub Item" + i);
        }
        groups.append(j, group);
    }
}

/**
 * Backward-compatible version of {@link ActionBar#getThemedContext()} that
 * simply returns the {@link android.app.Activity} if
 * <code>getThemedContext</code> is unavailable.
 */
@TargetApi(Build.VERSION_CODES.ICE_CREAM_SANDWICH)
private Context getActionBarThemedContextCompat() {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH) {
        return getActionBar().getThemedContext();
    } else {
        return this;
    }
}

@Override
public void onRestoreInstanceState(Bundle savedInstanceState) {
    // Restore the previously serialized current dropdown position.
    if (savedInstanceState.containsKey(STATE_SELECTED_NAVIGATION_ITEM)) {
        getActionBar().setSelectedNavigationItem(savedInstanceState.getInt(STATE_SELECTED_NAVIGATION_ITEM));
    }
}

@Override
public void onSaveInstanceState(Bundle outState) {
    // Serialize the current dropdown position.
    outState.putInt(STATE_SELECTED_NAVIGATION_ITEM, getActionBar().getSelectedNavigationIndex());
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);

    // Search
    getMenuInflater().inflate(R.menu.options_menu, menu); 

    // Associate searchable configuration with the SearchView
    SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
    SearchView searchView = (SearchView) menu.findItem(R.id.search).getActionView();
    searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));

    return true;
}

@Override
public boolean onNavigationItemSelected(int position, long id) {
    // When the given dropdown item is selected, show its contents in the
    // container view.
    Fragment fragment = new DummySectionFragment();
    Bundle args = new Bundle();
    args.putInt(DummySectionFragment.ARG_SECTION_NUMBER, position + 1);
    fragment.setArguments(args);
    getSupportFragmentManager().beginTransaction().replace(R.id.container, fragment).commit();
    return true;
}

/**
 * A dummy fragment representing a section of the app, but that simply
 * displays dummy text.
 */
public static class DummySectionFragment extends Fragment {
    /**
     * The fragment argument representing the section number for this
     * fragment.
     */
    public static final String ARG_SECTION_NUMBER = "section_number";

    public DummySectionFragment() {
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_main_dummy, container, false);
        TextView dummyTextView = (TextView) rootView.findViewById(R.id.section_label);
        dummyTextView.setText(Integer.toString(getArguments().getInt(ARG_SECTION_NUMBER)));
        return rootView;
    }
}

}

搜索结果活动.java

public class SearchResultsActivity extends Activity {

// more efficient than HashMap for mapping integers to objects
SparseArray<Group> groups = new SparseArray<Group>();

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_search_result);

    handleIntent(getIntent());
}

@Override
protected void onNewIntent(Intent intent) {
    setIntent(intent);
    handleIntent(intent);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.search_result, menu);
    return true;
}

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

        createData();
        ExpandableListView listView = (ExpandableListView) findViewById(R.id.listView);
        SearchresultExpandableListAdapter adapter = new SearchresultExpandableListAdapter(this, groups);
        listView.setAdapter(adapter);
        for (int i = 0; i < groups.size(); i++) {
            listView.expandGroup(i, false);         
        } 
    }
}

public void createData() {
    for (int j = 0; j < 5; j++) {
        Group group = new Group("Test " + j);
        for (int i = 0; i < 5; i++) {
            group.children.add("Sub Item" + i);
        }
        groups.append(j, group);
    }
}

}

到目前为止,我已经将代码浏览了很多次了。我只是没有看到这个错误。我究竟做错了什么?

谢谢你的帮助。


我刚刚设法让它发挥作用。

清单文件必须如下所示才能正常工作:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.cheatdatabase"
android:versionCode="1"
android:versionName="1.0" >

<uses-sdk
    android:minSdkVersion="14"
    android:targetSdkVersion="18" />

<uses-permission android:name="android.permission.GET_ACCOUNTS" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />

<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name="com.cheatdatabase.ItemListActivity"
        android:label="@string/app_name" >
    </activity>
    <activity
        android:name="com.cheatdatabase.ItemDetailActivity"
        android:label="@string/title_item_detail"
        android:parentActivityName=".ItemListActivity" >
        <meta-data
            android:name="android.support.PARENT_ACTIVITY"
            android:value=".ItemListActivity" />
    </activity>
    <activity
        android:name="com.cheatdatabase.MainActivity"
        android:label="@string/title_activity_main" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>

        <meta-data
            android:name="android.app.default_searchable"
            android:value=".SearchResultsActivity" />
    </activity>
    <activity
        android:name=".SearchResultsActivity"
        android:label="@string/title_activity_search_result" >
        <intent-filter>
            <action android:name="android.intent.action.SEARCH" />
        </intent-filter>

        <meta-data
            android:name="android.app.searchable"
            android:resource="@xml/searchable" />
    </activity>
</application>
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Android 搜索界面未提交查询 的相关文章

随机推荐

  • xpath 的多个 string() 结果?

    string 在我试图从中提取文本的某个网页上效果很好 具有类似的结构 对于 bing 我尝试过的 xpath 是 string h3 a 即使有强标签等 它也能很好地获取搜索结果 但只返回第一个结果 有没有类似 strings 的东西 这
  • 将 for 循环中的值传递给事件侦听器 - Javascript [重复]

    这个问题在这里已经有答案了 可能的重复 使用 Google Maps API v3 循环遍历标记问题 https stackoverflow com questions 2670356 looping through markers wit
  • 闪亮的如何阻止用户访问选项卡?

    我需要阻止用户访问其他选项卡 直到完成某些操作 在这个可重现的示例中 我想阻止用户访问Tab 2直到他按下按钮 该应用程序如下所示 这是该应用程序的代码 library shiny ui lt shinyUI navbarPage tabP
  • 如何真正释放 Linux 中的大页面以供新进程使用?

    真的找不到太多关于此的信息 希望有人可以提供帮助 我正在假脱机使用 100GB java 堆作为大数据缓存 为了避免与文件系统缓存等内容发生冲突 并且因为它通常性能更好 我将其分配在大页面中 我保留了 51 200 x 2MB 大页面 一切
  • 如何在 VS Code 中集成 babun shell

    我尝试过更改设置 terminal integrated shell windows to babun mintty地点 但是 babun shell 窗口单独打开 并且不与 VS code 集成 有人知道如何实现这一目标吗 经过2个小时的
  • 如何强制send_data在浏览器中下载文件?

    好吧 我的问题是我正在使用send data on my Rails 3应用程序向用户发送文件AWS S3类似的服务 Base establish connection access key id gt my key secret acce
  • Python 使用 pandas 和 str.strip 崩溃

    这段最少的代码使我的 Python 崩溃了 设置 pandas 0 13 0 python 2 7 3 AMD64 Win7 import pandas as pd input file r c3 csv input df pd read
  • Google Pub/Sub 是否有队列或主题?

    我熟悉 JMS 对 Google Pub Sub 还很陌生 在 JMS 中有 2 个选项 Queue 只有一个消费者可以接受消息 Topic 每个消费者接受来自主题的每条消息 我相信 Google Pub Sub 应该支持这样的东西 但是快
  • 如何通过值获取 JavaScript“Map”中的键?

    我有一个像这样的 JavaScript 地图 let people new Map people set 1 jhon people set 2 jasmein people set 3 abdo 我想要某种方法按值返回键 let jhon
  • 通过vba在每个空间范围之间添加求和公式

    我试图进行自动化 但我被困在这里 我需要在空间范围之间动态添加总和公式 我完全迷失了使用 VBA 添加公式的能力 任何人都可以帮助我 先感谢您 我假设您想要的是 如果单元格中有空白 您希望将所有其他元素相加并将结果放置在该空白中 可能有很多
  • 没有找到任务运行程序配置?

    我有 新安装的 Visual Studio Professional 2017 V 15 9 4 视觉工作室解决方案 https learn microsoft com en us visualstudio ide solutions an
  • 尝试使用 python 连接 mongodb atlas 时连接超时

    我正在尝试连接到我的 mongodb atlas 集群 但是当我尝试对我的数据库执行某些操作时 我总是超时 我使用的数据库是在 mongoshell 中创建的 也是我在 mongodb compass 中检查它们是否存在的集合 ERROR
  • 工作站和嵌入式程序员之间的心态差异[关闭]

    就目前情况而言 这个问题不太适合我们的问答形式 我们希望答案得到事实 参考资料或专业知识的支持 但这个问题可能会引发辩论 争论 民意调查或扩展讨论 如果您觉得这个问题可以改进并可能重新开放 访问帮助中心 help reopen questi
  • Numpy 的舍入方式与 Python 不同

    The code import numpy as np a 5 92270987499999979065 print round a 8 print round np float64 a 8 gives 5 92270987 5 92270
  • Linux 命令行工具验证 XSD 1.1?

    是否有任何命令行工具可以根据 XSD 版本验证 XML1 1 Xmllint https en wikipedia org wiki Libxml2不验证版本 1 1 我在 Xerces J 周围找到了一个方便的包装 https www d
  • 在 OrderedDict 中如何按特定属性排序? [关闭]

    Closed 这个问题需要调试细节 help minimal reproducible example 目前不接受答案 我正在尝试通过执行以下操作对以下 OrderedDict 进行排序 gt gt gt gt from collectio
  • AVSpeechUtterance 最大音量非常安静且速率非常快

    我正在考虑向我的应用程序添加语音提示 并在 iOS 7 中测试 AVSpeechUtterance 但默认语音速率非常快 最低语速更容易理解 但是最大音量值1太安静了 我在 iPhone 4 上进行了测试 并将音量调到最大 一定是出了什么问
  • 如何分析Android应用程序的电池使用情况并对其进行优化?

    我想分析我的应用程序的电池使用情况 我的意思是应用程序的各个部分 例如 广播接收器 监听器 服务等 使用多少电池 我需要一个详细的列表 从列表中 我想优化电池的使用 方法与使用内存分析器类似 http android developers
  • 如何通过反射访问抽象父类中的实例字段?

    所以 举例来说 StringBuilder继承自抽象类AbstractStringBuilder 据我了解 StringBuilder本身没有字段 除了serialVersionUID 相反 它的状态由以下字段表示AbstractStrin
  • Android 搜索界面未提交查询

    我按照官方教程实现了一个搜索界面 搜索小部件 搜索界面 http developer android com training search setup html密切 一切看起来都不错 但我无法提交搜索查询 当我单击键盘上的 发送 按钮时