dimanche 25 décembre 2016

Android, Spinners onItemSelectedListener fires not at required time

I have a Spinner and Vertical LinearLayout views. When Spinners item changes, it will clear all child views inside LinearLayout and then fills it with CheckBoxes depending on selected Spinners object:

int selectedSpinnerIndex = 0;
Spinner mySpinner;
List<SpinnerData> spinnerDatas;
SpinnerAdapter spinnerAdapter;

LinearLayout layout;
List<CheckBox> checkboxes;

// lets just say that I somehow got spinnerDatas;



void setSpinnersAndCheckBoxes() {
    if (mySpinner.getAdapter() == null) {
        spinnerAdapter = new SpinnerAdapter(spinnerDatas, this);
        mySpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
           @Override
                public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
                    selectedSpinnerIndex = i;
                    layout.removeAllViews();
                    checkboxes.clear();
                    setSpinnersAndCheckBoxes()
                }

                @Override
                public void onNothingSelected(AdapterView<?> adapterView) {

                }
        }
    }

    if (checkBoxes.isEmpty()) {
        SpinnerData data = spinnerDatas.get(selectedSpinnerIndex);
        for (String checkBoxString : data.checkboxesStrings) {
            LinearLayout.LayoutParams lparams = new LinearLayout.LayoutParams(
                        LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
                CheckBox cb = new CheckBox(this);
                cb.setLayoutParams(lparams);
                cb.setText(checkBoxString.getName());
                checkBoxes.add(cb);
                llAddCarServices.addView(cb);
        } 
    }
}

I have made my code very simple and specific for this example. This code works fine. When Spinner changes its selected item, new checkboxes will appear in layout.

This works fine for creating new Data, but there is also updating existing data by using same activity.

When I want to update my existing data. First I download all spinner datas and make initial call for setSpinnersAndCheckBoxes() to populate spinners and checkboxes changing logic.

Then I change selected position of spinner according to existing datas value.

After that I need to set checked values for its checkboxes:

// This method called after initial call of setSpinnersAndCheckBoxes
void setDataForUpdate() {

   ExistingData existingData = getExistingData();
   for (int i = 0; i < spinnerDatas.size(); i++)
       if (spinnerDatas.get(i).getSpinnersComparingString().equals(existingData.getComparingString())) {
           mySpinner.setSelection(i, true);
           break;
       }

   // Then I need to compare checkboxes of selected Spinners data
   for (int i = 0; i < checkboxes.size(); i++) {
        if (existingData.getCheckBoxesComparingStrings().contains(checkboxes.get(i).getText().toString())) {
            checkboxes.get(i).setChecked(true);
        }
    }
}

No there is the main problem. Checkboxes always are unchecked. I have been trying to debug it and I have found out that setSelection of spinner fires after my for loop for checkboxes.

Does that mean that Listeners work asynchronously? Is there a method where I can wait before Listener finishes itself so I can start my for loop for checkboxes?




Aucun commentaire:

Enregistrer un commentaire