当我在操作栏的搜索小部件中按Enter键进行查询时,没有任何反应

时间:2014-02-05 09:16:49

标签: android xml android-actionbar android-manifest searchview

我正在制作像密码管理员这样的应用。我的更新按钮正常工作,我的视图按钮正在工作,并提供所有数据库。我可以在操作栏的搜索小部件中键入我的查询,但是当我按下输入时没有任何反应....我需要帮助,我已经尝试了所有的东西....下面是代码

MainActivity.java //我想在搜索小部件中键入查询

public class MainActivity extends Activity implements OnClickListener {
Button sqlUpdate, sqlView, sqlDelete;
EditText sqlWebsite, sqlUser, sqlPass;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    sqlUpdate = (Button) findViewById(R.id.update_button);
    sqlView = (Button) findViewById(R.id.view_button);
    sqlWebsite = (EditText) findViewById(R.id.websiteText);
    sqlUser = (EditText) findViewById(R.id.userText);
    sqlPass = (EditText) findViewById(R.id.passText);
    sqlUpdate.setOnClickListener((android.view.View.OnClickListener) this);
    sqlView.setOnClickListener((android.view.View.OnClickListener) this);
}

@TargetApi(Build.VERSION_CODES.HONEYCOMB)
//@SuppressLint("NewApi")
@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);
    // 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
public void onClick(View arg0) {
    switch (arg0.getId())
    {

    case R.id.update_button:
        boolean didItWork = true;
        try{
        String website = sqlWebsite.getText().toString();
        String username = sqlUser.getText().toString();
        String password = sqlPass.getText().toString();

        UpdateActivity entry = new UpdateActivity(MainActivity.this);
        entry.open();
        entry.createEntry(website, username, password);
        entry.close();
        }catch (Exception e){
            didItWork = false;
            String error = e.toString(); 
            Dialog d = new Dialog(this);
            d.setTitle("Failed!");
            TextView tv = new TextView(this);
            tv.setText(error);
            d.setContentView(tv);
            d.show();
        }finally{
            if(didItWork){
                Dialog d = new Dialog(this);
                d.setTitle("Done!");
                TextView tv = new TextView(this);
                tv.setText("Successfull");
                d.setContentView(tv);
                d.show();
            }
        }
        break;

    case R.id.view_button:
        Class c = null;
        try {
             c=Class.forName("com.example.store_passwords.ViewActivity");
        } catch (ClassNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        Intent i = new Intent(MainActivity.this,c);
        break;
    }

}


@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
    case R.id.action_search:
        onSearchRequested();
        return true;
}
return super.onOptionsItemSelected(item);
}   
}

Searchable.java //在清单文件中传递intent之后打开。我确实实现了列表视图,但后来我尝试了一个searchableActivity的表视图......那不是问题

public class SearchableActivity extends Activity {


//TextView selection;

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


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


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



    private void handleIntent(Intent intent) {

        Toast.makeText(this, "Entered handle intent: ", Toast.LENGTH_SHORT).show();


        if (Intent.ACTION_SEARCH.equals(intent.getAction())) {
            String query = intent.getStringExtra(SearchManager.QUERY);
            //use the query to search your data somehow
            doMySearch(query);
            Toast.makeText(this, "QUERY after do my search: " + query, Toast.LENGTH_SHORT).show();
        }


    }

    private void doMySearch(String query) {
        // TODO Auto-generated method stub

        TextView tv1 = (TextView) findViewById(R.id.tvSQLinfo4);
        UpdateActivity info1 = new UpdateActivity(this);
        info1.open();
        String data1 = info1.getWebsitesSearch(query);
        info1.close();
        tv1.setText(data1);

        TextView tv2 = (TextView) findViewById(R.id.tvSQLinfo5);
        UpdateActivity info2 = new UpdateActivity(this);
        info2.open();
        String data2 = info2.getUsernamesSearch(query);
        info2.close();
        tv2.setText(data2);

        TextView tv3 = (TextView) findViewById(R.id.tvSQLinfo6);
        UpdateActivity info3 = new UpdateActivity(this);
        info3.open();
        String data3 = info3.getPasswordsSearch(query);
        info3.close();
        tv3.setText(data3);
}

}

RE /菜单/ main.xml中

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

    <item
    android:id="@+id/action_search"
    android:orderInCategory="100"
    android:showAsAction="always"
    android:icon="@drawable/ic_launcher"
    android:title="Search"
    android:actionViewClass="android.widget.SearchView"/>



</menu>

searchable.xml

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

<item
    android:id="@+id/action_settings"
    android:orderInCategory="100"
    android:showAsAction="never"
    android:title="@string/action_settings"/>

</menu>

RES / XML / searchable.xml

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

AndroidManifest.xml //我也试过把元数据放在这个文件中的主要活动中,这个文件没有用,所以我把它放在应用程序标签里面

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

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

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

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

    <activity
        android:name="com.example.store_passwords.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.store_passwords.UpdateActivity"
        android:label="@string/title_activity_update" >
    </activity>
    <activity
        android:name="com.example.store_passwords.ViewActivity"
        android:label="@string/title_activity_view" >
    </activity>


    <activity android:name=".SearchableActivity"
       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>

0 个答案:

没有答案