【问题标题】:Actionbarsherlock searchview: setOnQueryTextListenerActionbarsherlock 搜索视图:setOnQueryTextListener
【发布时间】:2013-02-06 04:05:06
【问题描述】:

我正在尝试使用 ActionBarSherlock 的搜索视图在列表中创建过滤器。我目前拥有的代码如下:

@Override
public boolean onCreateOptionsMenu(final Menu menu) {
    getSupportMenuInflater().inflate(R.menu.building_search, menu);

    SearchView searchView = new SearchView(getSupportActionBar().getThemedContext());

    SearchView.OnQueryTextListener queryTextListener = new SearchView.OnQueryTextListener() 
    {
        public boolean onQueryTextChange(String newText) 
        {
            // this is your adapter that will be filtered
            listAdapter.getFilter().filter(newText);
            return true;
        }

        public boolean onQueryTextSubmit(String query) 
        {
            // this is your adapter that will be filtered
            listAdapter.getFilter().filter(query);
            return true;
        }
    };
    searchView.setOnQueryTextListener(queryTextListener);

    return true;
}

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

这些是我的导入:

import android.os.Bundle;
import android.widget.AbsListView;
import android.widget.ArrayAdapter;
import be.ugent.zeus.hydra.AbstractSherlockActivity;
import be.ugent.zeus.hydra.R;
import be.ugent.zeus.hydra.data.caches.AssociationsCache;
import com.actionbarsherlock.view.Menu;
import com.actionbarsherlock.view.MenuItem;
import com.actionbarsherlock.widget.SearchView;
import com.dd.plist.NSArray;
import com.dd.plist.NSDictionary;
import com.dd.plist.NSString;
import com.dd.plist.XMLPropertyListParser;
import com.emilsjolander.components.stickylistheaders.StickyListHeadersListView;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.logging.Level;
import java.util.logging.Logger;

大部分都有效:在我的操作栏中,我得到一个搜索图标,可点击并展开搜索视图。然而,真正的监听器不起作用。我在这两种方法中都设置了断点,但是当我调试时,什么也没有发生。程序没有中断,没有任何内容被过滤,我不知道为什么。

有人知道吗?

提前致谢。

【问题讨论】:

  • 你能把你的导入贴在这个文件里吗?
  • 我将它们添加到原始问题中。

标签: android actionbarsherlock searchview


【解决方案1】:

如果您还没有找到答案,我就是这样做的(我正在使用 ActionBarSherlock):

  • 在菜单声明中添加一个条目

      <item android:id="@+id/menu_search"
        android:actionViewClass="com.actionbarsherlock.widget.SearchView"
        android:icon="@drawable/wfm_menu_search"
        android:showAsAction="always|withText|collapseActionView"
        android:title="@string/menu_search"/>
    
  • 在您的活动中覆盖“onCreateOptionsMenu”以膨胀您的菜单并关联您的 SearcView :

    public boolean onCreateOptionsMenu(final Menu menu) {
        getSupportMenuInflater().inflate(R.menu.main_menu, menu);
        this.refreshMenuItem = menu.findItem(R.id.menu_refresh);
    
        this.searchView = (SearchView) menu.findItem(R.id.menu_search).getActionView();
        this.searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
    
        @Override
        public boolean onQueryTextSubmit(String query) {
            // collapse the view ?
            menu.findItem(R.id.menu_search).collapseActionView();
            return false;
        }
    
        @Override
        public boolean onQueryTextChange(String newText) {
            // search goes here !!
            // listAdapter.getFilter().filter(query);
            return false;
        }
    
    });
    

就是这样,没有其他活动。希望对您有用。

【讨论】:

    【解决方案2】:

    由于其他所有答案都包含一些事实,但没有完全回答问题,我将创建自己的答案。

    当这个问题出现时 - 半年前 - ABS 没有完全实现 SearchView 功能。从 ABS 4.2.0 开始,这已经实现。

    可以在here 找到我的完整实现,遵循 ABS 示例应用程序。

    【讨论】:

      【解决方案3】:

      这就是我使用 ActionBarSherlock 实现搜索的方式:

      在res/menu文件夹下的menu.xml中,我添加了一个图标:

          <item android:id="@+id/search"
            android:title="@string/search_title"
            android:icon="@drawable/ic_search"
            android:showAsAction="collapseActionView|ifRoom" 
            android:actionViewClass="com.actionbarsherlock.widget.SearchView"/>
      

      然后我创建了一个类,负责接收动作搜索查询并呈现数据:

          public class SearchActivity extends SherlockFragmentActivity{
      
          @Override
              protected void onCreate(Bundle savedInstanceState) {
             Log.d(TAG, "This is the search view activity");
             // TODO Auto-generated method stub
             super.onCreate(savedInstanceState);
             setContentView(R.layout.search_result_layout);
                  }
      
      
          private void handleIntent(Intent intent){
              if(Intent.ACTION_SEARCH.equals(intent.getAction())){
              searcdhQuery = intent.getStringExtra(SearchManager.QUERY);
              //here we shall do e search..
              Log.d(TAG, "This is the search query:" + searcdhQuery);
      
                          //This is the asynctask query to connect to the database...
              String[] value = {searcdhQuery};
              SearchQuery searchQuery = new SearchQuery();
              searchQuery.execute(value);
              }
              }
      
                  @Override
              protected void onNewIntent(Intent intent) {
               // TODO Auto-generated method stub
               super.onNewIntent(intent);
               handleIntent(intent);
              }
              }
      

      在清单中,此活动包含在搜索和查看过滤器中,如下所示:

          <activity android:name=".SearchActivity"
              android:launchMode="singleTop">
              <intent-filter>
                  <action android:name="android.intent.action.SEARCH" />
              </intent-filter>
              <intent-filter>
                  <action android:name="android.intent.action.VIEW" />
              </intent-filter>
              <meta-data 
                  android:name="android.app.searchable"
                  android:resource="@xml/searchable"/>
          </activity>
      

      同样在manifest中,不要忘记应用中显示activity默认搜索的元数据,如下图:

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

      最后在 onCreateOptionsMenu 中,一定要通过关联搜索服务来添加搜索配置:

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

      onOptionsItemSelected 方法中不需要任何内容​​。

      这对我来说非常有效。如果您需要更多详细信息,请从 actionBarSherlock 中的 Search View Demodevelopers tutorial on the Setting a Search Interface 中找到更多信息。

      我希望这会有所帮助。

      【讨论】:

      • 你在哪里附加一个 onQueryTextListener?我想在用户输入时更新我正在显示的列表。我在您的代码中没有看到任何地方。
      • 我没有在查询文本listner上实现,但我认为,像下面提到的@Luksprog,它应该附加到SearchView,同时配置onOptionsItemSelected方法。
      【解决方案4】:

      这是你的完整代码吗?现在在onCreateOptionsMenu 中创建一个SearchView 并分配给它一个OnQueryTextListener,但这就是你所做的一切。我没有在您的代码中看到您将此SearchView 添加到膨胀菜单的位置。

      正如您所说您在屏幕上看到小部件,我假设您已在 R.menu.building_search 文件中声明小部件,在这种情况下,您应该查找该小部件(它没有设置侦听器)所以不执行任何操作)并且不声明一个新的(不会与用户交互):

      SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
      SearchView searchView = (SearchView) menu.findItem(R.id.menu_search).getActionView();
      

      (这是来自 android 开发者网站上的搜索指南,如果我没记错的话,这应该也适用于 ActionBarSherlock)。

      【讨论】:

        猜你喜欢
        • 2013-03-13
        • 2015-11-24
        • 1970-01-01
        • 2016-09-08
        • 2012-07-16
        • 1970-01-01
        • 1970-01-01
        • 2013-05-26
        • 2019-02-11
        相关资源
        最近更新 更多