我正在尝试使用Firebase制作一个Android应用程序。 在该应用程序中,我给出了基于传感器值控制输出设备的功能。 例如,如果(来自LDR传感器的)光强度值大于用户输入的值,则LED将为HIGH。而且我还将这些值保存在另一个使用数据库类的活动中。
我能够完成所有这些操作,但是ListView中的数据被多次添加。
我认为这段代码给出了错误
if(analogvalue < value){
mDatabaseHelper.addData(status);
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");}
}
confirm2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
// Get your custom_toast.xml layout
LayoutInflater inflater = getLayoutInflater();
final View layout = inflater.inflate(R.layout.custom_toast,
(ViewGroup) findViewById(R.id.custom_toast_layout_id));
//Initializing the text views
EditText editText = findViewById(R.id.link2);
final String link2 = editText.getText().toString();
EditText editText1 = findViewById(R.id.idvalue);
final String StringValue = editText1.getText().toString();
//Adding functionality to the text views
fire2 = new Firebase("https://iotsense.firebaseio.com/");
Firebase fireChild = fire2.child("S1");
fireChild.setValue("4");
// 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("Analog").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();
return;
}
else{
analogvalue = Integer.parseInt(status);
pin.setText(status);
mDatabaseHelper.addData(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");}
}
}
}
}
@Override
public void onCancelled(@NonNull DatabaseError error) {
// Failed to read value
// Log.w(TAG, "Failed to read value.", error.toException());
}
});
在数据库中,值被多次添加。
我认为,如果我可以使用Spinner的未选择值,那么if(analogvalue < value)
循环将不会运行多次。