I am having difficulties returning the correct views once I use my searchview of my recyclerview.
I inflate the correct view when I use just the recycler on item click, however, once i do a search and the results appear, the wrong view is inflated.
I believe this is due to my use of position
isn't the best way to go about maintaining original recyclerview positions when using the searchview.
I have seen that getAdapterPosition()
is the best way of maintaining the position of an item through a searchview.
If anyone can help me implement the best way of maintaining what gets inflated after the search I would greatly appreciate it! I will also provide an example after the code for the specific scenario.
Also, for clarification, I inflate the views from a RecyclerAdapter, within the adapter's onBindViewHolder method.
public class RecyclerViewAdapter extends RecyclerView.Adapter<RecyclerViewHolder> {
private List<Data> listData = new ArrayList<Data>();
private Context context;
Dialog myDialog;
public RecyclerViewAdapter(List<Data> listData, Context context) {
this.listData = listData;
this.context = context;
}
@Override
public RecyclerViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
LayoutInflater inflater = LayoutInflater.from(parent.getContext());
View itemView = inflater.inflate(R.layout.item,parent,false);
return new RecyclerViewHolder(itemView);
}
@Override
public void onBindViewHolder(RecyclerViewHolder holder, int position) {
holder.itemz.setImageResource(listData.get(position).getitemz());
holder.itemName.setText(listData.get(position).getitemName());
holder.itemPrice.setText(listData.get(position).getitemPrice());
holder.itemVal.setText(listData.get(position).getitemVal());
holder.setItemClickListener(new ItemClickListener() {
@Override
public void onClick(View view, int position) {
final SwipeDismissDialog swipeDismissDialog;
Button btnOK;
if (position == 0) {
myDialog = new Dialog(context);
LayoutInflater inflater = (LayoutInflater) context.getSystemService(context.LAYOUT_INFLATER_SERVICE);
View itemView = inflater.inflate(R.layout.item_cards, null);
swipeDismissDialog = new SwipeDismissDialog.Builder(myDialog.getContext()).setView(itemView).build().show();
TextView words = (TextView)itemView.findViewById(R.id.words);
words.setText("Success1");
btnOK = (Button)itemView.findViewById(R.id.btnOK);
btnOK.setOnClickListener(
new View.OnClickListener() {
@Override
public void onClick(View v) {
swipeDismissDialog.dismiss();
}
}
);
}
else if (position == 1){
myDialog = new Dialog(context);
LayoutInflater inflater = (LayoutInflater) context.getSystemService(context.LAYOUT_INFLATER_SERVICE);
View itemView = inflater.inflate(R.layout.item_cards, null);
swipeDismissDialog = new SwipeDismissDialog.Builder(myDialog.getContext()).setView(itemView).build().show();
TextView words = (TextView)itemView.findViewById(R.id.words);
words.setText("Success2");
btnOK = (Button)itemView.findViewById(R.id.btnOK);
btnOK.setOnClickListener(
new View.OnClickListener() {
@Override
public void onClick(View v) {
swipeDismissDialog.dismiss();
}
}
);
}
}
});
}
@Override
public int getItemCount() {
return listData.size();
}
public void filterList(ArrayList<Data> filteredList){
listData = filteredList;
notifyDataSetChanged();
}
}
What is happening:
- When I load the activity and click on item 1 in the recycler the content
success1
appears as expected. - When I make a search for
item2
, the item returns as expected, however, if I click on the item,success1
appears instead ofsuccess2
.
What might be the best fix for this?
Any help would be appreciated!