Add search function to list view in android (Example) 您所在的位置:网站首页 上海亲子三日游玩路线 Add search function to list view in android (Example)

Add search function to list view in android (Example)

2022-06-26 04:22| 来源: 网络整理| 查看: 265

Action bar with SearchView Beginning with Android 3.0, android supports to add SearchView widget to your action bar. This search view can be use to search the content in a list view

More info about SearchView http://developer.android.com/training/search/setup.html

Following are the implementation steps of searchable list view Add search menu

Create a menu resource file with search menu item(when click on search menu item, search widget will be appear in action bar)

Following is an example menu resource that I have created(res/menu/search_menu.xml)

In above menu item the collapseActionView attribute allows your SearchView to expand to take up the whole action bar and collapse back down into a normal action bar item when not in use Create SearchableConfiguration

Searchable Configuration defines how SearchView behaves

Need to define it in a xml(res/xml/searchable.xml). Following is an example searchable configuration file

Then add this element into a relevant activity with tag In my scenario I'm gonna add SearchView to FriendListActivity Add Menu and SearchableConfiguration to activity Associate searchable configuration with the SearchView in the activity class @Override public boolean onCreateOptionsMenu(Menu menu) { MenuInflater inflater = getMenuInflater(); inflater.inflate(R.menu.search_menu, menu); SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE); searchMenuItem = menu.findItem(R.id.search); searchView = (SearchView) searchMenuItem.getActionView(); searchView.setSearchableInfo(searchManager. getSearchableInfo(getComponentName())); searchView.setSubmitButtonEnabled(true); searchView.setOnQueryTextListener(this); return true; } Now the SearchView added to the activity. But still searching functionality not working

Picture

Add searching functionality Implement SearchView.OnQueryTextListener in activity, need to override two new methods now public boolean onQueryTextSubmit(String query) public boolean onQueryTextChange(String newText) This interface listen to text change events in SearchView @Override public boolean onQueryTextSubmit(String query) { return false; } @Override public boolean onQueryTextChange(String newText) { friendListAdapter.getFilter().filter(newText); return true; }

In here onQueryTextChange function do the filtering with list adapter, In order to do the filtering, adapter need to be implement Filterable interface

Following is the complete implementation of list activity(FriendListActivity)

https://github.com/erangaeb/dev-notes/blob/master/android-search-list/FriendListActivity.java

Filterable adapter

In my scenario I have used BaseAdapter with implementing Filterable interface

Following is the implementation of my adapter class(FriendListAdapter)

https://github.com/erangaeb/dev-notes/blob/master/android-search-list/FriendListAdapter.java

In here I have defined custom Filter class FriendFilter to do the filtering, I'm filtering the list according to the name entering in SearchView

I have defined two array lists here. One for display in list view and other one for filtering(original list)

// original list, filtering do with this list private ArrayList friendList; // filtered list construct by filtering the friendList // it uses to create the list view private ArrayList filteredList; Searching functionality is working now. Following is an example output of searching list

Picture

Collapse action bar SearchView programatically

In my scenario when list item clicks, I'm navigate to new activity ShareActivity

When navigating, I'm collapsing the SearchView if it is visible

private void handelListItemClick(User user) { // close search view if its visible if (searchView.isShown()) { searchMenuItem.collapseActionView(); searchView.setQuery("", false); } // pass selected user and sensor to share activity Intent intent = new Intent(this, ShareActivity.class); intent.putExtra("com.score.senzors.pojos.User", user); this.startActivity(intent); this.overridePendingTransition(R.anim.right_in, R.anim.stay_in); } Add search view popup text while searching First enable filtering on the list view friendListView.setTextFilterEnabled(true); Modify onQueryTextChange function like below @Override public boolean onQueryTextChange(String newText) { if (TextUtils.isEmpty(newText)) { friendListView.clearTextFilter(); } else { friendListView.setFilterText(newText.toString()); } return true; } Now search view popup will be visible when searching. Actually is displays text that typing in SearchView

Picture

Complete source code https://github.com/erangaeb/dev-notes/tree/master/android-search-list



【本文地址】

公司简介

联系我们

今日新闻

    推荐新闻

    专题文章
      CopyRight 2018-2019 实验室设备网 版权所有