Search view onQueryTextChange issue - android

I implemented a search view, that searches from recycler view & update recycler accordingly. When It doesn't matches any item I am showing another layout "No Record" and set recycler view visibility to GONE. Suppose recycler view contains an item with title "judge" and when I type first two letters "ju", it updates recycler view with judge on top.
Now the problem is:
When I type "juz" it should show NoRecord but it is showing blank white screen.
When I type "juzm" it show NoRecord and hide recycler view that is working good but when I backspace search text to "juz" it show NoRecord, again backspace "ju" it show NoRecord and again backspace "j" it shows that item.
Search View method
#Override
public boolean onQueryTextChange(String query) {
newsListAdapter.getFilter().filter(query);
if (newsListAdapter.getItemCount() > 0) {
listRecyclerView.setVisibility(View.VISIBLE);
noRecord.setVisibility(View.GONE);
} else {
listRecyclerView.setVisibility(View.GONE);
noRecord.setVisibility(View.VISIBLE);
}
return false;
}
Filter Method of adapter
#Override
public Filter getFilter() {
return new Filter() {
#Override
protected FilterResults performFiltering(CharSequence charSequence) {
String charString = charSequence.toString();
if (charString.isEmpty()) {
filteredNewsList = newsList;
} else {
List<News.ArticlesBean> filteredList = new ArrayList<>();
for (News.ArticlesBean row : newsList) {
if (row.getTitle().toLowerCase().contains(charString.toLowerCase())) {
filteredList.add(row);
}
}
filteredNewsList = filteredList;
}
FilterResults filterResults = new FilterResults();
filterResults.values = filteredNewsList;
return filterResults;
}
#Override
protected void publishResults(CharSequence charSequence, FilterResults filterResults) {
filteredNewsList = (ArrayList<News.ArticlesBean>) filterResults.values;
notifyDataSetChanged();
}
};
}

This worked
#Override
public boolean onQueryTextChange(String query) {
Filter.FilterListener listener = new Filter.FilterListener() {
#Override
public void onFilterComplete(int count) {
if (newsListAdapter.getItemCount() <=0) {
listRecyclerView.setVisibility(View.GONE);
noRecord.setVisibility(View.VISIBLE);
} else {
listRecyclerView.setVisibility(View.VISIBLE);
noRecord.setVisibility(View.GONE);
}
}
};
newsListAdapter.getFilter().filter(query, listener);
return false;
}

Related

Rotate causing crash due to onQueryTextListener?

I have a search bar for a list of players. When they user types in the search bar it filters the list. Noticed that when the device rotates it crashes. The logic for the search view is carried out in a separate adapter. Is there a way to amend the code so that it does not crash on rotate? The error i get is
Attempt to invoke virtual method 'android.widget.Filter com.example.android.prototype2.views.PlayerListAdapter.getFilter()' on a null object reference
//Assign the search view variable
searchView = findViewById(R.id.search_users);
//Set onQueryListener for when text is entered into the search bar
searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
#Override
public boolean onQueryTextSubmit(String query) {
return false;
}
#Override
//When text is changed filter the results from the playerlist adapter
public boolean onQueryTextChange(String newText) {
adapter.getFilter().filter(newText);
return false;
}
});
The adapter logic is in the playerlistAdapter
private Filter exampleFilter = new Filter() {
#Override
protected FilterResults performFiltering(CharSequence constraint) {
List<UserHelperClass> filteredList = new ArrayList<>();
if (constraint == null || constraint.length() == 0) {
filteredList.addAll(playerListFull);
} else {
//trim -empty spaces removed
String filterPattern = constraint.toString().toLowerCase().trim();
for (UserHelperClass player : playerListFull) {
if (player.getName().toLowerCase().contains(filterPattern)) {
filteredList.add(player);
}
}
}
FilterResults results = new FilterResults();
results.values = filteredList;
return results;
}
#Override
protected void publishResults(CharSequence constraint, FilterResults results) {
playersList.clear();
playersList.addAll((List) results.values);
notifyDataSetChanged();
}
};
I managed to get it working by adding if (adapter != null), before adapter.getFilter().filter(newText);

How to make search view with modal in Android

I want to make a searchView like this image below. The searchView is received data from local database then it filters data like AutoCompleteTextView.
How can i make it?
Here is image:
enter image description here
I use RecyclerView for list display item, and in Adapter I implements a Filterable:
private List<Bank> banksList;
private List<Bank> bankListFiltered;
#Override
public Filter getFilter() {
return new Filter() {
#Override
protected FilterResults performFiltering(CharSequence constraint) {
String charString = constraint.toString();
if (charString.isEmpty()) {
bankListFiltered = banksList;
} else {
List<Bank> filteredList = new ArrayList<>();
for (Bank row : banksList) {
String shortName = row.getShortName().toLowerCase();
if (shortName.contains(charString)) {
filteredList.add(row);
}
}
bankListFiltered = filteredList;
}
FilterResults filterResults = new FilterResults();
filterResults.values = bankListFiltered;
return filterResults;
}
#Override
protected void publishResults(CharSequence constraint, FilterResults results) {
bankListFiltered = (ArrayList<Bank>) results.values;
notifyDataSetChanged();
}
};
}
#Override
public int getItemCount() {
return bankListFiltered.size();
}
public BanksAdapter(List<Bank> banksList) {
this.banksList = banksList;
this.bankListFiltered = banksList;
}
And in my activity, add event for SearchView:
new SearchView(this).setOnQueryTextListener(new SearchView.OnQueryTextListener() {
#Override
public boolean onQueryTextSubmit(String query) {
return true;
}
#Override
public boolean onQueryTextChange(String newText) {
banksAdapter.getFilter().filter(newText);
return true;
}
});

Recyclerview item long press not working using search view?

I have an app which contains recyclerview with searchview on long press item is working fine in case of a normal list, But when I type anything using searchview it comes on the top but when I click on that item I am always getting an item from the normal list at position 0.How do I resolve this issue.
code:-
public Filter getFilter() {
return new Filter() {
#Override
protected FilterResults performFiltering(CharSequence constraint) {
String charString = constraint.toString();
if (charString.isEmpty()) {
mFilterList = models;
} else {
ArrayList<UserListingModel> filteredList = new ArrayList<>();
for (UserListingModel userListingModel : models) {
if (userListingModel.getUseName().toLowerCase().contains(charString.toLowerCase()) || userListingModel.getUseName().toUpperCase().contains(charString.toUpperCase())) {
filteredList.add(userListingModel);
}
}
mFilterList = filteredList;
}
FilterResults filterResults = new FilterResults();
filterResults.values = mFilterList;
return filterResults;
}
#Override
protected void publishResults(CharSequence constraint, FilterResults results) {
mFilterList = (ArrayList<UserListingModel>) results.values;
notifyDataSetChanged();
}
};
}
searchview code:-
mSearchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
#Override
public boolean onQueryTextSubmit(String query) {
Log.e(TAG, "Position");
Users.mAdapter.getFilter().filter(query);
return false;
}
#Override
public boolean onQueryTextChange(String newText) {
Users.mAdapter.getFilter().filter(newText);
return false;
}
});

Weird textbox appeared on search

As on the picture linked below, there is a dark textbox on the center of the screen as user enter the text on searchView. How to get rid of that?
Search related code in my MainActivity.java
listView.setTextFilterEnabled(true);
searchview = (SearchView) findViewById(R.id.searchView);
searchview.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
#Override
public boolean onQueryTextChange(String newText)
{
if (TextUtils.isEmpty(newText)) {
listView.clearTextFilter();
} else {
listView.setFilterText(newText);
}
return false;
}
#Override
public boolean onQueryTextSubmit(String query)
{
return false;
}
});
Search related code on my MyAdapter.java
public Filter getFilter() {
return new Filter() {
#Override
protected FilterResults performFiltering(CharSequence constraint) {
final FilterResults oReturn = new FilterResults();
final ArrayList<DrugItem> results = new ArrayList<DrugItem>();
if (filteredItemsArrayList == null)
filteredItemsArrayList = itemsArrayList;
if (constraint != null) {
if (itemsArrayList != null && filteredItemsArrayList.size() > 0) {
for (final DrugItem g : filteredItemsArrayList) {
if (g.getDrugGenericName().toLowerCase()
.contains(constraint.toString()) || g.getDrugBrandName().toLowerCase()
.contains(constraint.toString()))
results.add(g);
}
}
oReturn.values = results;
}
return oReturn;
}
#SuppressWarnings("unchecked")
#Override
protected void publishResults(CharSequence constraint,
FilterResults results) {
itemsArrayList = (ArrayList<DrugItem>) results.values;
notifyDataSetChanged();
}
};
}
public void notifyDataSetChanged() {
super.notifyDataSetChanged();
}
Search related code in my activity_main.xml
<SearchView
android:iconifiedByDefault="false"
android:layout_below="#+id/toprow"
android:id="#+id/searchView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:queryHint="Enter Drug Name To Filter......." />
How do I remove popup text from listview filter?
i Searched this was answered before as the link above. There is no solution. Use other way to do filtering instead of setTextFilterEnabled(true)

Filter on search in android listView

I am trying to search in list view in android. here is my code
#Override
public Filter getFilter(){
return new Filter(){
#Override
protected FilterResults performFiltering(CharSequence constraint) {
FilterResults result = new FilterResults();
if (constraint != null && constraint.toString().length() > 0) {
constraint = constraint.toString();
List<FoodCell> founded = new ArrayList<FoodCell>();
for(FoodCell item: allItemFood){
if(item.cellText.toString().contains(constraint)){
founded.add(item);
}
}
result.values = founded;
result.count = founded.size();
}else
result.count = 0;
return result;
}
#SuppressWarnings("unchecked")
#Override
protected void publishResults(CharSequence constraint, FilterResults results) {
clear();
if(results.count != 0){
for (FoodCell item : (List<FoodCell>) results.values) {
add(item);
}
}
notifyDataSetChanged();
}
};
}
#Override
public boolean onQueryTextChange(String newText) {
//check if user deleted a character
if(length > newText.length()){
allItemFood = allItemFoodTmp;
}
if (TextUtils.isEmpty(newText)) {
adapter = new CustomListViewAdapter(this, R.layout.listfood,allItemFood);
getListView().setAdapter(adapter);
getListView().setChoiceMode(ListView.CHOICE_MODE_SINGLE);
} else {
adapter.getFilter().filter(newText);
}
length = newText.length();
return true;
}
#Override
public boolean onQueryTextSubmit(String query) {
return false;
}
assume that we have the following items in list , aa,ab,ac
when i enter text "aaz" as search query , it shows empty list which is true but when i delete character "z" from search query,the result remains empty which is false as a result expected.
how can i solve it ?
Not sure which widget you are using to take the search text input, if it is an EditView (considering which I am answering) then you should try adding a TextWatcher to it and then in the onTextChanged() method insert your search login which will execute it every time you add or remove a character inside the EditText.
Do let me know if you are using something else, and I will try to help accordingly. Happy coding. :)

Categories

Resources