Solution 1 :
Use this in your adapter:
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();
}
Then change in your mainclass itemAdapter.getfilter()
to itemAdapter.filterData("your query")
Now it should work
Problem :
Hello guys I want to implement a Search interface on my Toolbar to search items in my GridView. However it appears on my Toolbar but when I click the search icon and start typing, nothing pops up. I’m lost on where the issue currently is. I use a custom adapter so I believe there is it bit more work into it.
Here is my custom ArrayAdapter class (WordAdapter). Focus the stuff after the getView() function since I believe that’s the most relevant part of the class. I also added some global variables such as ArrayList<> list and listFull since I believe I needed some copy of my entire item list but honestly I wasn’t sure on how to go about that.
//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;
}
}
Here is the part of the code in my MainActivity that I created the Search interface for reference. Btw itemadapter is the name of my custom adapter variable
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;
}
I updated this since question hasn’t been answered. I’ve been following and watching Youtube videos (not just 1 vid) and the android developer guide about this and made sure the code was “working” and yet my search bar does not do anything at the moment.
Comments
Comment posted by Al-0
It doesn’t work. adding the itemAdapter.getFilter().filter(s) on the TextSubmit but I don’t received any errors either. .
Comment posted by aryanknp
can you try by changing the object you are creating everytime of filter to just a method inside arrayadapter like
Comment posted by aryanknp
which arraylist are you using to set up your adapter?
Comment posted by Al-0
I’m using an ArrayList with contains a WordFolder object (contains 3 Strings, filled with getters and setters). My custom adapter also has 2 ArrayList objects which are copies of the list. “original_list” and “new_list”. I hope that answers your question. Also I’m confused on what I should do with the created method filterData(string q)? and what I should do with the string arg? Thank you
Comment posted by Al-0
Thank you for helping me. My biggest problem was that I cast both actualData and originalData to an entirely new copy of the list. However, when I used notifysetDataChange, nothing happens, so to fix it I made originalData = new ArrayList<> and actualData = word; That way the Variable “actualData” is pointing to the same “word” List. A dumb mistake on my part.