I created a CategoryChild class with a checkbox and a string for the checkbox text. Now I want to access to the Checkbox state in my adapter, but it returns null, what could be the problem?
Adapter
private ArrayList<CategoryName> categoryTopic = new ArrayList<>();
private ArrayList<CategoryName> allValues = new ArrayList<>();
private ArrayList<CategoryName> checkedValues = new ArrayList<>();
private ArrayList<CategoryName> uncheckedValues = new ArrayList<>();
@Override
public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
CategoryName parentItem = categoryTopic.get(position);
holder.categoryName.setText(parentItem.categoryName);
setCatItemRecycler(holder.childRecView, categoryTopic.get(position).getChildViewList(), parentItem.getCategoryName());
}
private void setCatItemRecycler(RecyclerView recyclerView, ArrayList<CategoryChild> categoryChildrenList, String categoryName) {
childAdapter = new SchrankChildAdapter(context, categoryChildrenList, categoryName);
recyclerView.setLayoutManager(new LinearLayoutManager(context));
recyclerView.setAdapter(childAdapter);
}
private Filter spinnerFilter = new Filter() {
@Override
protected FilterResults performFiltering(CharSequence constraint) {
ArrayList<CategoryName> filteredList = new ArrayList<>();
ArrayList<CategoryChild> filteredSubList = new ArrayList<>();
boolean containsSubItems = false;
if(constraint == null || constraint.length() == 0 || constraint.equals("all")) {
filteredList.addAll(allValues);
} else if(constraint.equals("checked")) {
ArrayList<CategoryChild> checkedChildList = new ArrayList<>();
for(CategoryName parent: categoryTopic) {
for(CategoryChild child: parent.getChildViewList()) {
System.out.println(child.getCategoryAttribute() + " has the Checkbox State: " + child.getCheckBoxState());
/*if(child.getCheckBoxState()) {
checkedChildList.add(new CategoryChild(child.getCategoryAttribute()));
}*/
checkedValues.add(new CategoryName(parent.categoryName, checkedChildList));
checkedChildList.clear();
}
}
// filteredList.addAll(checkedValues);
} else if(constraint.equals("unchecked")) {
filteredList.addAll(uncheckedValues);
}
FilterResults results = new FilterResults();
if(filteredList != null) {
results.values = filteredList;
}
else {
results.values = allValues;
}
return results;
}
@Override
protected void publishResults(CharSequence constraint, FilterResults results) {
/*categoryTopic.clear();
categoryTopic.addAll((ArrayList) results.values);
notifyDataSetChanged();
*/
}
};
public class ViewHolder extends RecyclerView.ViewHolder{
TextView categoryName;
RelativeLayout childView;
RecyclerView childRecView;
Button btFolderPlus, btFolderMinus;
Spinner dropDownMenu;
public ViewHolder(@NonNull View itemView) {
super(itemView);
categoryName = itemView.findViewById(R.id.categoryName);
childView = itemView.findViewById(R.id.childView);
childRecView = itemView.findViewById(R.id.childRecView);
btFolderPlus = itemView.findViewById(R.id.btFolderPlus);
btFolderMinus = itemView.findViewById(R.id.btFolderMinus);
dropDownMenu = itemView.findViewById(R.id.dropDown);
}
}
CategoryChild Class
public class CategoryChild {
private CheckBox checkBox;
private String checkBoxText;
public CategoryChild(String categoryAttribute) {
this.checkBoxText = categoryAttribute;
}
public CheckBox getCheckBox() {
return checkBox;
}
public void setCheckBox(CheckBox checkBox) {
this.checkBox = checkBox;
}
public String getCategoryAttribute() {
return checkBoxText;
}
public void setCategoryAttribute(String categoryAttribute) {
this.checkBox.setText(categoryAttribute);
}
public boolean getCheckBoxState()
{
return checkBox.isChecked();
}
public void setCheckBoxState(boolean isChecked) {
checkBox.setChecked(isChecked);
}
}
So this line of code from my adapter causes an error:
System.out.println(child.getCategoryAttribute() + " has the Checkbox State: " + child.getCheckBoxState());
The exception:
Filter: An exception occured during performFiltering()!
java.lang.NullPointerException: Attempt to invoke virtual method 'boolean android.widget.CheckBox.isChecked()' on a null object reference
at com.atromic.cookedia.CategoryChild.getCheckBoxState(CategoryChild.java:32)
at com.atromic.cookedia.SchrankAdapter$2.performFiltering(SchrankAdapter.java:184)
at android.widget.Filter$RequestHandler.handleMessage(Filter.java:236)
at android.os.Handler.dispatchMessage(Handler.java:107)
at android.os.Looper.loop(Looper.java:214)
at android.os.HandlerThread.run(HandlerThread.java:67)
I tested the same line without the getCheckBoxState() and just printing the name of that checkbox and it works. So there must be a value in the arraylist, but I'm not able to get the checkbox state, only the name of it.
UPDATE: I'll add my childAdapter which basically handles the checkboxes.
ChildAdapter
public SchrankChildAdapter(Context context, ArrayList<CategoryChild> childItemArrayList, String categoryName) {
this.context = context;
this.childItemArrayList = childItemArrayList;
this.categoryName = categoryName;
uncheckedSubList.addAll(childItemArrayList);
}
@NonNull
@Override
public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_category_item, parent, false);
return new ViewHolder(view);
}
@Override
public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
CategoryChild childItem = childItemArrayList.get(position);
holder.checkBox.setText(childItem.getCategoryAttribute());
childItem.setCheckBox(holder.checkBox);
childItem.getCheckBox().setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(childItem.getCheckBoxState()) {
System.out.println("Checkbox: " + childItem.getCategoryAttribute() + " is " + childItem.getCheckBoxState());
childItem.getCheckBox().setChecked(true);
checkedSubList.add(new CategoryChild(childItem.getCategoryAttribute()));
deleteChidList(uncheckedSubList, childItem.getCategoryAttribute());
containsChecked = true;
notifyDataSetChanged();
}
else if(!childItem.getCheckBoxState()) {
System.out.println("Checkbox: " + childItem.getCategoryAttribute() + " is " + childItem.getCheckBoxState());
childItem.getCheckBox().setChecked(false);
uncheckedSubList.add(new CategoryChild(childItem.getCategoryAttribute()));
deleteChidList(checkedSubList, childItem.getCategoryAttribute());
notifyDataSetChanged();
}
}
});
}
private void deleteChidList(ArrayList<CategoryChild> list, String childName){
if(list.size() > 0) {
for(int i = 0; i < list.size(); i++) {
if(childName.equals(list.get(i).getCategoryAttribute()))
list.remove(i);
}
}
}
public class ViewHolder extends RecyclerView.ViewHolder {
CheckBox checkBox;
public ViewHolder(@NonNull View itemView) {
super(itemView);
checkBox = itemView.findViewById(R.id.checkbox);
}
}