简体   繁体   English

操作栏中的Android SearchView无法正常工作

[英]Android SearchView in actionbar not working

First of all sorry if im new to android and java and still want to implement something for my thesis regarding the app im creating, I just want it to have a search view in the action bar for a certain activity to search for the corresponding letters or words in the search for my custom listview... and I dont know what to put in the "doMySearch(String query)" to get the results. 首先,很抱歉,如果我是android和java的新手,并且仍然想为我有关应用程序im创建的论文实现一些东西,我只希望它在操作栏中有一个用于特定活动的搜索视图,以搜索相应的字母或在搜索我的自定义列表视图中的单词...,我不知道要在“ doMySearch(String query)”中放入什么以获取结果。

SearchResultsActivity.java SearchResultsActivity.java

  import android.app.ActionBar;
  import android.app.ListActivity;
  import android.app.SearchManager;
  import android.content.Intent;
  import android.os.Bundle;
  import android.widget.TextView;

  public class SearchResultsActivity extends ListActivity {

private TextView txtQuery;

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

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

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

    txtQuery = (TextView) findViewById(R.id.txtQuery);

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

        doMySearch(query);
        txtQuery.setText("Search Query: " + query);
    }
}

private void doMySearch(String query) {
    // TODO Auto-generated method stub
       ArrayAdapter<String> adapter =new ArrayAdapter<String>(this, R.layout.subject_list_layout, R.id.subject_course_title_row);
    return adapter;

}
}

Searchable.xml Searchable.xml

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

Manifest.xml Manifest.xml

        <!-- Sample Search -->
    <activity
        android:name="com.randomlyroaming.com.loaopencourseware.courses.BSIT"
        android:screenOrientation="portrait"
        android:windowSoftInputMode="stateHidden" >
        <meta-data
            android:name="android.app.default_searchable"
            android:value=".SearchResultsActivity" />
    </activity>

    <!-- Search results activity -->
    <activity
        android:name=".SearchResultsActivity"
        android:label="@string/app_name"
        android:screenOrientation="portrait" >
        <intent-filter>
            <action android:name="android.intent.action.SEARCH" />
        </intent-filter>

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

Please Help... It is already redirecting to "txtQuery.setText("Search Query: " + query);" 请帮助...它已经重定向到“ txtQuery.setText(” Search Query:“ + query);” but no results... Do I need to put something the method "doMySearch(query);" 但没有结果...我需要在方法“ doMySearch(query);”中添加一些内容吗? what code can I put it in there for it to work?.. 我可以在其中放置什么代码才能正常工作?..

Thank you and Your help is much Appreciated... 谢谢,非常感谢您的帮助...

but no results... Do I need to put something the method "doMySearch(query); 但没有结果...我需要在方法中添加“ doMySearch(query);

Yes! 是! it isn't supposed to show anything because you are just printing the query into a TextView . 它不应该显示任何内容,因为您只是将查询打印到TextView

If you want to access an SQLite Database, change a ListView 's adapter, or do something else, you would have to implement it in that method. 如果要访问SQLite数据库,更改ListView的适配器或执行其他操作,则必须在该方法中实现它。

Going back to what you want, I think you are looking to filter the words in the ListView using the query, am I right? 回到您想要的内容,我想您正在寻找使用查询过滤ListView的单词的方法,对吗? Anyway, Here you have a nice tutorial for that. 无论如何, 这里您有一个不错的教程。 Also, this post may help as you well. 另外, 这篇文章可能对您有帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM