在我的应用程序中,我在recycler视图适配器中实现了可过滤的界面。问题是,当我输入文本但它不在列表中时,应用程序会出现以下异常:
java.lang.IndexOutOfBoundsException: Invalid index 0, size is 0
at java.util.ArrayList.throwIndexOutOfBoundsException(ArrayList.java:255)
at java.util.ArrayList.get(ArrayList.java:308)
at in.droom.adapters.recyclerviewadapter.MultiSelectRecyclerViewAdapter.onBindViewHolder(MultiSelectRecyclerViewAdapter.java:48)
at in.droom.adapters.recyclerviewadapter.MultiSelectRecyclerViewAdapter.onBindViewHolder(MultiSelectRecyclerViewAdapter.java:20)
at android.support.v7.widget.RecyclerView$Adapter.bindViewHolder(RecyclerView.java:5138)
at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:4433)
at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:4326)
at android.support.v7.widget.LinearLayoutManager$LayoutState.next(LinearLayoutManager.java:1955)
at android.support.v7.widget.LinearLayoutManager.layoutChunk(LinearLayoutManager.java:1364)
at android.support.v7.widget.LinearLayoutManager.fill(LinearLayoutManager.java:1327)
at android.support.v7.widget.LinearLayoutManager.onLayoutChildren(LinearLayoutManager.java:556)
at android.support.v7.widget.RecyclerView.dispatchLayout(RecyclerView.java:2713)
at android.support.v7.widget.RecyclerView.onLayout(RecyclerView.java:3011)
at android.view.View.layout(View.java:16636)
at android.view.ViewGroup.layout(ViewGroup.java:5437)
at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1743)
at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1586)
at android.widget.LinearLayout.onLayout(LinearLayout.java:1495)
at android.view.View.layout(View.java:16636)
at android.view.ViewGroup.layout(ViewGroup.java:5437)
at android.widget.FrameLayout.layoutChildren(FrameLayout.java:336)
at android.widget.FrameLayout.onLayout(FrameLayout.java:273)
at android.view.View.layout(View.java:16636)
at android.view.ViewGroup.layout(ViewGroup.java:5437)
at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1743)
at android.widget.LinearLayout.layoutHorizontal(LinearLayout.java:1732)
at android.widget.LinearLayout.onLayout(LinearLayout.java:1497)
at android.view.View.layout(View.java:16636)
at android.view.ViewGroup.layout(ViewGroup.java:5437)
at android.widget.RelativeLayout.onLayout(RelativeLayout.java:1079)
at android.view.View.layout(View.java:16636)
at android.view.ViewGroup.layout(ViewGroup.java:5437)
at android.widget.FrameLayout.layoutChildren(FrameLayout.java:336)
at android.widget.FrameLayout.onLayout(FrameLayout.java:273)
at android.view.View.layout(View.java:16636)
at android.view.ViewGroup.layout(ViewGroup.java:5437)
at android.widget.FrameLayout.layoutChildren(FrameLayout.java:336)
at android.widget.FrameLayout.onLayout(FrameLayout.java:273)
at android.view.View.layout(View.java:16636)
at android.view.ViewGroup.layout(ViewGroup.java:5437)
at android.widget.FrameLayout.layoutChildren(FrameLayout.java:336)
at android.widget.FrameLayout.onLayout(FrameLayout.java:273)
at android.view.View.layout(View.java:16636)
at android.view.ViewGroup.layout(ViewGroup.java:5437)
at android.support.v4.widget.DrawerLayout.onLayout(DrawerLayout.java:907)
at android.view.View.layout(View.java:16636)
at android.view.ViewGroup.layout(ViewGroup.java:5437)
at android.widget.FrameLayout.layoutChildren(FrameLayout.java:336)
at android.widget.FrameLayout.onLayout(FrameLayout.java:273)
at android.view.View.layout(View.java:16636)
at android.view.ViewGroup.layout(ViewGroup.java:5437)
at com.android.internal.widget.ActionBarOverlayLayout.onLayout(ActionBarOverlayLayout.java:493)
at android.view.View.layout(View.java:16636)
at android.view.ViewGroup.layout(ViewGroup.java:5437)
at android.widget.FrameLayout.layoutChildren(FrameLayout.java:336)
at android.widget.FrameLayout.onLayout(FrameLayout.java:273)
at com.android.internal.policy.PhoneWindow$DecorView.onLayout(PhoneWindow.java:2678)
at android.view.View.layout(View.java:16636)
at android.view.ViewGroup.layout(ViewGroup.java:5437)
at android.view
这是我的适配器代码:
public class MultiSelectRecyclerViewAdapter extends SelectableAdapter<MultiSelectRecyclerViewAdapter.ViewHolder> implements Filterable {
private ArrayList<FacetDataModel> mOriginalDataList;
private ArrayList<FacetDataModel> filteredList = new ArrayList<>();
private Context mContext;
private int filterCategoryPosition;
private ViewHolder.ClickListener clickListener;
public MultiSelectRecyclerViewAdapter(Context mContext, int filterCategoryPosition, ArrayList<FacetDataModel> mOriginalDataList, ViewHolder.ClickListener clickListener) {
this.mContext = mContext;
this.filterCategoryPosition = filterCategoryPosition;
this.mOriginalDataList = mOriginalDataList;
filteredList.addAll(mOriginalDataList);
this.clickListener = clickListener;
}
// Create new views
@Override
public MultiSelectRecyclerViewAdapter.ViewHolder onCreateViewHolder(ViewGroup parent,
int viewType) {
View itemLayoutView = LayoutInflater.from(parent.getContext()).inflate(
R.layout.row_facet_textview_checkbox, parent, false);
ViewHolder viewHolder = new ViewHolder(itemLayoutView, clickListener);
return viewHolder;
}
@Override
public void onBindViewHolder(final ViewHolder viewHolder, final int position) {
final FacetDataModel mFacetDataModel = filteredList.get(position);
String facetName = DroomUtil.changeToCustomCamelCase(mFacetDataModel.getFacetDataName()) + " (" + String.valueOf(mFacetDataModel.getFacetDataValue() + ")");
viewHolder.facet_name.setText(facetName);
if (mFacetDataModel.isSelected()) {
viewHolder.facet_tick_mark.setVisibility(View.VISIBLE);
} else {
viewHolder.facet_tick_mark.setVisibility(View.GONE);
}
viewHolder.itemLayoutView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (clickListener != null) {
clickListener.onItemClicked(filterCategoryPosition, position, mFacetDataModel);
}
}
});
}
@Override
public int getItemCount() {
if (filteredList != null && !filteredList.isEmpty())
return filteredList.size();
else
return mOriginalDataList.size();
}
@Override
public Filter getFilter() {
return new Filter() {
@Override
protected FilterResults performFiltering(CharSequence constraint) {
ArrayList<FacetDataModel> filteredResults = new ArrayList<>();
//NEW CODE
FilterResults results = new FilterResults();
if (!TextUtils.isEmpty(constraint)) {
for (FacetDataModel item : mOriginalDataList) {
if (item.getFacetDataName().toLowerCase().contains(constraint)) {
filteredResults.add(item);
}
}
results.values = filteredResults;
results.count = filteredResults.size();
} else {
synchronized (mOriginalDataList) {
results.values = mOriginalDataList;
results.count = mOriginalDataList.size();
}
}
return results;
}
@Override
protected void publishResults(CharSequence charSequence, FilterResults filterResults) {
filteredList = (ArrayList<FacetDataModel>) filterResults.values;
MultiSelectRecyclerViewAdapter.this.notifyDataSetChanged();
}
};
}
protected ArrayList<FacetDataModel> getFilteredResults(String constraint) {
ArrayList<FacetDataModel> results = new ArrayList<>();
for (FacetDataModel item : mOriginalDataList) {
if (item.getFacetDataName().toLowerCase().contains(constraint)) {
results.add(item);
}
}
return results;
}
public static class ViewHolder extends RecyclerView.ViewHolder {
private RobotoLightTextView facet_name;
private View itemLayoutView, divider;
private ImageView facet_tick_mark;
private ClickListener listener;
public ViewHolder(View itemLayoutView, ClickListener listener) {
super(itemLayoutView);
this.itemLayoutView = itemLayoutView;
this.listener = listener;
facet_name = (RobotoLightTextView) itemLayoutView.findViewById(R.id.facet_name);
divider = itemLayoutView.findViewById(R.id.divider);
facet_tick_mark = (ImageView) itemLayoutView.findViewById(R.id.facet_tick_mark);
}
public interface ClickListener {
void onItemClicked(int filterCategoryPosition, int filterDataPosition, FacetDataModel facetDataModel);
}
}
public void refreshData(int filterCategoryPosition, ArrayList<FacetDataModel> mDataList) {
this.filterCategoryPosition = filterCategoryPosition;
this.mOriginalDataList = mDataList;
this.filteredList.clear();
this.filteredList.addAll(mDataList);
notifyDataSetChanged();
}
}
第一行适配器中的48是:
final FacetDataModel mFacetDataModel = filteredList.get(position);
我没有得到我所缺少的东西。请建议。
答案 0 :(得分:1)
我可以看到,错误是java.lang.IndexOutOfBoundsException: Invalid index 0, size is 0
。
在public void onBindViewHolder(final ViewHolder viewHolder, final int position)
上,使用以下代码替换第一行代码,看看它是否适合您。
final FacetDataModel mFacetDataModel = (filteredList != null && !filteredList.isEmpty()) ? filteredList.get(position) : mOriginalDataList.get(position);