gpt4 book ai didi

java - Android List(RecyclerView Adapter)在 Edittext On Text 发生变化时发生变化

转载 作者:搜寻专家 更新时间:2023-11-01 09:50:34 24 4
gpt4 key购买 nike

© RecyclerView 过滤器有问题。我正在使用 edittext on text change 方法在 recyclerview 中过滤文本查询,但是当我过滤时,我的产品列表正在改变。

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

我的问题是:当我写一些东西时它正在工作,但同时我的产品列表的元素正在更改为过滤器的结果。

所以在一开始的例子中 我的列表有 40 个项目 筛选数据列表为空在我在 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);
}
}
});

我的适配器类

 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);

关于java - Android List(RecyclerView Adapter)在 Edittext On Text 发生变化时发生变化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36415560/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com