在经典版本中,我的适配器运行良好。在我决定在列表末尾添加一个按钮后,应用程序开始崩溃。我在ArrayIndexOutOfBoundsException
中有getItemId()
,但无法弄清楚如何修复它。
找到按钮实现here
方法onCreateViewHolder()
,onBindViewHolder()
和类`ViewHolder不包含任何显着的内容,所以我只是指出它们存在。
适配器:
public class PersonAdapter
extends RealmRecyclerViewAdapter<Person, RecyclerView.ViewHolder> {
private Realm realm;
private PersonFragment personFragment;
private OrderedRealmCollection<Person> adapterData;
public PersonAdapter(PersonFragment fragment, Realm realm, OrderedRealmCollection<Person> collection) {
super(collection, true, true);
this.realm = realm;
this.adapterData = collection;
this.personFragment = fragment;
setHasStableIds(true);
}
@Override
public int getItemViewType(int position) {
return (position == adapterData.size()) ? R.layout.rrv_button_add : R.layout.rrv_item;
}
@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
// see example above
}
@Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
Person person = getData().get(position);
PersonHolder personHolder = (ProductHolder) holder;
if(position == adapterData.size()){
//personHolder.button.setOnClickListener
}
else {
personHolder.name.setText(person.getName());
}
}
@Override
public long getItemId(int index) {
return getItem(index).getId();
}
@Override
public int getItemCount() {
return isDataValid() ? adapterData.size()+1 : 0;
}
private boolean isDataValid() {
return adapterData != null && adapterData.isValid();
}
public class ProductHolder
extends RecyclerView.ViewHolder {
// all that's needed
}
}
我觉得解决方案是表面上的,但我完全没电了。请指出我的错误