Android Searchview 放置在 Listview 上时不触发

Android Searchview not firing when placed over Listview

我已经四处寻找了几个小时,我发现这个主题是 4 年多以前使用 EditText 或尝试 运行 菜单中的搜索框。

我想做的是使用现在方便的 SearchView 和 Listview 和自定义适配器(在您键入时进行过滤,甚至显示搜索结果)。不幸的是,这不太顺利,需要帮助。所以 - 列表视图可以很好地加载所有内容,甚至出现搜索框 - 但不幸的是,它在键入后什么也不做: 没有类型过滤,没有搜索 ,它似乎甚至没有搜索框.

这是文件的截断-

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>

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

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

行组织为:

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

最后是自定义适配器:

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