0

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:

  1. When I load the activity and click on item 1 in the recycler the content success1 appears as expected.
  2. When I make a search for item2, the item returns as expected, however, if I click on the item, success1 appears instead of success2.

What might be the best fix for this?

Any help would be appreciated!

Devil10
  • 1,853
  • 1
  • 18
  • 22
Modest
  • 53
  • 1
  • 7

1 Answers1

0

I suppose that when you make your search, you set a new list of data in your adapter, and this new list contains the item2 at position 0.

So when you click on the item, you are clicking on the item at position 0, and in your code you have something like that:

So you should use a condition on the item and not the position. Here is the best way of doing that:

public Data getDataAtPosition(int position) {
    if (listData!= null && listData.size() > position)
        return listData.get(position);
    else
        return null;
}

And then:

@Override
public void onBindViewHolder(RecyclerViewHolder holder, int position) {
    // ...
    holder.setItemClickListener(new ItemClickListener() {
        @Override
        public void onClick(View view, int position) {
            Data data = getDataAtPosition(position);
            if (data != null && data.isItem1())
                // Success1
            else if (data != null && data.isItem2())
               // Success2
        }
}

PS: you create a new listener everytimes that your holder changes, maybe you could check this explanation about how to create a ClickListener on a RecyclerView: https://stackoverflow.com/a/26196831/9434800

Mickael B.
  • 4,755
  • 4
  • 24
  • 48
  • Thanks for a response! What exactly do you mean with "data.isItem1()" ? I understand the data != null part but I am not sure what you are referring to in the latter – Modest Apr 28 '18 at 02:40
  • I guess you don't really want to check the condition on the position in the list but on the item itself (if this is the item1 or item2). So `data.isItem1()` could be replaced by an id on your items like `data.getId() == 1` – Mickael B. Apr 28 '18 at 02:41
  • I don't currently have Id's on the items within the recyclerview so trying to get the Id's requires me to assign them first. Would I do this in the view holder? – Modest Apr 28 '18 at 03:13
  • You do it the same way you did if for the Name, Price and Val that you are using just before your condition. – Mickael B. Apr 28 '18 at 03:32