Class Diagram:
Searchview and ListView can often be combined to achieve the search and filtering of data.
1. Monitor Searchview,searchview.setonquerytextlistener (Onquerytextlistener listener);
2. Turn on the filter function of the ListView, listview.settextfilterenabled (true). Must be turned on, otherwise it will not be filtered;
3. When the Searchview receives the input event, it calls the Listview.setfiltertext (FilterText) method, which gets the filter through adapter and then calls Filter.filter ( FilterText):
[Java]View PlainCopyprint?
- Public void Setfiltertext (String filtertext) {
- //Todo:should We check for Acceptfilter ()?
- if (mtextfilterenabled &&!) Textutils.isempty (FilterText)) {
- Createtextfilter (false);
- //This is going-to-listener ontextchanged, but we might not
- //Be ready to bring up a window yet
- Mtextfilter.settext (FilterText);
- Mtextfilter.setselection (Filtertext.length ());
- if (madapter instanceof filterable) {
- //If Mpopup is non-null and then ontextchanged'll do the filtering
- if (mpopup = = null) {
- Filter f = ((filterable) madapter). GetFilter ();
- F.filter (FilterText);
- }
- //Set filtered to true so we'll display the filter window when our main
- //Window is ready
- mfiltered = true;
- Mdatasetobserver.clearsavedstate ();
- }
- }
- }
The 4.filter.filter (FilterText) method will eventually call Filter.performfiltering (FilterText) and Filter.publishresults (CharSequence FilterText, Filterresults results). The Performfiltering (FilterText) method completes the filtering process and returns the result Filterresults, while Publishresults (Charsequence FilterText, FilterResults Results) is processed according to the returned results.
5.filter.publishresults (charsequence FilterText, filterresults results) called the Baseadapter.notifydatasetchanged () method, This method is used to inform the UI main thread to draw the interface according to the new data when the adapter data is changed:
[Java]View PlainCopy print?
- @Override
- protected void Publishresults (charsequence constraint, filterresults results) {
- //noinspection Unchecked
- Mobjects = (list<t>) results.values;
- if (Results.count > 0) {
- Notifydatasetchanged ();
- } Else {
- Notifydatasetinvalidated ();
- }
- }
This is how data filtering is done.
An example is given below.
Layout file Filter_activity.xml:
[HTML]View PlainCopyprint?
- <? XML version= "1.0" encoding="Utf-8"?>
- <linearlayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:orientation="vertical" >
- <Searchview
- android:id="@+id/searchview1"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content" >
- </searchview>
- <ListView
- android:id="@+id/listview1"
- android:layout_width="match_parent"
- android:layout_height="wrap_content" >
- </ListView>
- </linearlayout>
class file Mainactivity.java:
[Java]View PlainCopy print?
- Package Com.zzj.ui.filterdemo;
- Import android.app.Activity;
- Import Android.os.Bundle;
- Import Android.widget.ArrayAdapter;
- Import Android.widget.ListView;
- Import Android.widget.SearchView;
- Import Android.widget.SearchView.OnQueryTextListener;
- Import COM.ZZJ.UI.R;
- Public class Mainactivity extends Activity implements Onquerytextlistener {
- Private ListView ListView;
- @Override
- protected void OnCreate (Bundle savedinstancestate) {
- super.oncreate (savedinstancestate);
- Setcontentview (r.layout.filter_activity);
- Searchview Searchview = (searchview) Findviewbyid (R.ID.SEARCHVIEW1);
- Searchview.setonquerytextlistener (this);
- Searchview.setsubmitbuttonenabled (false);
- Searchview.seticonifiedbydefault (false);
- ListView = (ListView) Findviewbyid (R.id.listview1);
- arrayadapter<string> adapter = New Arrayadapter<string> (This,
- Android. R.layout.simple_list_item_1, new string[] { "Bei jing",
- "Shang hai", "Chang Sha", "Chang Chun", "Nan jing" ,
- "Dong Jing", "Ji nan", "Qing Dao", "Xiang tan",
- "Zhu Zhou", "Heng Yang"});
- Listview.setadapter (adapter);
- //Turn on filter function
- Listview.settextfilterenabled (true);
- }
- @Override
- Public boolean onquerytextsubmit (String query) {
- return false;
- }
- @Override
- Public Boolean onquerytextchange (String newtext) {
- if (NewText = = Null | | newtext.length () = = 0) {
- Listview.cleartextfilter ();
- } Else {
- Listview.setfiltertext (NewText);
- }
- return true;
- }
- }
:
, A floating box pops up, which is Listview.setfiltertext (FilterText). If you do not want this floating box, you can get the filter first and then call Filter.filter (FilterText).
Modify the Searchview listener function as follows:
[Java]View PlainCopyprint?
- @Override
- Public Boolean onquerytextchange (String newtext) {
- ListAdapter adapter = Listview.getadapter ();
- if (adapter instanceof filterable) {
- Filter filter = ((filterable) adapter). GetFilter ();
- if (NewText = = Null | | newtext.length () = = 0) {
- Filter.filter (null);
- } Else {
- Filter.filter (NewText);
- }
- }
- return true;
- }
Using this method does not require the filter function of the ListView to be turned on. The effect is as follows:
The above is using the Arrayadapter filter function, we can also inherit the Baseadapter, and then implement the Filterable interface, define their own filters.
Android Data filter: Filter