jeudi 15 août 2019

Checkbox true/false for ischecked working, But not displaying the ui check mark

I have a list of timezones that gets added to a recycler view. However, my main list in the activity checks properly but when i use the search and the list condenses and i click the checkbox it will not show the checkmark. However, in debug the value is set to true when clicked and it will still add it into the recycler view properly.

I have tried looking online but there was no solution for this specific problem.

@Override
    public void onBindViewHolder(@NonNull final TimezoneViewHolder holder, final int position) {
        // Initialize tools
        final Timezone_Item currentTimezoneItem = timezoneList.get(position);
        int pos = currentTimezoneItem.getId();
        final int tzID = --pos;

        holder.mChkboxSelect.setText(currentTimezoneItem.getValue());
        holder.mUTCCode.setText(currentTimezoneItem.getName());


        // This is the solution for... Clicking the checkbox once would select multiple timezones. Not with this.
        if(selectedTimezones.get(position)){
            holder.mChkboxSelect.setChecked(true);
            currentTimezoneItem.setIsSelected(true);
        }else{
            holder.mChkboxSelect.setChecked(false);
            currentTimezoneItem.setIsSelected(false);
        }

        // Manually activate the clicks in checkbox
        holder.mChkboxSelect.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(currentTimezoneItem.getIsSelected()){
                    currentTimezoneItem.setIsSelected(false);
                    holder.mChkboxSelect.setChecked(false);
                }else {
                    currentTimezoneItem.setIsSelected(true);
                    holder.mChkboxSelect.setChecked(true);
                }

                if(TimezonePickerActivity.isSearching){
                    selectedTimezones.put(currentTimezoneItem.getId() - 1, currentTimezoneItem.getIsSelected());
                }else {
                    selectedTimezones.put(tzID, currentTimezoneItem.getIsSelected());
                }

                notifyDataSetChanged();
            }
        });
    }

This is my Search filter...

private Filter SearchFilter = new Filter() {
        @Override
        protected FilterResults performFiltering(CharSequence searchText) {
            List<Timezone_Item> filteredList = new ArrayList<>();

            if (searchText == null || searchText.length() == 0) {
                TimezonePickerActivity.isSearching = false;
                filteredList.addAll(timezoneListFull);
            } else {
                String filterPattern = searchText.toString().toLowerCase().trim();
                TimezonePickerActivity.isSearching = true;

                for (Timezone_Item item : timezoneListFull) {
                    if (item.getName().toLowerCase().contains(filterPattern)) {
                        filteredList.add(item);
                    }
                }
            }

            FilterResults filterResults = new FilterResults();
            filterResults.values = filteredList;

            return filterResults;
        }

        @Override
        protected void publishResults(CharSequence searchText, FilterResults results) {
            timezoneList.clear();
            timezoneList.addAll((List) results.values);
            notifyDataSetChanged();
        }
    };

This is my code to add the selected timezone into the recycler view

fabAddTimezone.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) { SparseBooleanArray selectedTimezones = Timezone_RVAdapter.selectedTimezones;

                // Filter out false values
                for (int i = 0; i < selectedTimezones.size(); i++) {
                    if(!selectedTimezones.valueAt(i)){
                        selectedTimezones.removeAt(i);
                        selectedTimezones.delete(i);
                    }
                }

                // Take filtered values and find its index to grab text and UTC code
                for (int i = 0; i < selectedTimezones.size(); i++) {
                    // Get the position(Key) which is actually the Timezone_Item ID
                    int position = selectedTimezones.keyAt(i);

                    // Create new clock item to add into list
                    Clock_Item clockItem = new Clock_Item(
                            Timezone_RVAdapter.timezoneListFull.get(position).getName(),
                            Timezone_RVAdapter.timezoneListFull.get(position).getValue()
                    );

                    // Add clock to a list
                    mClockList.add(clockItem);
                }

                // Save clock list
                sharedPrefs.SaveClockList(mClockList);

                // Go back to main menu. Clock list should automatically load once activity boots
                finish();
            }
        });




Aucun commentaire:

Enregistrer un commentaire