其他分享
首页 > 其他分享> > android – Actionbarsherlock searchview:setOnQueryTextListener

android – Actionbarsherlock searchview:setOnQueryTextListener

作者:互联网

我正在尝试使用ActionBarSherlock的搜索视图在List中创建一个过滤器.我目前的代码如下:

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

其中大部分都有效:在我的操作栏中,我获得了一个可点击的搜索图标,并展开了搜索视图.然而,不起作用的是实际的听众.我在两种方法中都加了断点,但是当我调试时,没有任何反应.该程序没有破坏,没有任何过滤,我无法弄清楚原因.

有没有人有任何想法?

提前致谢.

解决方法:

这就是我用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>

同样在清单中,不要忘记应用程序中显示活动默认搜索的元数据,如下所示:

    <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和developers tutorial on the Setting a Search Interface中找到Search View Demo的更多信息.

我希望这有帮助.

标签:android,actionbarsherlock,searchview
来源: https://codeday.me/bug/20191007/1864984.html