Recyclerview item long press not working using search view? - android

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;
}
});

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);

Expandable Recycler view search view not working

This is filter method of my recyclerview filterable code.
#Override
public Filter getFilter() {
return new Filter() {
#Override
protected FilterResults performFiltering(CharSequence charSequence) {
String charString = charSequence.toString();
if (charString.isEmpty()) {
mParentItemList = mOriginalList;
} else {
List<ParentListItem> filteredList = new ArrayList<>();
for (ParentListItem row : mParentItemList) {
// name match condition. this might differ depending on your requirement
// here we are looking for name or phone number match
for (Object childListItem : row.getChildItemList()) {
if (((Help) childListItem).getName().toLowerCase().contains(charString.toLowerCase())) {
filteredList.add(row);
}
}
// if (row.getName().toLowerCase().contains(charString.toLowerCase()) || row.getPhone().contains(charSequence)) {
// filteredList.add(row);
// }
}
mParentItemList = filteredList;
}
FilterResults filterResults = new FilterResults();
filterResults.values = mParentItemList;
return filterResults;
}
#Override
protected void publishResults(CharSequence charSequence, FilterResults filterResults) {
mParentItemList = (ArrayList<ParentListItem>) filterResults.values;
notifyDataSetChanged();
}
};
}
I am getting filtered list in mParentList variable properly but when i am calling notifydatasetchanged() recyclerview not updating and UI is not updating.

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;
}
});

Implement "Search" functionality on RecyclerView

I am trying to search on the recyclerview items. For this I've implemented Filterable. Problem is - instaed of matched items, I am getting the whole list of items. Here is the constructor of the Adapter class :
public RecyclerAdapter (ArrayList<List<ResponseModel>> items) {
this.items = items; // original items
this.tempItems = items; // for searching purpose
}
Here is the overrided method :
#Override
public Filter getFilter() {
return new Filter() {
#Override
protected FilterResults performFiltering(CharSequence constraint) {
String charString = constraint.toString();
if (charString.isEmpty()) {
items = tempItems;
} else {
ArrayList<List<ResponseModel>> filteredList = new ArrayList<>();
for (List<ResponseModel> responseModel : tempItems) {
for (int i =0; i < tempItems.size() ; i++) {
if (responseModel.get(i).getName().toLowerCase().contains(charString)){
filteredList.add(responseModel);
}
}
}
items = filteredList;
}
FilterResults filterResults = new FilterResults();
filterResults.values = items;
return filterResults;
}
#Override
protected void publishResults(CharSequence constraint, FilterResults results) {
items = (ArrayList<List<ResponseModel>>) results.values;
notifyDataSetChanged();
}
};
}
Here is onCreate optionsMenu inside a Fragment class :
#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
inflater.inflate(R.menu.options_menu, menu);
MenuItem item = menu.findItem(R.id.action_search);
SearchView sv = (SearchView) MenuItemCompat.getActionView(menu.findItem(R.id.action_search));
MenuItemCompat.setShowAsAction(item, MenuItemCompat.SHOW_AS_ACTION_COLLAPSE_ACTION_VIEW | MenuItemCompat.SHOW_AS_ACTION_IF_ROOM);
MenuItemCompat.setActionView(item, sv);
sv.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
#Override
public boolean onQueryTextSubmit(String query) {
return false;
}
#Override
public boolean onQueryTextChange(String newText) {
adapter.getFilter().filter(newText);
return true;
}
});
}
Try below in you publishResultsMethod
#Override
protected void publishResults(CharSequence constraint, FilterResults results) {
items.clear();
items.addAll((ArrayList<List<ResponseModel>>) results.values);
notifyDataSetChanged();
}

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)

Categories

Resources