gpt4 book ai didi

android - 将 recyclerview 项目限制为仅 10 个而不使用过滤器分页

转载 作者:行者123 更新时间:2023-11-29 14:35:00 25 4
gpt4 key购买 nike

我正在使用包含 283 个项目的 Recycler View 和一个普通的 TextView 来过滤列表。

我只想一直显示10条记录。

我不能使用 getCount(或 GetItemCount),因为当我过滤列表时它会给我 IndexOutOfBoundsExceptions,还有其他方法可以做到这一点吗?

public class RealmAutoCompleteAdapter extends RecyclerView.Adapter<RealmAutoCompleteAdapter.ProductViewHolder>  {
Realm realm;

RealmChangeListener<RealmResults<FieldsItem>> realmChangeListener;
RealmResults<FieldsItem> items;
String searchTerm;

public RealmAutoCompleteAdapter(Realm realm, RealmResults<FieldsItem> items) {
this.realm = realm;
this.realmChangeListener = new RealmChangeListener<RealmResults<FieldsItem>>() {
@Override
public void onChange(RealmResults<FieldsItem> results) {
notifyDataSetChanged();
}
};
this.items = items;
items.addChangeListener(realmChangeListener);
}

@Override
public ProductViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
return new ProductViewHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.autocomplete_row, parent, false));
}

@Override
public void onBindViewHolder(ProductViewHolder holder, int position) {
FieldsItem item = items.get(position);
holder.title.setText(item.getTitle());
}

public void updateInput(String input) {
this.searchTerm = input;
if(items != null && items.isValid()) {
items.removeAllChangeListeners();
}
RealmQuery<FieldsItem> query = realm.where(FieldsItem.class);
if(searchTerm != null && !"".equals(searchTerm)) {
query = query.contains("title", searchTerm, Case.INSENSITIVE);
}
items = query.findAll();
items.addChangeListener(realmChangeListener);
notifyDataSetChanged();
}

@Override
public int getItemCount() {
if(items == null || !items.isValid()) {
return 0;
}
return items.size();
}

static class ProductViewHolder extends RecyclerView.ViewHolder {

TextViewWithFont title;
ProductViewHolder(View itemView) {
super(itemView);
title = (TextViewWithFont)itemView.findViewById(R.id.title);
}

}

最佳答案

你可以这样做:

return Math.min(items.size(), 10);

关于android - 将 recyclerview 项目限制为仅 10 个而不使用过滤器分页,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49540001/

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