lalitahuja
lalitahuja

Reputation: 115

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

I'm making an app for an IoT Device My App is connected to the firebase database which shows the Ultrasonic sensor values. I have given the option of making pins on the IoT Device HIGH and LOW when the current value of the sensor is greater than the value set by the user in the app. I'm using a spinner that has only two values "HIGH" and "LOW". 1. When the Selected value>current value I want the values of the spinner to be updated in the database. 2. And when Current value>Selected value I want that the non selected values should get updated in the database. The case 1 is working fine but case 2 is not working.

In the code analogvalue means the current value of sensor and value means the selected 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");}
                        }
                    }

                }

[Screenshot of the app[][1][1]

Upvotes: 0

Views: 40

Answers (1)

Himanshu Dudhat
Himanshu Dudhat

Reputation: 1609

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))));
}

Check out this solution that will work for your case only if there are two values in spinners

Upvotes: 1

Related Questions