为什么我在应用了一个搜索过滤器到循环视图之后得到了错误的适配器位置?

66bbxpm5  于 2021-06-30  发布在  Java
关注(0)|答案(0)|浏览(122)

我试图从“回收”视图中获取所选项目的位置,但每当我在搜索后点击项目时,它都会显示错误的位置。但是,如果我只是点击它而不搜索,我可以收到正确的位置。我已经在stackoverflow上看过一些关于这个的帖子,但它并不能解决我的问题。如果有人能帮我这个忙,我将不胜感激。

public class SearchStockAdapter extends RecyclerView.Adapter<SearchStockAdapter.ChatListViewHolder> implements Filterable {
    private Context context;

    private ArrayList<Stock_List_Model> filteredStockInformation;
    private ArrayList<Stock_List_Model> stockInformationFull;

      public SearchStockAdapter(Context context, ArrayList<Stock_List_Model> stockInforma) {
            this.context = context;
            this.filteredStockInformation = stockInforma;
            this.stockInformationFull = stockInforma;

        }

        @NonNull
        @Override
        public ChatListViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
            View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.list_item_row, viewGroup, false);
            return new ChatListViewHolder(view);

        }

        //Loads data into views
        @Override
        public void onBindViewHolder(@NonNull ChatListViewHolder viewHolder, final int i) 

            viewHolder.stockTickerPrice.setText("$" + filteredStockInformation.get(i).getPrice());
            viewHolder.stockTickerTv.setText(filteredStockInformation.get(i).getSymbol());
            viewHolder.stockNametv.setText(filteredStockInformation.get(i).getName());

        }

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

        class ChatListViewHolder extends RecyclerView.ViewHolder {

            private TextView stockTickerTv, stockNametv, stockTickerPrice;

            private CircleImageView defaultStockImage;

            private RelativeLayout layoutRow;

            public ChatListViewHolder(@NonNull View itemView) {
                super(itemView);

                stockTickerTv = itemView.findViewById(R.id.stockTickerTv);
                stockNametv = itemView.findViewById(R.id.stockNameTv);
                defaultStockImage = itemView.findViewById(R.id.defaultImageColor);
                stockTickerPrice = itemView.findViewById(R.id.informationTypeImage);
                layoutRow = itemView.findViewById(R.id.layoutRow);

            }

        }

        @Override
        public Filter getFilter() {
            return exampleFilter;
        }

        private Filter exampleFilter = new Filter() {
            @Override
            protected FilterResults performFiltering(CharSequence constraint) {
                ArrayList<Stock_List_Model> filteredList = new ArrayList<>();
                String filterPattern = constraint.toString().toLowerCase().trim();

                if (constraint == null || constraint.length() == 0) {
                    filteredList.addAll(stockInformationFull);
                } else {

                    for (Stock_List_Model item : stockInformationFull) {

                        if (item.getSymbol().toLowerCase().contains(filterPattern)) {
                            filteredList.add(item);

                        }

                        if (item.getName().toLowerCase().contains(filterPattern)) {
                            filteredList.add(item);
                        }

                    }
                }

                FilterResults results = new FilterResults();
                results.values = filteredList;
                return results;
            }
            //updates UI

            @Override
            protected void publishResults(CharSequence constraint, FilterResults results) {
                filteredStockInformation = (ArrayList<Stock_List_Model>) results.values;
                notifyDataSetChanged();

            }
        };

    }

public void initRecycleView() {
        layoutManager = new LinearLayoutManager(getContext(), LinearLayoutManager.VERTICAL, false);
        searchRecycleView = rootView.findViewById(R.id.searchRecyleView);
        searchRecycleView.setLayoutManager(layoutManager);

        searchRecycleView.addOnItemTouchListener(
                new RecyclerItemClickListener(getContext(), searchRecycleView, new RecyclerItemClickListener.OnItemClickListener() {
                    @Override
                    public void onItemClick(View view, int position) {

                        String stockName = stockListArrayList.get(position).getName();
//                        String stockSymbol = stockListArrayList.get(position).getSymbol();

                        Log.i("stockName", stockName);
//

                       // Log.i("tapped", "I TAPPED");
                        // do whatever

//
//
//                        Bundle bundle = new Bundle();
//                        bundle.putString("STOCKSYMBOL", stockSymbol); // Put anything what you want
//
//                        AnalyzeStockFragment analyzeStockFragment = new AnalyzeStockFragment();
//                        analyzeStockFragment.setArguments(bundle);
//
//                        getFragmentManager()
//                                .beginTransaction()
//                                .replace(R.id.fragment_container, analyzeStockFragment)
//                                .commit();

                    }

                    @Override
                    public void onLongItemClick(View view, int position) {
                        // do whatever
                    }
                })
        );

        searchStockAdapter = new SearchStockAdapter(getContext(), stockListArrayList);
        searchRecycleView.setAdapter(searchStockAdapter);

    }

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题