我正在使用带有卡片视图和recyclerview的列表。我为列表添加了适配器并添加了getItemCount方法。我想为我的列表添加搜索功能。所以我也添加了一个过滤器。但是当我在搜索视图中输入时,我在返回ItemList.size()时遇到异常;这一行..我错过了什么?
适配器
public class ItemAdapter extends RecyclerView.Adapter<ListViewHolder> implements Filterable{
public ItemsFilter itemFilter;
ArrayList<Item> mStringFilterList;
private ArrayList<Item> ItemList;
private Context context;
private MyItemClickListener mItemClickListener;
private MyItemLongClickListener mItemLongClickListener;
public ItemAdapter(Context context,ArrayList<Item> ItemList) {
this.context= context;
this.ItemList = ItemList;
}
@Override
public ListViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
View v = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.card_layout, null);
ListViewHolder mh = new ListViewHolder(v);
return mh;
}
@Override
public void onBindViewHolder(final ListViewHolder ListViewHolder, int i) {
Item Item = ItemList.get(i);
ListViewHolder.textName.setText(Item.getItem_name());
ListViewHolder.textDesc.setText(Item.getItem_desc());
ListViewHolder.textQty.setText(Item.getItem_qty());
ListViewHolder.textName.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
}
});
}
@Override
public int getItemCount() {
return ItemList.size();
}
public class ItemsFilter extends Filter {
@Override
protected FilterResults performFiltering(CharSequence constraint) {
FilterResults results = new FilterResults();
if (constraint != null && constraint.length() > 0) {
ArrayList<Item> filterList = new ArrayList<Item>();
for (int i = 0; i < mStringFilterList.size(); i++) {
if ((mStringFilterList.get(i).getItem_name())
.contains(constraint.toString())) {
Item item = new Item(mStringFilterList.get(i)
.getItem_name(), mStringFilterList.get(i)
.getItem_desc(), mStringFilterList.get(i)
.getItem_qty());
filterList.add(item);
}
}
results.count = filterList.size();
results.values = filterList;
} else {
results.count = mStringFilterList.size();
results.values = mStringFilterList;
}
return results;
}
@Override
public void publishResults(CharSequence constraint,
Filter.FilterResults results) {
ItemList = (ArrayList<Item>) results.values;
notifyDataSetChanged();
}
}
@Override
public Filter getFilter() {
// return a filter that filters data based on a constraint
if (itemFilter == null) {
itemFilter = new ItemsFilter();
}
return itemFilter;
}
}
片段
public class ViewStock extends Fragment implements SearchView.OnQueryTextListener {
final MainActivity act = (MainActivity) this.getActivity();
public ViewStock() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View rootview = inflater.inflate(R.layout.fragment_view_stock, container, false);
setHasOptionsMenu(true);
act.array_data = new ArrayList<Item>();
// act.listview.setTextFilterEnabled(true);
// act. = new ArrayList<Item>();
// Context mCtx = getActivity().getApplicationContext();
RecyclerView mRecyclerView = (RecyclerView)rootview.findViewById(R.id.my_recycler_view);
LinearLayoutManager llm = new LinearLayoutManager(getActivity());
llm.setOrientation(LinearLayoutManager.VERTICAL);
mRecyclerView.setLayoutManager(llm);
// ItemAdapter adapter = new ItemAdapter(getActivity(), R.layout.list_item, act.arrayList);
// listview.setAdapter(adapter);
act.db = new DBHandler(getActivity());
ArrayList<Item> item_array_from_db = act.db.Get_items();
for (int i = 0; i < item_array_from_db.size(); i++) {
int idno = item_array_from_db.get(i).getID();
String name = item_array_from_db.get(i).getItem_name();
String desc = item_array_from_db.get(i).getItem_desc();
String qty = item_array_from_db.get(i).getItem_qty();
Item cnt = new Item();
cnt.setID(idno);
cnt.setItem_name(name);
cnt.setItem_desc(desc);
cnt.setItem_qty(qty);
act.array_data.add(cnt);
}
act.db.close();
act.adapter=new ItemAdapter(getActivity(),act.array_data);
mRecyclerView.setAdapter(act.adapter);
return rootview;
}
@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
inflater.inflate(R.menu.menu_main, menu);
final MenuItem item = menu.findItem(R.id.menu_search);
final SearchView searchView = (SearchView) MenuItemCompat.getActionView(item);
searchView.setOnQueryTextListener(this);
}
@Override
public boolean onQueryTextChange(String newText) {
if (TextUtils.isEmpty(newText)) {
act.adapter.getFilter().filter("");
} else {
act.adapter.getFilter().filter(newText.toString());
}
return true;
}
@Override
public boolean onQueryTextSubmit(String query) {
return false;
}
}
类
public class Item {
public int id;
public String item_name;
public String item_desc;
public String item_qty;
public Item(){}
public Item(int id, String item_name, String item_desc, String item_qty) {
super();
this.item_name = item_name;
this.item_desc = item_desc;
this.item_qty = item_qty;
}
public Item(String item_name, String item_desc, String item_qty){
this.item_name = item_name;
this.item_desc=item_desc;
this.item_qty = item_qty;
}
public int getID(){
return id;
}
public void setID(int id){
this.id= id;
}
public String getItem_name(){
return item_name;
}
public void setItem_name(String item_name)
{
this.item_name=item_name;
}
public String getItem_desc()
{
return item_desc;
}
public void setItem_desc(String item_desc)
{
this.item_desc=item_desc;
}
public String getItem_qty()
{
return item_qty;
}
public void setItem_qty(String item_qty) {
this.item_qty = item_qty;
}
}
答案 0 :(得分:5)
ItemList
在某处设置为null
。防止这种情况的一种方法是,如果列表为空,则将getItemCount
方法更改为0:
int getItemCount(){
return ItemList == null ? 0 : ItemList.size();
}
答案 1 :(得分:2)
只需初始化您的ItemList,
private ArrayList<Item> ItemList = new ArrayList<Item>();
或者,在getItemCount()
中添加一个检查 @Override
public int getItemCount() {
If (ItemList == null)
return 0;
else
return ItemList.size();
}
<强> EDITED 强>
@Override
public int getItemCount() {
if (ItemList == null)
return 0;
else
return ItemList.size();
}