我想在工具栏中实现searchView
小部件来过滤recyclerview
。但是我收到了一条错误消息。
这是我的代码:
PasalAdapter.java
public class PasalAdapter extends RecyclerView.Adapter<ItemViewHolder> {
private List<PasalItem> mPasalItem;
public PasalAdapter(List<PasalItem> PasalItem) {
mPasalItem = new ArrayList<>(PasalItem);
}
@Override
public void onBindViewHolder(ItemViewHolder itemViewHolder, int i) {
final PasalItem getPasal = mPasalItem.get(i);
itemViewHolder.bind(getPasal);
}
@Override
public ItemViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.list_row, viewGroup, false);
return new ItemViewHolder(view);
}
@Override
public int getItemCount() {
return mPasalItem.size();
}
/** Filter Logic**/
public void animateTo(List<PasalItem> models) {
applyAndAnimateRemovals(models);
applyAndAnimateAdditions(models);
applyAndAnimateMovedItems(models);
}
private void applyAndAnimateRemovals(List<PasalItem> newModels) {
for (int i = mPasalItem.size() - 1; i >= 0; i--) {
final PasalItem model = mPasalItem.get(i);
if (!newModels.contains(model)) {
removeItem(i);
}
}
}
private void applyAndAnimateAdditions(List<PasalItem> newModels) {
for (int i = 0, count = newModels.size(); i < count; i++) {
final PasalItem model = newModels.get(i);
if (!mPasalItem.contains(model)) {
addItem(i, model);
}
}
}
private void applyAndAnimateMovedItems(List<PasalItem> newModels) {
for (int toPosition = newModels.size() - 1; toPosition >= 0; toPosition--) {
final PasalItem model = newModels.get(toPosition);
final int fromPosition = mPasalItem.indexOf(model);
if (fromPosition >= 0 && fromPosition != toPosition) {
moveItem(fromPosition, toPosition);
}
}
}
public PasalItem removeItem(int position) {
final PasalItem model = mPasalItem.remove(position);
notifyItemRemoved(position);
return model;
}
public void addItem(int position, PasalItem model) {
mPasalItem.add(position, model);
notifyItemInserted(position);
}
public void moveItem(int fromPosition, int toPosition) {
final PasalItem model = mPasalItem.remove(fromPosition);
mPasalItem.add(toPosition, model);
notifyItemMoved(fromPosition, toPosition);
}
}
ItemViewHolder.java
public class ItemViewHolder extends RecyclerView.ViewHolder {
private TextView pasal_TextView;
private TextView bab_TextView;
public ItemViewHolder(View itemView) {
super(itemView);
pasal_TextView = (TextView) itemView.findViewById(R.id.tx_pasal);
bab_TextView = (TextView) itemView.findViewById(R.id.tx_bab);
}
public void bind(PasalItem pasalItem) {
pasal_TextView.setText(pasalItem.getPasal());
bab_TextView.setText(pasalItem.getBab());
}
}
PasalItem.Java
public class PasalItem {
private String pasal;
private String bab;
public String getPasal() {
return pasal;
}
public void setPasal(String pasal) {
this.pasal = pasal;
}
public String getBab() {
return bab;
}
public void setBab(String bab) {
this.bab = bab;
}
@Override
public String toString() {
return this.pasal;
}
}
修改
12-23 07:55:31.062 29266-29266/id.rsspdev.uud E/AndroidRuntime: FATAL EXCEPTION: main
java.lang.NullPointerException
at id.rsspdev.uud.adapter.ItemViewHolder.bind(ItemViewHolder.java:26)
at id.rsspdev.uud.adapter.PasalAdapter.onBindViewHolder(PasalAdapter.java:28)
at id.rsspdev.uud.adapter.PasalAdapter.onBindViewHolder(PasalAdapter.java:17)
at android.support.v7.widget.RecyclerView$Adapter.onBindViewHolder(RecyclerView.java:5217)
at android.support.v7.widget.RecyclerView$Adapter.bindViewHolder(RecyclerView.java:5250)
at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:4487)
at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:4363)
at android.support.v7.widget.LinearLayoutManager$LayoutState.next(LinearLayoutManager.java:1961)
at android.support.v7.widget.LinearLayoutManager.layoutChunk(LinearLayoutManager.java:1370)
at android.support.v7.widget.LinearLayoutManager.fill(LinearLayoutManager.java:1333)
at android.support.v7.widget.LinearLayoutManager.onLayoutChildren(LinearLayoutManager.java:562)
at android.support.v7.widget.RecyclerView.dispatchLayout(RecyclerView.java:2900)
at android.support.v7.widget.RecyclerView.onLayout(RecyclerView.java:3071)
at android.view.View.layout(View.java:14099)
at android.view.ViewGroup.layout(ViewGroup.java:4464)
at android.support.design.widget.CoordinatorLayout.layoutChild(CoordinatorLayout.java:1037)
at android.support.design.widget.CoordinatorLayout.onLayoutChild(CoordinatorLayout.java:747)
at android.support.design.widget.CoordinatorLayout.onLayout(CoordinatorLayout.java:761)
at android.view.View.layout(View.java:14099)
at android.view.ViewGroup.layout(ViewGroup.java:4464)
at android.widget.FrameLayout.onLayout(FrameLayout.java:448)
at android.view.View.layout(View.java:14099)
at android.view.ViewGroup.layout(ViewGroup.java:4464)
at android.support.design.widget.CoordinatorLayout.layoutChild(CoordinatorLayout.java:1037)
at android.support.design.widget.CoordinatorLayout.onLayoutChild(CoordinatorLayout.java:747)
at android.support.design.widget.ViewOffsetBehavior.onLayoutChild(ViewOffsetBehavior.java:42)
at android.support.design.widget.AppBarLayout$ScrollingViewBehavior.onLayoutChild(AppBarLayout.java:1156)
at android.support.design.widget.CoordinatorLayout.onLayout(CoordinatorLayout.java:760)
at android.view.View.layout(View.java:14099)
at android.view.ViewGroup.layout(ViewGroup.java:4464)
at android.support.v4.widget.DrawerLayout.onLayout(DrawerLayout.java:1043)
at android.view.View.layout(View.java:14099)
at android.view.ViewGroup.layout(ViewGroup.java:4464)
at android.widget.FrameLayout.onLayout(FrameLayout.java:448)
at android.view.View.layout(View.java:14099)
at android.view.ViewGroup.layout(ViewGroup.java:4464)
at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1670)
at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1528)
at android.widget.LinearLayout.onLayout(LinearLayout.java:1441)
at android.view.View.layout(View.java:14099)
at android.view.ViewGroup.layout(ViewGroup.java:4464)
at android.widget.FrameLayout.onLayout(FrameLayout.java:448)
at android.view.View.layout(View.java:14099)
at android.view.ViewGroup.layout(ViewGroup.java:4464)
at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1670)
at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1528)
at android.widget.LinearLayout.onLayout(LinearLayout.java:1441)
at android.view.View.layout(View.java:14099)
at android.view.ViewGroup.layout(ViewGroup.java:4464)
at android.widget.FrameLayout.onLayout(FrameLayout.java:448)
at android.view.View.layout(View.java:14099)
at android.view.ViewGroup.layout(ViewGroup.java:4464)
at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:2183)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1947)
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1139)
at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:4879)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:776)
at android.view.
谁能帮助我吗?
答案 0 :(得分:2)
您的RecyclerView中发生的事情与此问题类似:
Getting position of View in onCreateViewHolder
其中recyclerview不维护静态位置索引。如果您不想在recyclerview执行此操作时丢失物品,则无法使用给定的位置。
尝试:
>>> import numbers
# /usr/lib/python2.7/numbers.pyc matches /usr/lib/python2.7/numbers.py
import numbers # precompiled from /usr/lib/python2.7/numbers.pyc
# /usr/lib/python2.7/__future__.pyc matches /usr/lib/python2.7/__future__.py
import __future__ # precompiled from /usr/lib/python2.7/__future__.pyc