首页 > 解决方案 > 如何从 Android Studio 中的 FireStore 数据库中获取 if..else 的值?

问题描述

我是 FireStore 的新手,我正在为教育机构构建一个应用程序。我有 2 个不同的批次,每个批次有 2 个不同的类别。{CBSE > xi, xii & CHSE > xi, xii}

我希望学生使用 FirebaseAuth 登录我的应用程序,而 FireStore 已经将这些学生数据作为

User(collection) >> users(Document)(using the userID given by FirebaseAuth)  >>  FullName: (Student's name)
                                                                                 Phone:(number)
                                                                                 Board:(There Batch or Board, i.e., CBSE or CHSE)
                                                                                 Class:(class, i.e., XI or XII)
                                                                                 email:(email)

因此,如果学生属于 CBSE 和 XI,我希望他们参加不同的活动,如果来自同一批次的 XII,则参加不同的活动。其他批次(CHSE)也同样如此。

我写了一个代码检查一下

public class test extends AppCompatActivity {

FirebaseAuth fauth;
FirebaseFirestore fstore;
String userID, mboard, mclass;
TextView Board, Class;
private Object testB, testC;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_maths);
    Board = findViewById(R.id.Board);
    Class = findViewById(R.id.Class);

    fauth = FirebaseAuth.getInstance();
    fstore = FirebaseFirestore.getInstance();

    userID = fauth.getCurrentUser().getUid();

    final DocumentReference documentReference = fstore.collection("users").document(userID);
    documentReference.addSnapshotListener(this, new EventListener<DocumentSnapshot>() {
        @Override
        public void onEvent(@Nullable DocumentSnapshot documentSnapshot, @Nullable FirebaseFirestoreException e) {
            Board.setText(documentSnapshot.getString("Board"));
            Class.setText(documentSnapshot.getString("Class"));
            mboard = Board.toString();
            mclass = Class.toString();
            testB = String.valueOf(documentSnapshot.getData().get(Board));
            testC = String.valueOf(documentSnapshot.getData().get(Class));


            if (testB == "CHSE") {
                if (testC == "XI") {
                    startActivity(new Intent(new Intent(getApplicationContext(),MainActivity.class)));
                }
                if (testC == "XII") {
                    startActivity(new Intent(new Intent(getApplicationContext(),test.class)));
                }
            }
        }
    });
  }
}

从 YouTube 教程中我得到了这个,但有两个问题:- 1. 我必须在布局上使用 textView。我怎样才能将值存储到变量中???2. if...else 不起作用。我只是希望这种情况发生。我将如何让学生去那里有批次和班级的受人尊敬的活动????

请帮助我,并提前感谢....

标签: javaandroidfirebaseif-statementgoogle-cloud-firestore

解决方案


运算符不是在 Java 中比较两个字符串的==有效方法。您应该改用该equals()方法。

if ("CHSE".equals(testB))

阅读:如何比较 Java 中的字符串?


推荐阅读