项目未在 SearchView 工具栏界面中弹出 Android Studio java
items are not popping up in the SearchView toolbar interface Android Studio java
大家好,我想在我的工具栏上实现一个搜索界面来搜索我的 GridView 中的项目。但是它出现在我的工具栏上,但是当我单击搜索图标并开始输入时,没有弹出任何内容。我不知道目前的问题在哪里。我使用自定义适配器,所以我相信它还有更多工作要做。
这是我自定义的 ArrayAdapter class (WordAdapter)。关注 getView() 函数之后的内容,因为我认为这是 class 中最相关的部分。我还添加了一些全局变量,例如 ArrayList<> list 和 listFull,因为我相信我需要整个项目列表的一些副本,但老实说我不确定如何去做。
//variable responsible for making checkbox visible or not
private boolean displayCheckBox;
private ArrayList<WordFolder> original_list;
private ArrayList<WordFolder> new_list;
//constructor - it takes the context and the list of words
WordAdapter(Context context, ArrayList<WordFolder> word){
super(context, 0, word);
//creating a copy of the ArrayList containing all the folders name
original_list = new ArrayList<>(word);
new_list = new ArrayList<>(word);
}
//sets the visibility of the checkBox
public void setCheckBoxVisibility(boolean visible){
this.displayCheckBox = visible;
}
@Override
public View getView(int position, View convertView, ViewGroup parent){
View listItemView = convertView;
if(listItemView == null){
listItemView = LayoutInflater.from(getContext()).inflate(R.layout.folder_view, parent, false);
}
//getting the checkBox view id
CheckBox checkBox = (CheckBox) listItemView.findViewById(R.id.check_box);
checkBox.setVisibility(displayCheckBox ? View.VISIBLE : View.GONE);
//Getting the current word
final WordFolder currentWord = getItem(position);
//making the 3 text view to match our word_folder.xml
TextView date_created = (TextView) listItemView.findViewById(R.id.date_created);
TextView title = (TextView) listItemView.findViewById(R.id.title);
TextView desc = (TextView) listItemView.findViewById(R.id.desc);
//using the setText to get the text and set it in the textView
date_created.setText(currentWord.getDateCreated());
title.setText(currentWord.getTitle());
desc.setText(currentWord.getTitleDesc());
//call automatically when checkbox is changed
checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener(){
@Override
//compound button = the view of the button
//b = the new state of the checkbox
public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
//set the value of the checkbox to the CurrentWord
currentWord.setChecked(b);
}
});
return listItemView;
}
@NonNull
@Override
public Filter getFilter() {
Filter filter = new Filter() {
@Override
protected FilterResults performFiltering(CharSequence charSequence) {
FilterResults filterResults = new FilterResults();
if (charSequence == null || charSequence.length() == 0) {
filterResults.count = original_list.size();
filterResults.values = original_list;
} else {
String searchStr = charSequence.toString().toLowerCase();
ArrayList<WordFolder> results = new ArrayList<>();
for (WordFolder item : original_list) {
if (item.getTitle().contains(searchStr)) {
results.add(item);
}
filterResults.count = results.size();
filterResults.values = results;
}
}
//new list which contains only filtered items
ArrayList<WordFolder> filteredList = new ArrayList<>();
if(charSequence == null || charSequence.length() == 0){
filteredList.addAll(original_list);
}
else{
String filterPattern = charSequence.toString().toLowerCase().trim();
for(WordFolder item : original_list){
if(item.getTitle().toLowerCase().contains(filterPattern)){
filteredList.add(item);
}
}
}
FilterResults results = new FilterResults();
results.values = filteredList;
return results;
}
@Override
protected void publishResults(CharSequence charSequence, FilterResults filterResults) {
new_list.clear();
new_list.addAll((ArrayList) filterResults.values);
notifyDataSetChanged();
}
};
return filter;
}
}
下面是我在MainActivity中创建Search界面的部分代码,供参考。顺便说一句,itemadapter 是我的自定义适配器变量的名称
public boolean onCreateOptionsMenu(Menu menu) {
// initialize menu inflater
MenuInflater inflater = getMenuInflater();
if(whichToolbar == 0){
//inflate menu
inflater.inflate(R.menu.menu_search, menu);
SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
SearchView searchView = (SearchView) menu.findItem(R.id.search).getActionView();
searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
searchView.setIconifiedByDefault(false);
//initialize menu item
MenuItem searchItem = menu.findItem(R.id.search);));
searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
@Override
public boolean onQueryTextSubmit(String s) {
itemAdapter.getFilter().filter(s);
return true;
}
@Override
public boolean onQueryTextChange(String newText) {
itemAdapter.getFilter().filter(newText);
return true;
}
});
searchView.setOnCloseListener(new SearchView.OnCloseListener() {
@Override
public boolean onClose() {
itemAdapter.getFilter().filter("");
return false;
}
});
}
else{
inflater.inflate(R.menu.delete_menu, menu);
}
return true;
}
由于问题尚未得到解答,我更新了此内容。我一直在关注和观看 Youtube 视频(不仅仅是 1 个视频)和 android 开发人员指南,并确保代码“有效”,但我的搜索栏目前没有做任何事情。
在你的适配器中使用这个:
private ArrayList<WordFolder> actualdata = new ArrayList<WordFolder>();
private ArrayList<WordFolder> orignallist= new ArrayList<>();
WordAdapter(Context context, ArrayList<WordFolder> word){
super(context, 0, word);
this.actualdata=new ArrayList<WordFolder>();
this.orignallist=new ArrayList<WordFolder>();
actualdata.addAll(word);//use this to set up your view
orignallist.addAll(word);
}
public void filterData(String query){
query=query.toLowerCase();
actualdata.clear();
if(query.isEmpty()){
actualdata.addAll(orignallist);
}
else {
ArrayList<WordFolder> newlist = new ArrayList<>();
for(WordFolder gd: orignallist) {
if ((gd.getTitle().contains(query)) ) {
newlist.add(gd);
}
}
if(newlist.size()> 0){
actualdata.addAll(newlist);
}
}
notifyDataSetChanged();
}
然后将主类 itemAdapter.getfilter()
更改为 itemAdapter.filterData("your query")
现在应该可以了
大家好,我想在我的工具栏上实现一个搜索界面来搜索我的 GridView 中的项目。但是它出现在我的工具栏上,但是当我单击搜索图标并开始输入时,没有弹出任何内容。我不知道目前的问题在哪里。我使用自定义适配器,所以我相信它还有更多工作要做。
这是我自定义的 ArrayAdapter class (WordAdapter)。关注 getView() 函数之后的内容,因为我认为这是 class 中最相关的部分。我还添加了一些全局变量,例如 ArrayList<> list 和 listFull,因为我相信我需要整个项目列表的一些副本,但老实说我不确定如何去做。
//variable responsible for making checkbox visible or not
private boolean displayCheckBox;
private ArrayList<WordFolder> original_list;
private ArrayList<WordFolder> new_list;
//constructor - it takes the context and the list of words
WordAdapter(Context context, ArrayList<WordFolder> word){
super(context, 0, word);
//creating a copy of the ArrayList containing all the folders name
original_list = new ArrayList<>(word);
new_list = new ArrayList<>(word);
}
//sets the visibility of the checkBox
public void setCheckBoxVisibility(boolean visible){
this.displayCheckBox = visible;
}
@Override
public View getView(int position, View convertView, ViewGroup parent){
View listItemView = convertView;
if(listItemView == null){
listItemView = LayoutInflater.from(getContext()).inflate(R.layout.folder_view, parent, false);
}
//getting the checkBox view id
CheckBox checkBox = (CheckBox) listItemView.findViewById(R.id.check_box);
checkBox.setVisibility(displayCheckBox ? View.VISIBLE : View.GONE);
//Getting the current word
final WordFolder currentWord = getItem(position);
//making the 3 text view to match our word_folder.xml
TextView date_created = (TextView) listItemView.findViewById(R.id.date_created);
TextView title = (TextView) listItemView.findViewById(R.id.title);
TextView desc = (TextView) listItemView.findViewById(R.id.desc);
//using the setText to get the text and set it in the textView
date_created.setText(currentWord.getDateCreated());
title.setText(currentWord.getTitle());
desc.setText(currentWord.getTitleDesc());
//call automatically when checkbox is changed
checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener(){
@Override
//compound button = the view of the button
//b = the new state of the checkbox
public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
//set the value of the checkbox to the CurrentWord
currentWord.setChecked(b);
}
});
return listItemView;
}
@NonNull
@Override
public Filter getFilter() {
Filter filter = new Filter() {
@Override
protected FilterResults performFiltering(CharSequence charSequence) {
FilterResults filterResults = new FilterResults();
if (charSequence == null || charSequence.length() == 0) {
filterResults.count = original_list.size();
filterResults.values = original_list;
} else {
String searchStr = charSequence.toString().toLowerCase();
ArrayList<WordFolder> results = new ArrayList<>();
for (WordFolder item : original_list) {
if (item.getTitle().contains(searchStr)) {
results.add(item);
}
filterResults.count = results.size();
filterResults.values = results;
}
}
//new list which contains only filtered items
ArrayList<WordFolder> filteredList = new ArrayList<>();
if(charSequence == null || charSequence.length() == 0){
filteredList.addAll(original_list);
}
else{
String filterPattern = charSequence.toString().toLowerCase().trim();
for(WordFolder item : original_list){
if(item.getTitle().toLowerCase().contains(filterPattern)){
filteredList.add(item);
}
}
}
FilterResults results = new FilterResults();
results.values = filteredList;
return results;
}
@Override
protected void publishResults(CharSequence charSequence, FilterResults filterResults) {
new_list.clear();
new_list.addAll((ArrayList) filterResults.values);
notifyDataSetChanged();
}
};
return filter;
}
}
下面是我在MainActivity中创建Search界面的部分代码,供参考。顺便说一句,itemadapter 是我的自定义适配器变量的名称
public boolean onCreateOptionsMenu(Menu menu) {
// initialize menu inflater
MenuInflater inflater = getMenuInflater();
if(whichToolbar == 0){
//inflate menu
inflater.inflate(R.menu.menu_search, menu);
SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
SearchView searchView = (SearchView) menu.findItem(R.id.search).getActionView();
searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
searchView.setIconifiedByDefault(false);
//initialize menu item
MenuItem searchItem = menu.findItem(R.id.search);));
searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
@Override
public boolean onQueryTextSubmit(String s) {
itemAdapter.getFilter().filter(s);
return true;
}
@Override
public boolean onQueryTextChange(String newText) {
itemAdapter.getFilter().filter(newText);
return true;
}
});
searchView.setOnCloseListener(new SearchView.OnCloseListener() {
@Override
public boolean onClose() {
itemAdapter.getFilter().filter("");
return false;
}
});
}
else{
inflater.inflate(R.menu.delete_menu, menu);
}
return true;
}
由于问题尚未得到解答,我更新了此内容。我一直在关注和观看 Youtube 视频(不仅仅是 1 个视频)和 android 开发人员指南,并确保代码“有效”,但我的搜索栏目前没有做任何事情。
在你的适配器中使用这个:
private ArrayList<WordFolder> actualdata = new ArrayList<WordFolder>();
private ArrayList<WordFolder> orignallist= new ArrayList<>();
WordAdapter(Context context, ArrayList<WordFolder> word){
super(context, 0, word);
this.actualdata=new ArrayList<WordFolder>();
this.orignallist=new ArrayList<WordFolder>();
actualdata.addAll(word);//use this to set up your view
orignallist.addAll(word);
}
public void filterData(String query){
query=query.toLowerCase();
actualdata.clear();
if(query.isEmpty()){
actualdata.addAll(orignallist);
}
else {
ArrayList<WordFolder> newlist = new ArrayList<>();
for(WordFolder gd: orignallist) {
if ((gd.getTitle().contains(query)) ) {
newlist.add(gd);
}
}
if(newlist.size()> 0){
actualdata.addAll(newlist);
}
}
notifyDataSetChanged();
}
然后将主类 itemAdapter.getfilter()
更改为 itemAdapter.filterData("your query")
现在应该可以了