I'm working with Checkboxes and a Spinner in which you can select how to filter your Checkboxes. You've got 3 options:
1 - "All" aka "Alle" -> Show all Checkboxes (Checked & Unchecked)
2 - "Checked" aka "Vorhanden" -> Show all checked Checkboxes
3 - "Unchecked" aka "Nicht vrhd." -> Show all unchecked Checkboxes
Let me show the necessary information in my Adapter, I'm working on, first:
SchrankAdapter:
public SchrankAdapter(Context context, ArrayList<CategoryName> categoryTopic, Map<String, String> dropDownValues, RelativeLayout parentLayout){
this.context = context;
this.categoryTopic = categoryTopic;
this.uncheckedValues.addAll(categoryTopic);
this.dropDownValues = dropDownValues;
this.parentLayout = parentLayout;
this.allValues.addAll(categoryTopic);
}
@NonNull
@Override
public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_schrank, parent, false);
return new ViewHolder(view);
}
@Override
public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
CategoryName parentItem = categoryTopic.get(position);
holder.categoryName.setText(parentItem.getCategoryName());
setCatItemRecycler(holder.childRecView, parentItem.getChildViewList(), parentItem.getCategoryName());
}
private void setCatItemRecycler(RecyclerView recyclerView, ArrayList<CategoryChild> categoryChildrenList, String categoryName) {
childAdapter = new SchrankChildAdapter(context, categoryChildrenList, categoryName, this, parentLayout);
recyclerView.setLayoutManager(new LinearLayoutManager(context));
recyclerView.setAdapter(childAdapter);
}
public Filter getSpinnerFilter() {
return spinnerFilter;
}
private Filter spinnerFilter = new Filter() {
@Override
protected FilterResults performFiltering(CharSequence constraint) {
ArrayList<CategoryName> filteredList = new ArrayList<>();
if(constraint == null || constraint.length() == 0 || constraint.equals("all")) {
filteredList.addAll(allValues);
spinnerValue = "all";
} else if(constraint.equals("checked")) {
filteredList.addAll(checkedValues);
spinnerValue = "checked";
} else if(constraint.equals("unchecked")) {
filteredList.addAll(uncheckedValues);
spinnerValue = "unchecked";
}
FilterResults results = new FilterResults();
if(filteredList != null)
results.values = sortEverything(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);
}
}
And this 2 methods are called on each checkbox, which is getting checked or unchecked:
@Override
public void onCheckboxChangeTrue(CategoryChild childItem, String categoryName) {
boolean containsName = false;
if(this.checkedValues != null && !this.checkedValues.isEmpty()) {
for(CategoryName checkedCategory: this.checkedValues) {
if(checkedCategory.getCategoryName().equals(categoryName)) {
if(!checkedCategory.getChildViewList().contains(childItem)) {
checkedCategory.getChildViewList().add(childItem);
}
containsName = true;
}
}
}
if(!containsName) {
ArrayList<CategoryChild> childList = new ArrayList<>();
childList.add(childItem);
this.checkedValues.add(new CategoryName(categoryName, childList));
}
if(!this.uncheckedValues.isEmpty() && this.uncheckedValues != null) {
for(CategoryName uncheckedCategory: this.uncheckedValues) {
if(uncheckedCategory.getCategoryName().equals(categoryName)) {
if(uncheckedCategory.getChildViewList().contains(childItem)) {
uncheckedCategory.getChildViewList().remove(childItem);
break;
}
}
}
} else {
this.uncheckedValues.clear();
}
}
@Override
public void onCheckboxChangeFalse(CategoryChild childItem, String categoryName) {
boolean containsName = false;
if(!this.checkedValues.isEmpty() && this.checkedValues != null) {
for (CategoryName checkedCategory : this.checkedValues) {
if (checkedCategory.getCategoryName().equals(categoryName)) {
if (checkedCategory.getChildViewList().contains(childItem)) {
checkedCategory.getChildViewList().remove(childItem);
break;
}
}
}
}
else {
this.checkedValues.clear();
}
for(CategoryName uncheckedCategory: this.uncheckedValues) {
if(uncheckedCategory.getCategoryName().equals(categoryName)) {
if(!uncheckedCategory.getChildViewList().contains(childItem)) {
uncheckedCategory.getChildViewList().add(childItem);
}
containsName = true;
}
}
if(!containsName) {
ArrayList<CategoryChild> childList = new ArrayList<>();
childList.add(childItem);
this.uncheckedValues.add(new CategoryName(categoryName, childList));
}
}
Here comes the problem:
Everytime I check a checkbox, it gets added to "checkedValues" which is fine, but it gets removed from "uncheckedValues" AND "allValues" even though, allValues should stay the same.
And I don't even call the remove() method on the "allValues" Arraylist, only on "uncheckedValues" and "checkedValues". So how is it possible, that elements of "allValues" get removed as well?
Thanks in advance!
Aucun commentaire:
Enregistrer un commentaire