gpt4 book ai didi

java - 方法 getItemCount 中出现空指针异常

转载 作者:行者123 更新时间:2023-12-01 06:43:06 25 4
gpt4 key购买 nike

我正在使用带有卡片 View 和回收器 View 的列表。我已经为列表添加了适配器并添加了 getItemCount 方法。我想为我的列表添加搜索功能。所以我也添加了一个过滤器。但是当我在搜索 View 中输入时,我在 return 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;
}
}

fragment

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;
}
}

最佳答案

ItemList 在某处设置为 null。防止这种情况的一种方法是将 getItemCount 方法更改为在列表为 null 时返回 0:

int getItemCount(){
return ItemList == null ? 0 : ItemList.size();
}

关于java - 方法 getItemCount 中出现空指针异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32337908/

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