Android Search Interface not submitting query

会有一股神秘感。 提交于 2019-12-07 16:11:57

问题


I've implemented a Search Interface (the Search Widget) by following the official tutorial for Search Interface closely. It all looks good but I can't submit the search query. When I click the "Send" button on the keyboard nothing happens.

Here is what I did:

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" >

res/menu/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;
    }
}

}

SearchResultsActivity.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);
    }
}

}

I've gone through the code so many times by now. I just don't see the bug. What am I doing wrong?

Thanks for any help.


回答1:


I have just managed to get it to work.

Here is how the manifest file has to look like in order for it to work:

<?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>



来源:https://stackoverflow.com/questions/19476814/android-search-interface-not-submitting-query

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!