• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Apps Unable to filter list view items

AKA001

Newbie
Feb 10, 2016
10
0
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:
    import java.text.SimpleDateFormat;
    import java.util.ArrayList;
    import java.util.Date;
    import java.util.List;

    import android.app.Activity;
    import android.content.pm.PackageInfo;
    import android.content.pm.PackageManager;
    import android.graphics.drawable.Drawable;
    import android.util.Log;
    import android.view.LayoutInflater;
    import android.view.View;
    import android.view.ViewGroup;
    import android.widget.BaseAdapter;
    import android.widget.CheckBox;
    import android.widget.Filter;
    import android.widget.Filterable;
    import android.widget.TextView;

    public class Listadapter extends BaseAdapter implements Filterable {


       List<PackageInfo> packageList;
        List<PackageInfo> filteredData = null;
        List<PackageInfo> originalData = null;
        Activity context;
        PackageManager packageManager;
        boolean[] itemChecked;
        private boolean notifyChanged = true;
        ItemFilter mFilter = new ItemFilter();
    LayoutInflater mInflater;

        public Listadapter(Activity context,  List<PackageInfo> packageList,
                           PackageManager packageManager) {
            super();
            this.context = context;
            this.packageList = packageList;
            itemChecked = new boolean[packageList.size()];
            this.packageManager = packageManager;


            this.filteredData = packageList;
            this.originalData = packageList;
            mInflater = LayoutInflater.from(context);

        }




        private class ViewHolder {
            TextView apkName;
            CheckBox ck1;
        }

        public int getCount() {
            if(filteredData==null){
                Log.v("LOG", "Warn, null filteredData");
                return 0;
            }else{
                return filteredData.size();
            }
        }

        public Object getItem(int position) {
            return filteredData.get(position);
        }



        public long getItemId(int position) {
            return 0;
        }

        [USER=1021285]@override[/USER]
        public View getView(final int position, View convertView, ViewGroup parent) {
            final ViewHolder holder;

            LayoutInflater inflater = context.getLayoutInflater();

            if (convertView == null) {
                convertView = mInflater.inflate(R.layout.list_item, null);
                holder = new ViewHolder();

                holder.apkName = (TextView) convertView
                        .findViewById(R.id.textView1);
               // holder.ck1 = (CheckBox) convertView
                 //       .findViewById(R.id.checkBox1);

                convertView.setTag(holder);

            } else {

                holder = (ViewHolder) convertView.getTag();
            }

            PackageInfo packageInfo = (PackageInfo) getItem(position);

            SimpleDateFormat sdf = new SimpleDateFormat("[yyyy-MM-dd]");


            Drawable appIcon = packageManager
                    .getApplicationIcon(packageInfo.applicationInfo);
            //String packageName = packageInfo.packageName;

            String appName = packageManager.getApplicationLabel(
                    packageInfo.applicationInfo).toString() + "\n" + sdf.format(new Date(packageInfo.firstInstallTime));
           // String appName = packageManager.getApplicationLabel(
             //              packageInfo.applicationInfo).toString()+ "\n" + packageInfo.packageName ;

                    appIcon.setBounds(0, 0, 150, 150);
            holder.apkName.setCompoundDrawables(appIcon, null, null, null);
            holder.apkName.setCompoundDrawablePadding(15);
            holder.apkName.setText(appName);
           // holder.apkName.setText(packageName);
          //  holder.ck1.setChecked(false);

          //  if (itemChecked[position])
           //     holder.ck1.setChecked(true);
           // else
             //   holder.ck1.setChecked(false);
    /*
            holder.ck1.setOnClickListener(new OnClickListener() {
                [USER=1021285]@override[/USER]
                public void onClick(View v) {
                    // TODO Auto-generated method stub
                    if (holder.ck1.isChecked())
                        itemChecked[position] = true;
                    else
                        itemChecked[position] = false;
                }
            });

            if (itemChecked != null) {

            }
    */
            return convertView;

        }

        //////search filter

        [USER=1021285]@override[/USER]
        public Filter getFilter() {
            return mFilter;
        }

        private class ItemFilter extends Filter {
            [USER=1021285]@override[/USER]
            protected FilterResults performFiltering(CharSequence constraint) {

                String filterString = constraint.toString().toLowerCase();

                FilterResults results = new FilterResults();

                final List<PackageInfo> list = originalData;

                int count = packageList.size();
                final ArrayList<PackageInfo> nlist = new ArrayList<>(count);

                PackageInfo filterableString;

                for (int i = 0; i < count; i++) {
                    filterableString = list.get(i);
                    if (filterString.toLowerCase().contains(filterString)) {
                        nlist.add(filterableString);
                    }
                }

                results.values = nlist;
                results.count = nlist.size();

                return results;
            }

            @SuppressWarnings("unchecked")
            [USER=1021285]@override[/USER]
            protected void publishResults(CharSequence constraint, FilterResults results) {
                packageList = (ArrayList<PackageInfo>) results.values;
                notifyDataSetChanged();
            }

        }


        ///filter finished


    }


In the MainActivity.java, I have below code to act on the Searchview inside OnCreate:



    final Listadapter Adapter = new Listadapter(this, (ArrayList<PackageInfo>) packageList1, packageManager);

            apps.setAdapter(Adapter);


            //enables filtering for the contents of the given ListView
            apps.setTextFilterEnabled(true);

           final SearchView sv = (SearchView) findViewById(R.id.inputSearch);

       

            sv.setOnQueryTextListener(new SearchView.OnQueryTextListener() {


                [USER=1021285]@override[/USER]
                public boolean onQueryTextSubmit(String text) {
                    return false;
                }

                [USER=1021285]@override[/USER]
                public boolean onQueryTextChange(String text) {

                    Adapter.getFilter().filter(text);
                    return false;
                }
            });


On entering text in Searchview, it does nothing. It does not filter the list view items or gives any error.
 
Last edited by a moderator:

BEST TECH IN 2023

We've been tracking upcoming products and ranking the best tech since 2007. Thanks for trusting our opinion: we get rewarded through affiliate links that earn us a commission and we invite you to learn more about us.

Smartphones