vendredi 22 avril 2016

How to save checkboxes state with onSaveInstanceState

I have an Alert Dialog that displays a couple of checkboxes(5 to be more exact). The check boxes filter 5 lists of markers that are displayed on my map, markers that are grouped in hour intervals. I have managed to make everything work except the fact that I do not know how I should write the code in onSaveInstanceState so that the checkboxes values will be kept after a screen rotation. Here is the part of the code that I think it's relevant for the question. Thanks in advance for any help!

public void filterTheMarkers(View view) {

        if (dialog == null) {

            AlertDialog.Builder builder;
            builder = new AlertDialog.Builder(this);
            LayoutInflater inflater = this.getLayoutInflater();
            @SuppressLint("InflateParams") View checkBoxView = inflater.inflate(R.layout.markers_filtering, null);
            builder.setView(checkBoxView);
            cbAllDay = (CheckBox) checkBoxView.findViewById(R.id.checkBox1);
            cbBefore12 = (CheckBox) checkBoxView.findViewById(R.id.checkBox2);
            cbBetween1216 = (CheckBox) checkBoxView.findViewById(R.id.checkBox3);
            cbBetween1620 = (CheckBox) checkBoxView.findViewById(R.id.checkBox4);
            ccbAfter20 = (CheckBox) checkBoxView.findViewById(R.id.checkBox5);

            dialog = builder.create();

        }
        dialog.show();
    }


public void displaySelectedMarkers(View view) {


        dialog.dismiss();
        Log.i("TAG", "All Day " + cbAllDay.isChecked() + " Before 12 " + cbBefore12.isChecked() + " Between 12-16 " + cbBetween1216.isChecked() + " Between 16-20" + cbBetween1620.isChecked() + " After 20 " + ccbAfter20.isChecked());
        //according these check boxes status execute your code to show/hide markers

        if (cbAllDay.isChecked() && cbBefore12.isChecked() && cbBetween1216.isChecked() && cbBetween1620.isChecked() && ccbAfter20.isChecked()) {
            // show all markers
            for (Marker marker : allDayList) {
                marker.setVisible(true);
            }
            for (Marker marker : before12List) {
                marker.setVisible(true);
            }
            for (Marker marker : between1216List) {
                marker.setVisible(true);
            }
            for (Marker marker : between1620List) {
                marker.setVisible(true);
            }
            for (Marker marker : after20List) {
                marker.setVisible(true);
            }
        } else if (cbAllDay.isChecked() && !cbBefore12.isChecked() && !cbBetween1216.isChecked() && !cbBetween1620.isChecked() && !ccbAfter20.isChecked()) {
            // show only All Day Markers
            for (Marker marker : allDayList) {
                marker.setVisible(true);
            }
            for (Marker marker : before12List) {
                marker.setVisible(false);
            }
            for (Marker marker : between1216List) {
                marker.setVisible(false);
            }
            for (Marker marker : between1620List) {
                marker.setVisible(false);
            }
            for (Marker marker : after20List) {
                marker.setVisible(false);
            } // and it goes like this forever covering every possible interval
                        .
                        .
                        .
     }

    }

    public void doNothing(View view) {

        dialog.dismiss();
    }




Aucun commentaire:

Enregistrer un commentaire