I Have Recyclerview With Searchview How To Send Details Data About Item I Searched About
I have recyclerview with searchview and have details data sended when select item in recyclerview problem is when i search about item and select it the details data not change alwa
Solution 1:
Glad you solved the issue. Below is the adapter code for 2nd solution:
public class RecyclerViewAdapter extends RecyclerView.Adapter<RecyclerViewAdapter.ViewHolder> implements Filterable {
private List<MenuItem> MenuItemsList;
private List<Integer> filteredId;
private RecyclerViewOnItemClick recyclerViewOnItemClick;
public RecyclerViewAdapter(List<MenuItem> nMenuItemsList, RecyclerViewOnItemClick NrecyclerViewOnItemClick) {
this.MenuItemsList = nMenuItemsList;
this.recyclerViewOnItemClick = NrecyclerViewOnItemClick;
filteredId = new ArrayList<>();
setDefaultIds(filteredId);
}
private void setDefaultIds(List<Integer> dataList) {
dataList.clear();
for (int i = 0; i < MenuItemsList.size(); i++) dataList.add(i);
}
@NonNull
@Override
public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.menu_item, parent, false);
ViewHolder holder = new ViewHolder(view, recyclerViewOnItemClick);
return holder;
}
@Override
public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
MenuItem menuItem = MenuItemsList.get(filteredId.get(position));
holder.image.setImageResource(menuItem.getImageResource());
holder.imageName.setText(menuItem.getOilName());
}
@Override
public int getItemCount() {
return filteredId.size();
}
public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
ImageView image;
TextView imageName;
CardView parentLayout;
RecyclerViewOnItemClick recyclerViewOnItemClick;
public ViewHolder(@NonNull View itemView, RecyclerViewOnItemClick recyclerViewOnItemClick) {
super(itemView);
image = itemView.findViewById(R.id.image);
imageName = itemView.findViewById(R.id.image_name);
parentLayout = itemView.findViewById(R.id.RelativeLayout);
this.recyclerViewOnItemClick = recyclerViewOnItemClick;
itemView.setOnClickListener(this);
}
@Override
public void onClick(View view) {
recyclerViewOnItemClick.onItemClick(filteredId.get(getAdapterPosition()));
}
}
@Override
public Filter getFilter() {
return filter;
}
private Filter filter = new Filter() {
//run on background thread
@Override
protected FilterResults performFiltering(CharSequence charSequence) {
List<Integer> filterList = new ArrayList<>();
if (charSequence == null || charSequence.length() == 0) {
// if search text is empty add all original list values to filterlist
setDefaultIds(filterList);
} else {
String filterPattern = charSequence.toString().toLowerCase().trim();
// Perform search on whole original list
MenuItem menuu;
for (int i = 0; i < MenuItemsList.size(); i++) {
menuu = MenuItemsList.get(i);
if (menuu.getOilName().toLowerCase().contains(filterPattern)) {
filterList.add(i);
}
}
}
FilterResults results = new FilterResults();
results.values = filterList;
return results;
}
//run on UI thread
@Override
protected void publishResults(CharSequence charSequence, FilterResults filterResults) {
filteredId.clear();
filteredId.addAll((List<Integer>) filterResults.values);
notifyDataSetChanged();
}
};
public interface RecyclerViewOnItemClick {
void onItemClick(int id);
}
}
Post a Comment for "I Have Recyclerview With Searchview How To Send Details Data About Item I Searched About"