Android recyclerview search filter not working

J

jonyiash

Guest
Updating data but not changing. What would be the reason? I tried a lot of methods, but none of them worked. No matter what I write, the result doesn't change. The list is still the same. Where am I doing wrong? I'm sure the adapter has been updated. Because I checked it. The problem must be something else.

searchFilter

MainActivity

@Override
public boolean onCreateOptionsMenu(final Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.main_menu, menu);
SearchManager searchManager =
(SearchManager) getSystemService(Context.SEARCH_SERVICE);
SearchView searchView =
(SearchView) menu.findItem(R.id.app_bar_search).getActionView();
searchView.setQueryHint("Search Customer");
searchView.setSearchableInfo(
searchManager.getSearchableInfo(getComponentName()));

searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
@Override
public boolean onQueryTextSubmit(String query) {
return true;
}

@Override
public boolean onQueryTextChange(String newText) {
mAdapter.getFilter().filter(newText);
return true;
}
});

return true;
}


Adapter

PlaylistAdapter(Context c,List<M3UItem> list) {
mContext = c;
mInflater = LayoutInflater.from(mContext);
this.mItem = list;
this.list = list;
}

@Override
public Filter getFilter() {
return new Filter() {
@Override
protected FilterResults performFiltering(CharSequence charSequence) {
String charString = charSequence.toString();
if (charString.isEmpty()) {
list = mItem;
} else {
List<M3UItem> filteredList = new ArrayList<>();
for (M3UItem row : filteredList) {

// name match condition. this might differ depending on your requirement
// here we are looking for name or phone number match
if (row.getItemName().toLowerCase().contains(charString.toLowerCase()) ) {
filteredList.add(row);

}
}

list = filteredList;
}

FilterResults filterResults = new FilterResults();
filterResults.values = list;
return filterResults;
}

@Override
protected void publishResults(CharSequence charSequence, FilterResults filterResults) {
list = (ArrayList<M3UItem>) filterResults.values;

notifyDataSetChanged();
// refresh the list with filtered data

}
};
}


EDİT:

onBindViewHolder


@Override
public void onBindViewHolder(final ItemHolder holder, final int position) {
final M3UItem item = mItem.get(position);
if (item != null) {
holder.update(item);
}
}


İtemHolder Update

void update(M3UItem item) {


try {
name.setText(item.getItemName());
int color = generator.getRandomColor();
if (item.getItemIcon().isEmpty()) {
textDrawable = TextDrawable.builder()
.buildRoundRect(String.valueOf(item.getItemName().charAt(0)), color, 100);
cImg.setImageDrawable(textDrawable);
} else {
if (Utils.getInstance().isNetworkAvailable(mContext)) {
Glide.with(mContext).load(item.getItemIcon()).into(cImg);
} else {
textDrawable = TextDrawable.builder()
.buildRoundRect(String.valueOf(item.getItemName().charAt(0)), color, 100);
cImg.setImageDrawable(textDrawable);
}
}

} catch (Exception ignored) {
}

}


MainActivity load item:

@Override
protected Boolean doInBackground(String... strings) {
try { //new FileInputStream (new File(name)

is = new FileInputStream(new File(strings[0])); // if u r trying to open file from asstes InputStream is = getassets.open(); InputStream
final M3UPlaylist playlist = parser.parseFile(is);


runOnUiThread(new Runnable() {

@Override
public void run() {

// Stuff that updates the UI
mAdapter.update(playlist.getPlaylistItems());
mAdapter.notifyDataSetChanged();
}
});


return true;
} catch (Exception e) {
Log.d("Google2", "_loadFile: " + e.toString());
return false;
}
}

Continue reading...
 
Top