1. Download our Official Android App: Forums for Android!

Apps Unable to filter list view items

Discussion in 'Android Development' started by AKA001, Feb 24, 2016.

  1. AKA001

    AKA001 Lurker
    Thread Starter
    Rank:
    None
    Points:
    5
    Posts:
    9
    Joined:
    Feb 10, 2016

    Feb 10, 2016
    9
    0
    5
    Hello,

    I am trying to list all the installed applications and filter the result in the list view dynamically. below is my code in the listadapter.java:

    Code (Text):
    1.  
    2.     import java.text.SimpleDateFormat;
    3.     import java.util.ArrayList;
    4.     import java.util.Date;
    5.     import java.util.List;
    6.  
    7.     import android.app.Activity;
    8.     import android.content.pm.PackageInfo;
    9.     import android.content.pm.PackageManager;
    10.     import android.graphics.drawable.Drawable;
    11.     import android.util.Log;
    12.     import android.view.LayoutInflater;
    13.     import android.view.View;
    14.     import android.view.ViewGroup;
    15.     import android.widget.BaseAdapter;
    16.     import android.widget.CheckBox;
    17.     import android.widget.Filter;
    18.     import android.widget.Filterable;
    19.     import android.widget.TextView;
    20.  
    21.     public class Listadapter extends BaseAdapter implements Filterable {
    22.  
    23.  
    24.        List<PackageInfo> packageList;
    25.         List<PackageInfo> filteredData = null;
    26.         List<PackageInfo> originalData = null;
    27.         Activity context;
    28.         PackageManager packageManager;
    29.         boolean[] itemChecked;
    30.         private boolean notifyChanged = true;
    31.         ItemFilter mFilter = new ItemFilter();
    32.     LayoutInflater mInflater;
    33.  
    34.         public Listadapter(Activity context,  List<PackageInfo> packageList,
    35.                            PackageManager packageManager) {
    36.             super();
    37.             this.context = context;
    38.             this.packageList = packageList;
    39.             itemChecked = new boolean[packageList.size()];
    40.             this.packageManager = packageManager;
    41.  
    42.  
    43.             this.filteredData = packageList;
    44.             this.originalData = packageList;
    45.             mInflater = LayoutInflater.from(context);
    46.  
    47.         }
    48.  
    49.  
    50.  
    51.  
    52.         private class ViewHolder {
    53.             TextView apkName;
    54.             CheckBox ck1;
    55.         }
    56.  
    57.         public int getCount() {
    58.             if(filteredData==null){
    59.                 Log.v("LOG", "Warn, null filteredData");
    60.                 return 0;
    61.             }else{
    62.                 return filteredData.size();
    63.             }
    64.         }
    65.  
    66.         public Object getItem(int position) {
    67.             return filteredData.get(position);
    68.         }
    69.  
    70.  
    71.  
    72.         public long getItemId(int position) {
    73.             return 0;
    74.         }
    75.  
    76.         [USER=1021285]@override[/USER]
    77.         public View getView(final int position, View convertView, ViewGroup parent) {
    78.             final ViewHolder holder;
    79.  
    80.             LayoutInflater inflater = context.getLayoutInflater();
    81.  
    82.             if (convertView == null) {
    83.                 convertView = mInflater.inflate(R.layout.list_item, null);
    84.                 holder = new ViewHolder();
    85.  
    86.                 holder.apkName = (TextView) convertView
    87.                         .findViewById(R.id.textView1);
    88.                // holder.ck1 = (CheckBox) convertView
    89.                  //       .findViewById(R.id.checkBox1);
    90.  
    91.                 convertView.setTag(holder);
    92.  
    93.             } else {
    94.  
    95.                 holder = (ViewHolder) convertView.getTag();
    96.             }
    97.  
    98.             PackageInfo packageInfo = (PackageInfo) getItem(position);
    99.  
    100.             SimpleDateFormat sdf = new SimpleDateFormat("[yyyy-MM-dd]");
    101.  
    102.  
    103.             Drawable appIcon = packageManager
    104.                     .getApplicationIcon(packageInfo.applicationInfo);
    105.             //String packageName = packageInfo.packageName;
    106.  
    107.             String appName = packageManager.getApplicationLabel(
    108.                     packageInfo.applicationInfo).toString() + "\n" + sdf.format(new Date(packageInfo.firstInstallTime));
    109.            // String appName = packageManager.getApplicationLabel(
    110.              //              packageInfo.applicationInfo).toString()+ "\n" + packageInfo.packageName ;
    111.  
    112.                     appIcon.setBounds(0, 0, 150, 150);
    113.             holder.apkName.setCompoundDrawables(appIcon, null, null, null);
    114.             holder.apkName.setCompoundDrawablePadding(15);
    115.             holder.apkName.setText(appName);
    116.            // holder.apkName.setText(packageName);
    117.           //  holder.ck1.setChecked(false);
    118.  
    119.           //  if (itemChecked[position])
    120.            //     holder.ck1.setChecked(true);
    121.            // else
    122.              //   holder.ck1.setChecked(false);
    123.     /*
    124.             holder.ck1.setOnClickListener(new OnClickListener() {
    125.                 [USER=1021285]@override[/USER]
    126.                 public void onClick(View v) {
    127.                     // TODO Auto-generated method stub
    128.                     if (holder.ck1.isChecked())
    129.                         itemChecked[position] = true;
    130.                     else
    131.                         itemChecked[position] = false;
    132.                 }
    133.             });
    134.  
    135.             if (itemChecked != null) {
    136.  
    137.             }
    138.     */
    139.             return convertView;
    140.  
    141.         }
    142.  
    143.         //////search filter
    144.  
    145.         [USER=1021285]@override[/USER]
    146.         public Filter getFilter() {
    147.             return mFilter;
    148.         }
    149.  
    150.         private class ItemFilter extends Filter {
    151.             [USER=1021285]@override[/USER]
    152.             protected FilterResults performFiltering(CharSequence constraint) {
    153.  
    154.                 String filterString = constraint.toString().toLowerCase();
    155.  
    156.                 FilterResults results = new FilterResults();
    157.  
    158.                 final List<PackageInfo> list = originalData;
    159.  
    160.                 int count = packageList.size();
    161.                 final ArrayList<PackageInfo> nlist = new ArrayList<>(count);
    162.  
    163.                 PackageInfo filterableString;
    164.  
    165.                 for (int i = 0; i < count; i++) {
    166.                     filterableString = list.get(i);
    167.                     if (filterString.toLowerCase().contains(filterString)) {
    168.                         nlist.add(filterableString);
    169.                     }
    170.                 }
    171.  
    172.                 results.values = nlist;
    173.                 results.count = nlist.size();
    174.  
    175.                 return results;
    176.             }
    177.  
    178.             @SuppressWarnings("unchecked")
    179.             [USER=1021285]@override[/USER]
    180.             protected void publishResults(CharSequence constraint, FilterResults results) {
    181.                 packageList = (ArrayList<PackageInfo>) results.values;
    182.                 notifyDataSetChanged();
    183.             }
    184.  
    185.         }
    186.  
    187.  
    188.         ///filter finished
    189.  
    190.  
    191.     }
    192.  
    193.  
    194. In the MainActivity.java, I have below code to act on the Searchview inside OnCreate:
    195.  
    196.  
    197.  
    198.     final Listadapter Adapter = new Listadapter(this, (ArrayList<PackageInfo>) packageList1, packageManager);
    199.  
    200.             apps.setAdapter(Adapter);
    201.  
    202.  
    203.             //enables filtering for the contents of the given ListView
    204.             apps.setTextFilterEnabled(true);
    205.  
    206.            final SearchView sv = (SearchView) findViewById(R.id.inputSearch);
    207.  
    208.        
    209.  
    210.             sv.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
    211.  
    212.  
    213.                 [USER=1021285]@override[/USER]
    214.                 public boolean onQueryTextSubmit(String text) {
    215.                     return false;
    216.                 }
    217.  
    218.                 [USER=1021285]@override[/USER]
    219.                 public boolean onQueryTextChange(String text) {
    220.  
    221.                     Adapter.getFilter().filter(text);
    222.                     return false;
    223.                 }
    224.             });

    On entering text in Searchview, it does nothing. It does not filter the list view items or gives any error.
     

    Advertisement

    #1 AKA001, Feb 24, 2016
    Last edited by a moderator: Feb 24, 2016
  2. LV426

    LV426 I say we take off and nuke this place from orbit
    Recognized Developer
    Rank:
     #12
    Points:
    1,988
    Posts:
    9,176
    Joined:
    Oct 16, 2015

    Oct 16, 2015
    9,176
    13,681
    1,988
    Male
    Software developer
    South West of England
    scary alien likes this.
  3. scary alien

    scary alien not really so scary
    Moderator
    Rank:
     #9
    Points:
    2,138
    Posts:
    22,319
    Joined:
    Mar 5, 2010

    Mar 5, 2010
    22,319
    23,814
    2,138
    Male
    space alien ;)
    Indy
    I edited their post above to add/use the code tags :).

    Cheers!
     
    LV426 likes this.
  4. LV426

    LV426 I say we take off and nuke this place from orbit
    Recognized Developer
    Rank:
     #12
    Points:
    1,988
    Posts:
    9,176
    Joined:
    Oct 16, 2015

    Oct 16, 2015
    9,176
    13,681
    1,988
    Male
    Software developer
    South West of England
    Marvelous! Couple of things -

    I would make onQueryTextChange() return true, not false.

    Set breakpoints in onQueryTextChange() and performFiltering() to verify that these methods are actually being called.
     
    scary alien likes this.

Share This Page

Loading...