无法在Android搜索小部件中传递搜索变量

时间:2014-01-31 10:46:10

标签: java android xml search searchview

我无法将我的MainActivity.java发送到我的SearchResultsActivity.java的搜索查询传递给on the official guide.所述的搜索小部件

但是,我不希望在操作栏上设置我的搜索小部件。

我没有错误。我唯一的愿望是通过" Log.v"来显示我的查询。 但官方指南似乎只是解释了当我们在操作栏中找到搜索小部件时如何调用另一个活动,这不是我们的例子。

如何在我的MainActivity.java中检索变量查询?



MainActivity.java:

package com.example.fidbacks_search;

import android.os.Bundle;
import android.app.Activity;
import android.app.SearchManager;
import android.content.Context;
import android.view.Menu;
import android.widget.SearchView;

public class MainActivity extends Activity {

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


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

        return true;
    }

}



SearchResultsActivity.java

package com.example.fidbacks_search;
import android.os.Bundle;
import android.app.Activity;
import android.app.SearchManager;
import android.content.Intent;
import android.util.Log;
import android.view.Menu;

public class SearchResultsActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_search_results);
         Log.v("QUERY", "this isn t even being displayed when the search button is pressed");

        // Get the intent, verify the action and get the query
        Intent intent = getIntent();
        if (Intent.ACTION_SEARCH.equals(intent.getAction())) {
          String query = intent.getStringExtra(SearchManager.QUERY);
          Log.v("QUERY", query.toString());
        }
    }

    @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_results, menu);
        return true;
    }

}



Searchable.xml

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



的AndroidManifest.xml

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

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

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.example.fidbacks_search.MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity
            android:name="com.example.fidbacks_search.SearchResultsActivity"
            android:label="@string/title_activity_search_results"
            android:launchMode="singleTop">
            <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>



布局/ activity_main.xml中

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >

    <SearchView
        android:id="@+id/searchfield"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_marginTop="144dp"
        android:iconifiedByDefault="false" >

    </SearchView>

</RelativeLayout>

cantpassvariable

1 个答案:

答案 0 :(得分:1)

我不确定你想要怎么做,但我最近使用了SearchView并得到了你想要的东西,我只想做以下几点。

在您的活动中,添加onCreate之类的内容:

searchView = (SearchView) findViewById(R.id.searchfield);

SearchManager searchManager = (SearchManager) getActivity().getSystemService(Context.SEARCH_SERVICE);
searchView.setQueryHint("Enter query here");
searchView.setOnQueryTextListener(this);

然后让你当前的活动实现SearchView.OnQueryTextListener的两种方法;你要找的是onQueryTextSubmit。在这里,使用传递给它的查询并调用SearchResultsActivity

Intent intent = new Intent(this, SearchResultsActivity.class);
intent.putExtra("query",query);
startActivity(intent);

然后让你的SearchResultsActivity在`onCreate'中获取查询:

query = getIntent().getStringExtra("query");

然后,使用查询并随意执行任何操作。