将Android Searchview放在Listview上时不触发

问题描述

我已经逛了好几个小时,我发现这个主题已经有4年多了,并使用EditText或尝试从菜单中运行搜索框。

我想做的是使用现在方便的 SearchView和Listview 和“自定义”适配器(键入时过滤,甚至显示搜索结果)。不幸的是,它进行得不太好,需要帮助。因此-列表视图可以很好地加载所有内容,甚至会出现搜索框-但不幸的是,键入后它什么也没做:没有类型过滤,没有搜索,只是看起来好像盒子不在那儿。>

以下是文件的摘录部分-

活动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>

行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" />

行级:

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;
    }
}

活动类别:


//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;
            }
        });

行的组织方式为:

        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 );

最后是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;
    }
}

在此先感谢我能获得的所有帮助!

解决方法

尝试以下代码:

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;
}
}