放置在 Listview 上时,Android Searchview 不会触发
Posted
技术标签:
【中文标题】放置在 Listview 上时,Android Searchview 不会触发【英文标题】:Android Searchview not firing when placed over Listview 【发布时间】:2021-01-12 07:33:55 【问题描述】:我已经环顾了好几个小时,我发现这个主题要么是 4 年前,要么是使用 EditText,要么是尝试从菜单中运行搜索框。
我想做的是使用现在方便的 SearchView 和 Listview 和自定义适配器(在您键入时进行过滤,甚至显示搜索结果)。不幸的是,这并不顺利,需要帮助。所以 - 列表视图很好地加载了所有内容,甚至出现了搜索框 - 但不幸的是,它在键入后什么也没做:没有类型过滤,没有搜索,看起来这个框甚至都不存在。
这是文件的截取部分-
活动 XML:
<SearchView
android:id="@+id/searchView"
android:layout_
android:layout_
android:queryHint="Search..."/>
<ListView
android:id="@+id/fullListView"
android:layout_
android:layout_
android:layout_weight="9"
android:divider="@color/menuColor"
android:dividerHeight="6dp" >
</ListView>
行 XML:
<TextView
android:id="@+id/textHeading"
android:layout_
android:layout_
android:text="TextView"
android:textColor="@color/textColor"
android:textSize="22dp"
android:textStyle="bold" />
<LinearLayout
android:layout_
android:layout_
android:orientation="vertical">
<TextView
android:id="@+id/textType"
android:layout_
android:layout_
android:text="TextView"
android:textColor="@color/textColor"
android:textSize="14dp"
android:textStyle="italic" />
</LinearLayout>
<LinearLayout
android:layout_
android:layout_
android:weightSum="2">
<TextView
android:id="@+id/textCounty"
android:layout_
android:layout_
android:layout_weight="1"
android:text="TextView"
android:textColor="@color/textColor"
android:textSize="16dp"
android:textStyle="italic" />
<TextView
android:id="@+id/textTown"
android:layout_
android:layout_
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;
提前感谢我能得到的所有帮助!
【问题讨论】:
【参考方案1】:试试下面的代码:
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;
【讨论】:
以上是关于放置在 Listview 上时,Android Searchview 不会触发的主要内容,如果未能解决你的问题,请参考以下文章
Android ListActivity - 如何在 ListView 下方添加视图?