android,单击保存后将微调器值设置为默认值

时间:2017-05-13 12:34:09

标签: android spinner

所以我有这个方法将一些值保存到sharedpref。我也存放3个微调器。我有旋转器3的问题.Actualy有onclick监听器。例如,如果单击3,则会出现一个带有androids" setTimePicker"的弹出窗口。这很好用,但是当用户选择所有3个时钟时,我需要将微调器中的值设置为0,要么取消所有3个时钟,要么他点击保存。 我怎么能得到那个?添加图片以便更好地理解。

主页:

enter image description here

时间选择器:

enter image description here

选择时间之后:(微调器现在应该像主页一样!没有值!)

enter image description here

   public void shrani_nastavitve(){

        shrani_nastavitve.setOnClickListener(
                new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {

                        String bolecina_main = spinner_pain.getSelectedItem().toString();;
                        String zdravilo_main = spinner_medicine.getSelectedItem().toString();
                        String kolicina_main = spinner_dailyInput.getSelectedItem().toString();
                        String ura1_main = clock1.toString();
                        String ura2_main = clock2.toString();
                        String ura3_main = clock3.toString();

                        SharedPreferences sharedPreferences = getActivity().getSharedPreferences("nastavitve_uporabnika", Context.MODE_PRIVATE);
                        SharedPreferences.Editor editor = sharedPreferences.edit();

                        if (bolecina_main.equals("")) {
                        } else {
                            editor.putString("bolecina", bolecina_main);
                        }
                        if (zdravilo_main.equals("")) {
                        } else {
                            editor.putString("zdravilo", zdravilo_main);
                        }
                        if (kolicina_main.equals("")) {
                        } else {
                            editor.putString("dnevna_kolicina", kolicina_main);
                        }
                        if (ura1_main.equals("")) {
                        } else {
                            editor.putString("ura1", ura1_main);
                        }
                        if (ura2_main.equals("")) {
                        } else {
                            editor.putString("ura2", ura2_main);
                        }
                        if (ura3_main.equals("")) {
                        } else {
                            editor.putString("ura3", ura3_main);
                        }
                        editor.apply();
                        Toast.makeText(getActivity(), "Uspešno shranjeno!", Toast.LENGTH_LONG).show();
                    }
                });

    }

微调值

 <string-array name="spinner_myDailyMedicineInput">
        <item></item>
        <item>1</item>
        <item>2</item>
        <item>3</item>
        <item>4</item>
        <item>5</item>
        <item>6</item>
        <item>7</item>
        <item>8</item>
    </string-array>



 spinner_dailyInput.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                switch (position){

                    case 1:
                        for(int i=0;i<1;i++) {
                            clock2=null;
                            clock3=null;
                            clock4=null;
                            clock5=null;
                            clock6=null;
                            clock7=null;
                            clock8=null;
                            setTimePicker(i);
                        }
                        break;
                    case 2:
                        for(int i=0;i<2;i++) {
                            clock3=null;
                            clock4=null;
                            clock5=null;
                            clock6=null;
                            clock7=null;
                            clock8=null;
                            setTimePicker(i);
                        }
                        break;
                    case 3:
                        for(int i=0;i<3;i++) {
                            clock4=null;
                            clock5=null;
                            clock6=null;
                            clock7=null;
                            clock8=null;
                            setTimePicker(i);
                        }
                        break;
                    case 4:
                        for(int i=0;i<4;i++) {
                            clock5=null;
                            clock6=null;
                            clock7=null;
                            clock8=null;
                            setTimePicker(i);
                        }
                        break;
                    case 5:
                        for(int i=0;i<5;i++) {
                            clock6=null;
                            clock7=null;
                            clock8=null;
                            setTimePicker(i);
                        }
                        break;
                    case 6:
                        for(int i=0;i<6;i++) {
                            clock7=null;
                            clock8=null;
                            setTimePicker(i);
                        }
                        break;
                    case 7:
                        for(int i=0;i<7;i++) {
                            clock8=null;
                            setTimePicker(i);
                        }
                        break;
                    case 8:
                        for(int i=0;i<8;i++) {
                            setTimePicker(i);
                        }

                        break;
                    default:
                }

            }

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

            }
        });

1 个答案:

答案 0 :(得分:0)

我提出了解决方案,删除了一些代码,并在初始化保存后清除了微调器。

 shrani_nastavitve.setOnClickListener(
                new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {

                        if (spinner_pain == null)
                        {
                            bolecina_main="";
                        }
                        else  {
                            bolecina_main = spinner_pain.getSelectedItem().toString();
                        }
                        if (spinner_medicine== null){
                            zdravilo_main="";
                        }
                        else {
                            zdravilo_main = spinner_medicine.getSelectedItem().toString();
                        }
                        if (spinner_dailyInput==null){
                            kolicina_main="";
                        }
                        else {
                            kolicina_main = spinner_dailyInput.getSelectedItem().toString();
                        }
                        if(clock1==null){
                            ura1_main="";
                        }
                        else {
                            ura1_main = clock1.toString();
                        }
                        if(clock2==null){
                            ura2_main="";
                        }
                        else {
                            ura2_main = clock2.toString();
                        }
                        if(clock3==null){
                            ura3_main="";
                        }
                        else {
                            ura3_main = clock3.toString();
                        }

                        SharedPreferences sharedPreferences = getActivity().getSharedPreferences("nastavitve_uporabnika", Context.MODE_PRIVATE);
                        SharedPreferences.Editor editor = sharedPreferences.edit();

                        if (bolecina_main.equals("")) {
                        } else {
                            editor.putString("bolecina", bolecina_main);
                        }
                        if (zdravilo_main.equals("")) {
                        } else {
                            editor.putString("zdravilo", zdravilo_main);
                        }
                        if (kolicina_main.equals("")) {
                        } else {
                            editor.putString("dnevna_kolicina", kolicina_main);
                        }
                        if (ura1_main.equals("")) {
                        } else {
                            editor.putString("ura1", ura1_main);
                        }
                        if (ura2_main.equals("")) {
                        } else {
                            editor.putString("ura2", ura2_main);
                        }
                        if (ura3_main.equals("")) {
                        } else {
                            editor.putString("ura3", ura3_main);
                        }
                        editor.apply();
                        Toast.makeText(getActivity(), "Uspešno shranjeno!", Toast.LENGTH_LONG).show();
                        spinner_dailyInput.setAdapter(null);
                        spinner_pain.setAdapter(null);
                        spinner_medicine.setAdapter(null);
                    }


                });