为啥顶部栏中的搜索在 android studio 中不起作用?
Posted
技术标签:
【中文标题】为啥顶部栏中的搜索在 android studio 中不起作用?【英文标题】:Why is search in top bar not working in android studio?为什么顶部栏中的搜索在 android studio 中不起作用? 【发布时间】:2021-12-09 22:13:08 【问题描述】:我用listView
制作了一个应用程序,我想在顶部栏中使用搜索,但它不起作用。
在这个程序中,我从 firebase 获取数据。
这些是下面的代码:
这是MainActivity
代码:
public class MainActivity extends AppCompatActivity implements LoaderManager.LoaderCallbacks<Cursor>
private static final int List_APP_LOADER = 0;
ArrayList<Informatin> listItem;
ListView listView;
listAdapter adapter;
DatabaseReference reference;
ArrayList<Informatin> mInformation;
@Override
protected void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
listItem = new ArrayList<>();
listView = findViewById(R.id.list);
LoaderManager.getInstance(this).initLoader(LIST_APP_LOADER, null, this);
mInformation = new ArrayList<>();
adapter = new listAdapter(this, mInformation);
listView.setAdapter(adapter);
@Override
public boolean onCreateOptionsMenu(Menu menu)
super.onCreateOptionsMenu(menu);
getMenuInflater().inflate(R.menu.menu_main_screen, menu);
MenuItem searchItem = menu.findItem(R.id.search_item);
SearchView searchView = (SearchView) searchItem.getActionView();
searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener()
@Override
public boolean onQueryTextSubmit(String query)
return false;
@Override
public boolean onQueryTextChange(String newText)
listView.getFilter().filter(newText);
return true;
);
return true;
这是listAdapter
代码
public class listAdapter extends ArrayAdapter<Informatin> implements Filterable
List<Informatin> arrayList;
Context context;
public listAdapter(Context context, ArrayList<Informatin> informatins)
super(context, 0, informatins);
this.context=context;
arrayList = informatins;
@Override
public View getView(int position, View convertView, ViewGroup parent)
View listItemView = convertView;
if (listItemView == null)
listItemView = LayoutInflater.from(getContext()).inflate(
R.layout.list_item, parent, false);
Informatin currentList = getItem(position);
TextView nameTextView = listItemView.findViewById(R.id.name);
TextView descriptionTextView = listItemView.findViewById(R.id.description);
TextView ageTextView = listItemView.findViewById(R.id.age);
ImageView imageView = listItemView.findViewById(R.id.image);
String firstName = currentList.getFirstName();
String lastName = currentList.getLastName();
String name = firstName + " " + lastName;
nameTextView.setText(name);
descriptionTextView.setText(currentList.getDescription());
ageTextView.setText(currentList.getAge());
return listItemView;
List<Informatin> informatins;
public Filter getFilter()
return new Filter()
@Override
protected FilterResults performFiltering(CharSequence constraint)
final FilterResults oReturn = new FilterResults();
final ArrayList<Informatin> results = new ArrayList<>();
if (informatins == null)
informatins = arrayList;
if (constraint != null)
if (informatins != null && informatins.size() > 0)
for (final Informatin g : informatins)
if (g.getFirstName().toLowerCase()
.contains(constraint.toString()))
results.add(g);
oReturn.values = results;
return oReturn;
@SuppressWarnings("unchecked")
@Override
protected void publishResults(CharSequence constraint,
FilterResults results)
arrayList = (ArrayList<Informatin>) results.values;
notifyDataSetChanged();
;
this is the picture of the problem
this is a screenshot for the problem from the device
【问题讨论】:
public boolean onQueryTextChange(String newText) adapter.filter(newList);返回假; 您是如何实现搜索过滤器的?分享您的列表适配器代码。 @Danish 我已经添加了上面的代码 @MohamedHossam 在你做`adapter.filter(newList);`之后你需要做adapter.notifyDataSetChanged()
@akhilnair 我输入了这段代码,但什么也没发生
【参考方案1】:
将此添加到您的列表适配器代码中:
Context context;
public listAdapter(Context context, List<Informatin> informatins)
super();
this.context=context;
arraylist=informatins;
ArrayList<Informatin> informatins;
public Filter getFilter()
return new Filter()
@Override
protected FilterResults performFiltering(CharSequence constraint)
final FilterResults oReturn = new FilterResults();
final ArrayList<Informatin> results = new ArrayList<Informatin>();
if (informatins == null)
informatins = arraylist;
if (constraint != null)
if (informatins != null && informatins.size() > 0)
for (final Information g : informatins)
if (g.getName().toLowerCase()
.contains(constraint.toString()))
results.add(g);
oReturn.values = results;
return oReturn;
@SuppressWarnings("unchecked")
@Override
protected void publishResults(CharSequence constraint,
FilterResults results)
arraylist = (ArrayList<Informatin>) results.values;
notifyDataSetChanged();
;
在你的 onquerytextchaged 中添加这个:
searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener()
@Override
public boolean onQueryTextSubmit(String query)
return false;
@Override
public boolean onQueryTextChange(String newText)
adapter.getFilter().filter(newText);//replace adapter with listview name which in your case is listview as informed by you in comment
return true;
);
return true;
修改MainActivity onCreate()代码:
@Override
protected void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
listItem = new ArrayList<>();
listView = findViewById(R.id.list);
LoaderManager.getInstance(this).initLoader(LIST_APP_LOADER, null, this);
mInformation = new ArrayList<>();
adapter = new listAdapter(this, mInformation);
listView.setAdapter(adapter);
listView.setTextFilterEnabled(true)//add this line
【讨论】:
什么是employeeArrayList 我已经编辑了答案。 我的意思是这条线informatins = employeeArrayList;
对不起,这是我的错误。现在检查
你的代码没有问题但是问题是在`listAdapter file I was using this code
public class listAdapter extends ArrayAdapter以上是关于为啥顶部栏中的搜索在 android studio 中不起作用?的主要内容,如果未能解决你的问题,请参考以下文章
为啥我的 phongap 应用程序在 android 状态栏中显示一个汉堡包图标?