Android Data filter: Filter

Source: Internet
Author: User

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?
  1. Public void Setfiltertext (String filtertext) {
  2. //Todo:should We check for Acceptfilter ()?
  3. if (mtextfilterenabled &&!) Textutils.isempty (FilterText)) {
  4. Createtextfilter (false);
  5. //This is going-to-listener ontextchanged, but we might not
  6. //Be ready to bring up a window yet
  7. Mtextfilter.settext (FilterText);
  8. Mtextfilter.setselection (Filtertext.length ());
  9. if (madapter instanceof filterable) {
  10. //If Mpopup is non-null and then ontextchanged'll do the filtering
  11. if (mpopup = = null) {
  12. Filter f = ((filterable) madapter). GetFilter ();
  13. F.filter (FilterText);
  14. }
  15. //Set filtered to true so we'll display the filter window when our main
  16. //Window is ready
  17. mfiltered = true;
  18. Mdatasetobserver.clearsavedstate ();
  19. }
  20. }
  21. }

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?
  1. @Override
  2. protected void Publishresults (charsequence constraint, filterresults results) {
  3. //noinspection Unchecked
  4. Mobjects = (list<t>) results.values;
  5. if (Results.count > 0) {
  6. Notifydatasetchanged ();
  7. } Else {
  8. Notifydatasetinvalidated ();
  9. }
  10. }

This is how data filtering is done.

An example is given below.

Layout file Filter_activity.xml:

[HTML]View PlainCopyprint?
  1. <? XML version= "1.0" encoding="Utf-8"?>
  2. <linearlayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:layout_width="match_parent"
  4. android:layout_height="match_parent"
  5. android:orientation="vertical" >
  6. <Searchview
  7. android:id="@+id/searchview1"
  8. android:layout_width="wrap_content"
  9. android:layout_height="wrap_content" >
  10. </searchview>
  11. <ListView
  12. android:id="@+id/listview1"
  13. android:layout_width="match_parent"
  14. android:layout_height="wrap_content" >
  15. </ListView>
  16. </linearlayout>

class file Mainactivity.java:

[Java]View PlainCopy print?
  1. Package Com.zzj.ui.filterdemo;
  2. Import android.app.Activity;
  3. Import Android.os.Bundle;
  4. Import Android.widget.ArrayAdapter;
  5. Import Android.widget.ListView;
  6. Import Android.widget.SearchView;
  7. Import Android.widget.SearchView.OnQueryTextListener;
  8. Import COM.ZZJ.UI.R;
  9. Public class Mainactivity extends Activity implements Onquerytextlistener {
  10. Private ListView ListView;
  11. @Override
  12. protected void OnCreate (Bundle savedinstancestate) {
  13. super.oncreate (savedinstancestate);
  14. Setcontentview (r.layout.filter_activity);
  15. Searchview Searchview = (searchview) Findviewbyid (R.ID.SEARCHVIEW1);
  16. Searchview.setonquerytextlistener (this);
  17. Searchview.setsubmitbuttonenabled (false);
  18. Searchview.seticonifiedbydefault (false);
  19. ListView = (ListView) Findviewbyid (R.id.listview1);
  20. arrayadapter<string> adapter = New Arrayadapter<string> (This,
  21. Android. R.layout.simple_list_item_1, new string[] { "Bei jing",
  22. "Shang hai", "Chang Sha", "Chang Chun", "Nan jing" ,
  23. "Dong Jing", "Ji nan", "Qing Dao", "Xiang tan",
  24. "Zhu Zhou", "Heng Yang"});
  25. Listview.setadapter (adapter);
  26. //Turn on filter function
  27. Listview.settextfilterenabled (true);
  28. }
  29. @Override
  30. Public boolean onquerytextsubmit (String query) {
  31. return false;
  32. }
  33. @Override
  34. Public Boolean onquerytextchange (String newtext) {
  35. if (NewText = = Null | | newtext.length () = = 0) {
  36. Listview.cleartextfilter ();
  37. } Else {
  38. Listview.setfiltertext (NewText);
  39. }
  40. return true;
  41. }
  42. }

, 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?
  1. @Override
  2. Public Boolean onquerytextchange (String newtext) {
  3. ListAdapter adapter = Listview.getadapter ();
  4. if (adapter instanceof filterable) {
  5. Filter filter = ((filterable) adapter). GetFilter ();
  6. if (NewText = = Null | | newtext.length () = = 0) {
  7. Filter.filter (null);
  8. } Else {
  9. Filter.filter (NewText);
  10. }
  11. }
  12. return true;
  13. }

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.