单击时使用RecyclerView搜索视图以显示旧数据

时间:2019-03-26 11:02:49

标签: android

我正在使用RecyclerView进行搜索视图,每件事都可以正常工作,就像在RecyclerView上显示的数据一样,搜索数据也可以过滤并显示结果,但是当我触摸数据时,它给出的旧位置意味着过滤器位置不会显示在吐司上,如果我们搜索键入z搜索视图,它显示所有数据以z开头,但是当我单击以z开头的数据时,它在烤面包数据上显示我,这意味着数据以a开头 我在列表中遇到了同样的问题,我找到了ListView对象的解决方案 list.getItemAtPosition可以解决与RecyclerView中相同的问题

这是界面

public interface RecyclerViewClickListener {
public void recyclerViewListClicked(View v, int position);}

此代码出现问题

    recycleitemclicklistener = new RecyclerViewClickListener() {
        @Override
        public void recyclerViewListClicked(View v, int position) {

            if (positionvalue.equals("CommonCustomer")) {
                CommonSetterANDGetter  getclick= commonSetterANDGetters.get(position);
                Toast.makeText(maincontext, "denm"+ getclick.getValueNameCustomer()+getclick.getIdCustomer(), Toast.LENGTH_SHORT).show();
            }

        }
    };

这是适配器

public class SearchAdapter extends 
 RecyclerView.Adapter<SearchAdapter.ViewHolder> implements Filterable {
    private ArrayList<CommonSetterANDGetter> mArrayList;
    private ArrayList<CommonSetterANDGetter> mFilteredList;
    private String positionvalue = null;
    RecyclerViewClickListener itemclicklistener;
    Context maincontext;

    public SearchAdapter(Context maincontext, ArrayList<CommonSetterANDGetter> arrayList, String positionvalue, RecyclerViewClickListener recycleitemclicklistener) {
        this.maincontext = maincontext;
        this.mArrayList = arrayList;
        this.mFilteredList = arrayList;
        this.itemclicklistener = recycleitemclicklistener;
        this.positionvalue=positionvalue;
    }

    @Override
    public SearchAdapter.ViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
        View view = null;
        if(positionvalue.equals( "StockSummary_List")) {
            view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.stocksummary_petty, viewGroup, false);
        }else if(positionvalue.equals( "CommonCustomer")){
            view= LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.customersearch_petty,viewGroup,false);
        }
         return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(SearchAdapter.ViewHolder viewHolder, int i) {
         if(positionvalue.equals( "StockSummary_List")) {
             viewHolder.itemCodeStock.setText(mFilteredList.get(i).getItemCodeStock());
             viewHolder.itemNameStock.setText(mFilteredList.get(i).getItemNameStock());
             viewHolder.issuedStock.setText("Iss :" + mFilteredList.get(i).getIssuedStock() + mFilteredList.get(i).getUmoStock());
             viewHolder.recievedStock.setText("Rec :" + mFilteredList.get(i).getRecievedStock() + mFilteredList.get(i).getUmoStock());
             viewHolder.closingStock.setText("Clo :" + mFilteredList.get(i).getClosingStock() + mFilteredList.get(i).getUmoStock());
             //    viewHolder.umoStock.setText(mFilteredList.get(i).getUmoStock());
         }else if(positionvalue.equals( "CommonCustomer")){
             viewHolder.custName_commonCustdetail.setText(mFilteredList.get(i).getValueNameCustomer());
         }
    }

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

    @Override
    public Filter getFilter() {

        return new Filter() {
            @Override
            protected FilterResults performFiltering(CharSequence charSequence) {
                String charString = charSequence.toString();
                if (charString.isEmpty()) {
                    mFilteredList = mArrayList;
                } else {
                    ArrayList<CommonSetterANDGetter> filteredList = new ArrayList<>();
                    for (CommonSetterANDGetter commonsetget : mArrayList) {
                        if (positionvalue.equals("StockSummary_List")) {
                            if (commonsetget.getItemCodeStock().toLowerCase().contains(charString) || commonsetget.getItemNameStock().toLowerCase().contains(charString)) {
                                filteredList.add(commonsetget);
                            }
                        }else if(positionvalue.equals( "CommonCustomer")){
                            if (commonsetget.getValueNameCustomer().toLowerCase().contains(charString)) {
                                filteredList.add(commonsetget);
                            }
                        }
                    }
                    mFilteredList = filteredList;
                }
                FilterResults filterResults = new FilterResults();
                filterResults.values = mFilteredList;
                return filterResults;
            }
            @Override
            protected void publishResults(CharSequence charSequence, FilterResults filterResults) {
                mFilteredList = (ArrayList<CommonSetterANDGetter>) filterResults.values;
                notifyDataSetChanged();
            }
        };
    }

    public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener  {
         TextView itemCodeStock, itemNameStock, issuedStock, recievedStock, closingStock, umoStock;// stock Sum
         TextView custName_commonCustdetail;// common detail cust
        public ViewHolder(View view) {
            super(view);
            if(positionvalue.equals( "StockSummary_List")) {
                itemCodeStock = (TextView) view.findViewById(R.id.itemcode_stocksum);
                itemNameStock = (TextView) view.findViewById(R.id.itemname_stocksum);
                issuedStock = (TextView) view.findViewById(R.id.issue_stocksum);
                recievedStock = (TextView) view.findViewById(R.id.recieve_stocksum);
                closingStock = (TextView) view.findViewById(R.id.closing_stocksum);
            }else if(positionvalue.equals( "CommonCustomer")){
                    custName_commonCustdetail= (TextView) view.findViewById(R.id.commonname_customerdetail);
                custName_commonCustdetail.setOnClickListener(this);
            }
        }
        @Override
        public void onClick(View v) {
            itemclicklistener.recyclerViewListClicked(v, this.getAdapterPosition());
        }
    }
}

0 个答案:

没有答案