Search code examples
androidandroid-listviewsearchview

Android Searchview not firing when placed over Listview


I've looked around for hours and i'll ive found on the topic are either over 4 years ago and using EditText or trying to run the searchbox from the menu.

What i would like to do is use the now handy SearchView with a Listview and Custom adapter (either filter as you type or even show search result). Which unfortunately does not go too well and would need a hand. So - the listview loads everything well, and even the searchbox appears - but unfortunately it does nothing after typing: no type filtering, no searching, it just seems as the box isn't even there.

Here are the cut-offs from the files-

Activity XML:

 <SearchView
        android:id="@+id/searchView"
        android:layout_width="match_parent"
        android:layout_height="60dp"
        android:queryHint="Search..."/>

    <ListView
        android:id="@+id/fullListView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="9"
        android:divider="@color/menuColor"
        android:dividerHeight="6dp" >
     </ListView>

Row XML:

 <TextView
        android:id="@+id/textHeading"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="TextView"
        android:textColor="@color/textColor"
        android:textSize="22dp"
        android:textStyle="bold" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">

        <TextView
            android:id="@+id/textType"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="TextView"
            android:textColor="@color/textColor"
            android:textSize="14dp"
            android:textStyle="italic" />

    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:weightSum="2">

        <TextView
            android:id="@+id/textCounty"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="TextView"
            android:textColor="@color/textColor"
            android:textSize="16dp"
            android:textStyle="italic" />

        <TextView
            android:id="@+id/textTown"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="TextView"
            android:textColor="@color/textColor"
            android:textSize="14dp"
            android:textStyle="italic" />

Row Class:

public class RowItem {

    private String heading;
    private String type;
    private String county;
    private String town;

    public void setHeading( String theHeading ) {
        this.heading = theHeading;    }
    public String getHeading() {
        return this.heading;
    }

    public void setType( String theType ) {
        this.type = theType;
    }
    public String getType() {
        return this.type;
    }

    public void setCounty( String theCounty ) {
        this.county = theCounty;
    }
    public String getCounty() {
        return this.county;
    }

    public void setTown( String theTown ) {
        this.town = theTown;
    }
    public String getTown() {
        return this.town;
    }
}

Activity Class:


//ListView populate
        myRowItems = new ArrayList<RowItem>();
        fullListView = (ListView) findViewById(R.id.fullListView);
        fillArrayList();
        final CustomAdapter myAdapter = new CustomAdapter(getApplicationContext(), myRowItems);
        fullListView.setAdapter( myAdapter );
        fullListView.setTextFilterEnabled(true);

//Searchies
        searchView = findViewById(R.id.searchView);

        searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
            @Override
            public boolean onQueryTextSubmit(String query) {
                return false;
            }

            @Override
            public boolean onQueryTextChange(String newText) {
                myAdapter.getFilter().filter(newText);
                return false;
            }
        });

with the rows organized as:

        RowItem row_001 = new RowItem( );
        row_001.setHeading("PlaceName1");
        row_001.setType("Type1");
        row_001.setCounty("Potato County");
        row_001.setTown("Potato Town");
        myRowItems.add( row_001 );

And finally CustomAdapter:

public class CustomAdapter extends BaseAdapter implements Filterable {

    private ArrayList<RowItem> singleRow, singleRowFiltered;
    private LayoutInflater thisInflater;

    public CustomAdapter(Context context, ArrayList<RowItem> aRow) {

        this.singleRow = aRow;
        thisInflater = ( LayoutInflater.from(context) );

    }

    @Override
    public int getCount() {
        return singleRow.size( );
    }

    @Override
    public int getViewTypeCount() {
        return getCount();
    }

    @Override
    public int getItemViewType(int position) {
        return position;
    }

    @Override
    public Object getItem (int position) {
        return singleRow.get( position );
    }

    @Override
    public long getItemId (int position) {
        return position;
    }

    @Override
    public View getView (int position, View convertView, ViewGroup parent) {
        if (convertView == null) {
            convertView = thisInflater.inflate( R.layout.list_view_row, parent, false );

            TextView theHeading = (TextView) convertView.findViewById(R.id.textHeading);
            TextView theType = (TextView) convertView.findViewById(R.id.textType);
            TextView theCounty = (TextView) convertView.findViewById(R.id.textCounty);
            TextView theTown = (TextView) convertView.findViewById(R.id.textTown);

            RowItem currentRow = (RowItem) getItem(position);

            theHeading.setText( currentRow.getHeading() );
            theType.setText( currentRow.getType() );
            theCounty.setText( currentRow.getCounty() );
            theTown.setText( currentRow.getTown() );

        }
        return convertView;
    }

//Filter SearchView
    @Override
    public Filter getFilter() {
        Filter filter = new Filter() {
            @Override
            protected FilterResults performFiltering(CharSequence constraint) {
                FilterResults results = new FilterResults();

                ArrayList<RowItem> arrayListFilter = new ArrayList<RowItem>();

                if (constraint == null || constraint.length() == 0) {
                    results.count = singleRow.size();
                    results.values = singleRow;
                } else {
                    for (RowItem rowItem : singleRow) {
                        if (rowItem.getHeading().toLowerCase().contains(constraint.toString().toLowerCase())) {
                            arrayListFilter.add(rowItem);
                        }
                    }
                    results.count = arrayListFilter.size();
                    results.values = arrayListFilter;

                }
                return results;
            }

            @Override
            protected void publishResults(CharSequence constraint, FilterResults results) {
                singleRowFiltered = (ArrayList<RowItem>) results.values;
                notifyDataSetChanged();
            }
        };
        return filter;
    }
}

Thanks in advance for all the help i can get!


Solution

  • Try below code:

    public class CustomAdapter extends BaseAdapter implements Filterable {
    
    private ArrayList<RowItem> singleRow, singleRowFiltered;
    private LayoutInflater thisInflater;
    
    public CustomAdapter(Context context, ArrayList<RowItem> aRow) {
        this.singleRow = aRow;
        singleRowFiltered = new ArrayList<>(singleRow);
        thisInflater = ( LayoutInflater.from(context) );
    }
    
    @Override
    public int getCount() {
        return singleRowFiltered.size( );
    }
    
    @Override
    public int getViewTypeCount() {
        return getCount();
    }
    
    @Override
    public int getItemViewType(int position) {
        return position;
    }
    
    @Override
    public RowItem getItem (int position) {
        return singleRowFiltered.get( position );
    }
    
    @Override
    public long getItemId (int position) {
        return position;
    }
    
    @Override
    public View getView (int position, View convertView, ViewGroup parent) {
        if (convertView == null) {
            convertView = thisInflater.inflate( R.layout.list_view_row, parent, false );
        }
        TextView theHeading = (TextView) convertView.findViewById(R.id.textHeading);
        TextView theType = (TextView) convertView.findViewById(R.id.textType);
        TextView theCounty = (TextView) convertView.findViewById(R.id.textCounty);
        TextView theTown = (TextView) convertView.findViewById(R.id.textTown);
    
        RowItem currentRow = getItem(position);
    
        theHeading.setText(currentRow.getHeading() );
        theType.setText(currentRow.getType() );
        theCounty.setText(currentRow.getCounty() );
        theTown.setText(currentRow.getTown() );
    
        return convertView;
    }
    
    //Filter SearchView
    @Override
    public Filter getFilter() {
        Filter filter = new Filter() {
            @Override
            protected FilterResults performFiltering(CharSequence constraint) {
                FilterResults results = new FilterResults();
    
                ArrayList<RowItem> arrayListFilter = new ArrayList<>();
    
                if (constraint == null || constraint.length() == 0) {
                    results.count = singleRow.size();
                    results.values = singleRow;
                } else {
                    for (RowItem rowItem : singleRow) {
                        if (rowItem.getHeading().toLowerCase().contains(constraint.toString().toLowerCase())) {
                            arrayListFilter.add(rowItem);
                        }
                    }
                    results.count = arrayListFilter.size();
                    results.values = arrayListFilter;
                }
                return results;
            }
    
            @Override
            protected void publishResults(CharSequence constraint, FilterResults results) {
                singleRowFiltered = (ArrayList<RowItem>) results.values;
                notifyDataSetChanged();
            }
        };
        return filter;
    }
    }