如何在 Android Studio 中只有两个项目的微调器中使用未选择的值?

How to use the non-selected value in a spinner which has only two items in Android Studio?

我正在为 IoT 设备制作应用程序我的应用程序已连接到显示超声波传感器值的 firebase 数据库。当传感器的当前值大于用户在应用程序中设置的值时,我已经给出了使物联网设备上的引脚高和低的选项。 我使用的微调器只有两个值 "HIGH" 和 "LOW"。 1. When the Selected value>current value I want the values of the spinner to be updated in the database. 2.当当前值>选定值时,我希望未选定的值应该在数据库中得到更新。 情况 1 工作正常,但情况 2 不工作。

代码中analogvalue表示传感器的当前值,value表示选择的值。

   // Read from the database
            myRef = FirebaseDatabase.getInstance().getReference();
            myRef.addValueEventListener(new ValueEventListener() {
                @Override
                public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
                    // This method is called once with the initial value and again
                    // whenever data at this location is updated.
                    if(StringValue.length()>0 && link2.length()>0) {

                        try{
                        status = dataSnapshot.child("Distance").getValue().toString();}
                        catch (NullPointerException ignored){}


                        if(status==null){
                            TextView text4 = layout.findViewById(R.id.text4);
                            text4.setText(" Error: Wrong ID. ");
                            // Toast...
                            Toast toast = new Toast(getApplicationContext());
                            toast.setGravity(Gravity.CENTER_VERTICAL, 0, 540);
                            toast.setDuration(Toast.LENGTH_LONG);
                            toast.setView(layout);
                            toast.show();
                        }
                        else{ analogvalue = Integer.parseInt(status);}
                        pin.setText(status);
                        final int value = Integer.parseInt(StringValue);



                        if (analogvalue > value) {
                            Firebase fireChild = fire2.child( "Pin1");
                            fireChild.setValue(spinner1.getSelectedItem().toString());
                            Firebase fireChild1 = fire2.child( "Pin2");
                            fireChild1.setValue(spinner2.getSelectedItem().toString());

                        }
                        if(analogvalue <= value) {
                            if(spinner1.getSelectedItem().toString().equals("HIGH")){Firebase fireChild = fire2.child("Pin1"); fireChild.setValue("LOW");}
                            else {Firebase fireChild = fire2.child("Pin1"); fireChild.setValue("HIGH");}
                            if(spinner2.getSelectedItem().toString().equals("HIGH")){Firebase fireChild = fire2.child("Pin2"); fireChild.setValue("LOW");}
                            else {Firebase fireChild = fire2.child( "Pin2"); fireChild.setValue("HIGH");}
                        }
                    }

                }

[

if (analogvalue > value) {
    Firebase fireChild = fire2.child(link2 + "/Pin1");
    fireChild.setValue(spinner1.getSelectedItem().toString());
    Firebase fireChild1 = fire2.child(link2 + "/Pin2");
    fireChild1.setValue(spinner2.getSelectedItem().toString());

} else if(analogvalue <= value) {
    Firebase fireChild = fire2.child(link2 + "/Pin2");
    fireChild.setValue(spinner1.getItemAtPosition(Math.abs(spinner.getSelectedItemPosition()-1)));
    Firebase fireChild1 = fire2.child(link2 + "/Pin1");
    fireChild1.setValue(Math.abs(spinner2.getItemAtPosition(Math.abs(spinner.getSelectedItemPosition()-1))));
}

查看此解决方案,仅当微调器中有两个值时才适用于您的情况