Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get original item position after filter in Android RecyclerView adapter?

Tags:

I have implement filter in android recyclerview adapter. But problem is that when I filtered item than position of item changed. I need the original position of the item after filter? How can i get it?

Here is my code:

public class SearchAdapter extends SelectableAdapter<SearchAdapter.ViewHolder> implements Filterable {

    static ArrayList<SingleHadishDisplayModel> singleHadishDisplayModels;
    ArrayList<SingleHadishDisplayModel> singleHadishDisplayModelsFilter;
    public ViewHolder.ClickListener clickListener;

    Context context;

    public SearchAdapter(ArrayList<SingleHadishDisplayModel> singleHadishDisplayModels) {
        this.singleHadishDisplayModelsFilter = singleHadishDisplayModels;
        if (singleHadishDisplayModels != null) {
            this.singleHadishDisplayModels = new ArrayList<>(singleHadishDisplayModels);
        } else {
            this.singleHadishDisplayModels = null;
        }
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        context = parent.getContext();
        View v = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.search_adapter, parent, false);

        ViewHolder vh = new ViewHolder(v, clickListener);
        return vh;
    }

    @Override
    public void onBindViewHolder(final ViewHolder holder, final int position) {
        holder.topic.setText(singleHadishDisplayModelsFilter.get(position).topic);
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                // Contants.clickPos=position;
                /*long id =  SearchAdapter.getItemId(position);
                Toast.makeText(context,Long.toString(id), Toast.LENGTH_SHORT).show();*/

                Intent intent = new Intent(context, HadishDetails.class);
                context.startActivity(intent);
            }
        });
    }

    @Override
    public int getItemCount() {
        return singleHadishDisplayModelsFilter.size();
    }

    public long getItemId(int position) {
        int itemID;
        // orig will be null only if we haven't filtered yet:
        if (singleHadishDisplayModels == null) {
            itemID = position;
        } else {
            itemID = singleHadishDisplayModels.indexOf(singleHadishDisplayModels.get(position));
        }
        return itemID;
    }


    @Override
    public Filter getFilter() {
        Filter filter = new Filter() {
            @Override
            protected FilterResults performFiltering(CharSequence constraint) {
                FilterResults filterResults = new FilterResults();

                if (constraint.length() == 0) {
                    filterResults.values = SearchAdapter.this.singleHadishDisplayModels;
                    return filterResults;
                }

                String searchString = constraint.toString().toLowerCase();

                List<SingleHadishDisplayModel> filteredList = new ArrayList<>();
                filterResults.values = filteredList;

                for (SingleHadishDisplayModel tableSelectionDTO : SearchAdapter.this.singleHadishDisplayModels) {
                    if (tableSelectionDTO.topic.toLowerCase().contains(searchString)) {
                        filteredList.add(tableSelectionDTO);
                    }
                }

                return filterResults;
            }

            @Override
            protected void publishResults(CharSequence constraint, FilterResults results) {
                SearchAdapter.this.singleHadishDisplayModelsFilter = (ArrayList<SingleHadishDisplayModel>) results.values;
                SearchAdapter.this.notifyDataSetChanged();
            }
        };

        return filter;
    }

    public static class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener, View.OnLongClickListener {
        TextView topic;

        private ClickListener listener;

        public ViewHolder(View itemView, ClickListener clickListener) {
            super(itemView);

            this.listener = clickListener;
            topic = (TextView) itemView.findViewById(R.id.topic_search);

            itemView.setOnClickListener(this);
            itemView.setOnLongClickListener(this);
        }

        @Override
        public void onClick(View v) {
            if (listener != null) {
                listener.onItemClicked(getAdapterPosition());
            }
        }

        @Override
        public boolean onLongClick(View view) {
            if (listener != null) {
                return listener.onItemLongClicked(getAdapterPosition());
            }
            return false;
        }

        public interface ClickListener {
            public void onItemClicked(int position);

            public boolean onItemLongClicked(int position);
        }

    }
}

My activity:

public class SearchActivity extends AppCompatActivity implements SearchAdapter.ViewHolder.ClickListener {
    private RecyclerView mRecyclerView;
    public SearchAdapter mAdapter;
    private RecyclerView.LayoutManager mLayoutManager;
    ArrayList<SingleHadishDisplayModel> singleHadishDisplayModels;
    SingleHadishDisplayModel singleHadishDisplayModel;
    int positionClick;
    public static final String POST_ID = "postID";

    FloatingSearchView floatingSearchView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_search);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        //floatingSearchView= (FloatingSearchView) findViewById(R.id.floating_search_view);
        singleHadishDisplayModels = new ArrayList<SingleHadishDisplayModel>();
        singleHadishDisplayModels = Contants.singleHadishDisplayModelsSaved;

        mRecyclerView = (RecyclerView) findViewById(R.id.search_reycler);
        mRecyclerView.setHasFixedSize(true);
        mLayoutManager = new LinearLayoutManager(this);
        mRecyclerView.setLayoutManager(mLayoutManager);

        mAdapter = new SearchAdapter(singleHadishDisplayModels);
        mRecyclerView.setAdapter(mAdapter);


        /*  floatingSearchView.setOnQueryChangeListener(new FloatingSearchView.OnQueryChangeListener() {
        @Override
        public void onSearchTextChanged(String oldQuery, final String newQuery) {
        mAdapter.clearSelection();
        mAdapter.getFilter().filter(newQuery);
    }
});*/

        EditText et = (EditText) findViewById(R.id.search_et);
        et.addTextChangedListener(new TextWatcher() {

            public void onTextChanged(CharSequence cs, int arg1, int arg2,
                                      int arg3) {
                // When user changed the Text
                SearchActivity.this.mAdapter.getFilter().filter(cs);
                Toast.makeText(SearchActivity.this, Integer.toString(arg1), Toast.LENGTH_SHORT).show();
            }

            public void beforeTextChanged(CharSequence arg0, int arg1,
                                          int arg2, int arg3) {
                // TODO Auto-generated method stub

            }

            public void afterTextChange(Editable arg0) {
                // TODO Auto-generated method stub

            }

            public void afterTextChanged(Editable s) {
                // TODO Auto-generated method stub

            }
        });


    }


    @Override
    public void onItemClicked(int position) {
        positionClick = position;


        // Intent intent =new Intent(SearchActivity.this,HadishDetails.class);
        //startActivity(intent);

        //  Contants.clickPos=position;

    }

    @Override
    public boolean onItemLongClicked(int position) {
        return false;
    }
}
like image 553
arif Avatar asked Sep 20 '16 16:09

arif


1 Answers

Use This Codes :

int pos = list.indexOf(arraylist1.get(getAdapterPosition()));

Here list is the original list and arrayList is filtered items list.

like image 122
Murtaza Agaz Avatar answered Sep 24 '22 14:09

Murtaza Agaz