如何从搜索结果活动中获得结果?

时间:2015-01-16 02:57:47

标签: java android searchview

我在操作栏中有一个带有搜索按钮的Activity(MainActivity)。它通过一些输入字符串进行搜索,并在另一个Activity(SearchResultsActivity)的ListView中显示结果。用户可以单击任何结果来选择它。

我想将用户选择的值返回到主活动,但它不起作用。我查看了documentation,但我没有发现任何相关内容。 我尝试在结果活动中使用setResult(Intent),但主活动中的onActivityResult()永远不会被调用。

我做错了什么?我该怎么办?

的AndroidManifest.xml:

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="foo.bar" >

    <!-- ... -->

    <uses-sdk android:minSdkVersion="11" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name">

        <!-- Main Activity -->
        <activity
            android:name=".MainActivity"
            android:label="@string/title_activity_main">

            <meta-data
                android:name="android.app.default_searchable"
                android:value=".SearchResultsActivity" />

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

        <!-- Search results activity -->
        <activity android:name=".SearchResultsActivity"
            android:parentActivityName=".MainActivity" >
            <intent-filter>
                <action android:name="android.intent.action.SEARCH" />
            </intent-filter>

            <meta-data
                android:name="android.app.searchable"
                android:resource="@xml/searchable" />
        </activity>
    </application>

</manifest>

menu.xml文件:

<menu xmlns:tools="http://schemas.android.com/tools"
    xmlns:android="http://schemas.android.com/apk/res/android"
    tools:context=".MainActivity">

    <!-- Search -->
    <item android:id="@+id/action_search"
        android:icon="@drawable/ic_action_search"
        android:title="@string/action_search"
        android:showAsAction="always"
        android:actionViewClass="android.widget.SearchView" />

    <!-- ... -->
</menu>

MainActivity:

public class MainActivity extends FragmentActivity {
    // ...

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        final MenuInflater menuInflater = getMenuInflater();
        menuInflater.inflate(R.menu.activity_main_menu, menu);

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

        return true;
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        // never gets called!
        super.onActivityResult(requestCode, resultCode, data);
    }

    // ...
}

SearchResultsActivity:

public class SearchResultsActivity extends Activity {
    private ListView listResults;

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

        // get the action bar
        ActionBar actionBar = getActionBar();

        // Enabling Back navigation on Action Bar icon
        actionBar.setDisplayHomeAsUpEnabled(true);

        listResults = (ListView) findViewById(R.id.listResults);

        handleIntent(getIntent());
    }

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

    /**
     * Handling intent data
     */
    private void handleIntent(Intent intent) {
        if (Intent.ACTION_SEARCH.equals(intent.getAction())) {
            String query = intent.getStringExtra(SearchManager.QUERY);

            final Serializable[] results = find(query);

            listResults.setAdapter(new ArrayAdapter<BusLine>(this, R.layout.list_view, busLines));
            listResults.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                    Serializable selectedResult = (Serializable) parent.getItemAtPosition(position);
                    setResult(RESULT_OK, new Intent().putExtra("result", selectedResult));
                    finish();
                }
            });
        }
    }
}

1 个答案:

答案 0 :(得分:0)

我可以理解为什么你会认为你会使用setResult和onActivityResult来处理搜索结果活动中的点击,但这不是它的工作原理。仅当您使用startActivityForResult启动活动时才会应用这些功能 - 而不是在显示搜索结果时。

解决方案是以正常方式调用MainActivity上的startActivity,即不使用活动结果。像这样:

listResults.setOnItemClickListener(new AdapterView.OnItemClickListener() {
    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        Serializable selectedResult = (Serializable) parent.getItemAtPosition(position);
        Intent intent = new Intent();
        intent.putExtra("result", selectedResult);
        intent.setClass(SearchResultsActivity.this, MainActivity.class);
        startActivity(intent);
        finish();
    }
});

现在您还需要在MainActivity中捕获此搜索结果,但您需要确保处理正常情况以及搜索结果案例:

@Override
protected void onCreate(Bundle savedInstanceState) {
    Intent intent = getIntent();
    if (intent.hasExtra("result")) {
        // Launched from search results
        Serializable selectedResult = (Serializable) intent.getSerializableExtra("result");
        ...
    }
    ...
}