3

Search Interfaceの公式チュートリアルに厳密に従って、Search Interface (検索ウィジェット) を実装しました。すべて問題ないように見えますが、検索クエリを送信できません。キーボードの [送信] ボタンをクリックしても何も起こりません。

これが私がしたことです:

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

}

私はこれまで何度もコードを調べてきました。バグが見えないだけです。私は何を間違っていますか?

助けてくれてありがとう。

4

1 に答える 1

5

私はそれを機能させることができました。

マニフェスト ファイルが機能するためには、次のようにする必要があります。

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

于 2013-10-20T12:02:40.903 に答える