簡體   English   中英

當文本上的Edittext更改時,Android List(RecyclerView Adapter)也在更改

[英]Android List(RecyclerView Adapter) is changing when Edittext On Text change

İ關於RecyclerView過濾器存在問題。 İ在recyclerview中使用文本更改方法上的edittext進行過濾文本查詢,但是在我過濾產品列表時,我正在更改。

注意:所有這些都在fragment和viewpager中的fragment中。

我的問題是:當我寫一些東西時,它正在工作,但同時我的產品列表的元素正變為過濾器的結果。

因此,在示例中,首先MyList有40個項目,FilteredDataList為空。我在edittext中寫了“ a”之后,FilteredDataList有30個項目,但MyList具有相同的30個項目。 但是我還沒有為Mylist設置任何內容

我的數據列表,我從sqlite獲取

 productList = new ArrayList<>();
        productList =  handler.getAllProduct();

我的篩選方法

private List<Product> filter(List<Product> models, String query) {
        query = query.toLowerCase();

        List<Product> filteredModelList = new ArrayList<>();

        filteredModelList.clear();
        for (Product model : models) {
            final String text = model.get_ProductName().toLowerCase();
            if (text.contains(query)) {
                filteredModelList.add(model);
            }
        }
        return filteredModelList;
    }

我的Edittext OnChange方法

searchEdt.addTextChangedListener(new TextWatcher() {

        @Override
        public void afterTextChanged(Editable s) {
        }

        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {
        }

        @Override
        public void onTextChanged(CharSequence s, int start,
                                  int before, int count) {
            if (s.length() != 0) {
                List<Product> filteredModelList = filter( productList, s.toString());

                rcAdapter.animateTo(filteredModelList);
                pager_recycler_view.scrollToPosition(0);
            } else {

                rcAdapter.animateTo(productList);
                pager_recycler_view.scrollToPosition(0);
            }
        }
    });

我的AdapterClass

 public class ProductRecyclerViewAdapter extends RecyclerView.Adapter< ProductRecyclerViewHolder > {

        private List<Product> itemList;
        private Context context;

        public ProductRecyclerViewAdapter(Context context, List<Product> itemList) {
            this.itemList = itemList;
            this.context = context;
        }

        @Override
        public ProductRecyclerViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

            View layoutView = LayoutInflater.from(parent.getContext()).inflate(R.layout.product_card_single_item, null);
            ProductRecyclerViewHolder rcv = new ProductRecyclerViewHolder(layoutView);
            return rcv;
        }

        @Override
        public void onBindViewHolder(ProductRecyclerViewHolder holder, int position) {


            holder.productName.setText(itemList.get(position).get_ProductName());
            holder.productWatCode.setText("%" +itemList.get(position).get_ProductWatCode());
            holder.productPOR.setText("%" +itemList.get(position).get_ProductPOR());
            holder.productRSP.setText("£" +itemList.get(position).get_ProductRSP());
            holder.productDescription.setText(itemList.get(position).get_ProductDescription());
            holder.productSKU.setText(itemList.get(position).get_ProductSKU());
            holder.productPrice.setText("£" + itemList.get(position).get_ProductPrice());
     //       holder.productCountCart.setText("");


            Picasso.with(context)
                    .load( "http://firmabayi.com/images/ilanK/" +itemList.get(position).get_ProductPhoto())
                    .placeholder(R.drawable.add_icon)
                    .error(R.drawable.minus_icon)
                    .into(holder.productPhoto);

           // holder.countryPhoto.setImageResource(itemList.get(position).get_ProductName());

        }

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

        public void animateTo(List<Product> itemList) {
            applyAndAnimateRemovals(itemList);
            applyAndAnimateAdditions(itemList);
            applyAndAnimateMovedItems(itemList);
        }


        private void applyAndAnimateRemovals(List<Product> newModels) {
            for (int i = itemList.size() - 1; i >= 0; i--) {
                final Product model = itemList.get(i);
                if (!newModels.contains(model)) {
                    removeItem(i);
                }
            }
        }

        private void applyAndAnimateAdditions(List<Product> newModels) {
            for (int i = 0, count = newModels.size(); i < count; i++) {
                final Product model = newModels.get(i);
                if (!itemList.contains(model)) {
                    addItem(i, model);
                }
            }
        }

        private void applyAndAnimateMovedItems(List<Product> newModels) {
            for (int toPosition = newModels.size() - 1; toPosition >= 0; toPosition--) {
                final Product model = newModels.get(toPosition);
                final int fromPosition = itemList.indexOf(model);
                if (fromPosition >= 0 && fromPosition != toPosition) {
                    moveItem(fromPosition, toPosition);
                }
            }
        }

        public Product removeItem(int position) {
            final Product model = itemList.remove(position);
            notifyItemRemoved(position);
            return model;
        }

        public void addItem(int position, Product model) {
            itemList.add(position, model);
            notifyItemInserted(position);
        }

        public void moveItem(int fromPosition, int toPosition) {
            final Product model = itemList.remove(fromPosition);
            itemList.add(toPosition, model);
            notifyItemMoved(fromPosition, toPosition);
        }

    }

我解決了我的問題,它僅與適配器類的第一行有關:

在適配器類中

代替

this.itemList = itemList;

用那個

this.itemList = new ArrayList<>(itemList);

它與您的productList有關。 當您像這樣創建對象時;

Class a = b();

您正在克隆對象。 在深處,它們是同一對象。 因此,當您過濾名為a的對象時,b會因此受到影響。

簡而言之,不要這樣做。 而不是克隆對象,您應該將每個項目從b逐個添加到a。 像這樣;

productList = new ArrayList<>();
    for( int i = 0 ; i <arrayFromSource.size() ; i++ )
    {
        productList.add(arrayFromSource.get(i));
    }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM