【问题标题】:Checking if the data from Firebase is equal to if else?检查来自 Firebase 的数据是否等于 if else?
【发布时间】:2022-01-21 13:01:21
【问题描述】:

如果来自 Firebase 的数据相同,我想打开一个新页面,但在编写的代码中,它会检查 xml 中的文本。如果来自 firebase 的数据相同,我只想让 If Else 工作。

我找不到哪里出错了,我才刚刚开始,我是初学者。 如果 text11 和 text22 相等,则应打开页面。

 public class MainActivity extends AppCompatActivity {

    TextView text11;
    TextView text22;
    View view;




    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        text11 = findViewById(R.id.text1);
        text22 = findViewById(R.id.text2);


        String veri1 = text11.getText().toString();
        String veri2 = text22.getText().toString();

        if(veri1.equals(veri2)) {
            System.out.println("firebase data equal");
        }else {
            setContentView(R.layout.fragment_sec);
            System.out.println("firebase data not equal");
        }

        //Firebase mesajı
        FirebaseDatabase database = FirebaseDatabase.getInstance();
        DatabaseReference myRef = database.getReference("pass1");


        myRef.addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(@NonNull @NotNull DataSnapshot snapshot) {

                String m=snapshot .getValue(String.class);
                text11.setText(m);


            }

            @Override
            public void onCancelled(@NonNull @NotNull DatabaseError error) {

            }
        });



        DatabaseReference myReff = database.getReference("pass2");
        myReff.addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(@NonNull @NotNull DataSnapshot snapshot) {

                String m=snapshot .getValue(String.class);
                text22.setText(m);


            }

            @Override
            public void onCancelled(@NonNull @NotNull DatabaseError error) {

            }
        });

    }

}

XML 代码;

<TextView
    android:id="@+id/text1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text=""
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toRightOf="parent"
    app:layout_constraintTop_toTopOf="parent" />

<TextView
    android:id="@+id/text2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text=""
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toRightOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    app:layout_constraintVertical_bias="0.603" />

【问题讨论】:

    标签: java android firebase if-statement firebase-realtime-database


    【解决方案1】:

    任何需要来自 Firebase 的数据的代码都需要在加载数据时调用的onDataChange 中。由于您需要数据库中的两条数据,因此您需要嵌套这些调用,然后在内部 onDataChange 中执行检查。

    在类似这样的代码中:

    // 1. Start loading first value
    myRef.addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(@NonNull @NotNull DataSnapshot snapshot) {
            // 2. Got first value
    
            String m=snapshot .getValue(String.class);
            text11.setText(m);
    
            // 3. Start loading second value
            DatabaseReference myReff = database.getReference("pass2");
            myReff.addValueEventListener(new ValueEventListener() {
                @Override
                public void onDataChange(@NonNull @NotNull DataSnapshot snapshot) {
                    // 4. Got second value
    
                    String m=snapshot .getValue(String.class);
                    text22.setText(m);
    
                    // 5. Compare values
                    String veri1 = text11.getText().toString();
                    String veri2 = text22.getText().toString();
    
                    if(veri1.equals(veri2)) {
                        System.out.println("firebase data equal");
                    }else {
                        setContentView(R.layout.fragment_sec);
                        System.out.println("firebase data not equal");
                    }
                }
    
                @Override
                public void onCancelled(@NonNull @NotNull DatabaseError error) {
                    throw error.toException(); // ? never ignore errors
                }
        });
        }
    
        @Override
        public void onCancelled(@NonNull @NotNull DatabaseError error) {
            throw error.toException(); // ? never ignore errors
        }
    });
    

    我已经在编号 cmets 中指出了通过此代码的流程。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-09-25
      • 2020-01-14
      • 1970-01-01
      • 2017-10-28
      • 2020-12-04
      • 1970-01-01
      • 1970-01-01
      • 2020-08-06
      相关资源
      最近更新 更多