java - 如何从 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 不起作用。我只是希望这种情况发生。我将如何让学生去那里有批次和班级的受人尊敬的活动????
请帮助我,并提前感谢....
解决方案
推荐阅读
- pivot - TradingView 图表上的水平线
- python - 为什么在检查界面时会出现语法错误?尝试使用 Python/argparse 为 Linux 制作 MAC 转换器
- c++ - 使用 SFML VertexArray 制作抛物线曲线:左侧未正确渲染
- python - 如何使函数可挑选,以便可以通过 scipy.optimize.differential_evolution 的并行版本将其最小化
- go - atomic.AddInt64() 是否强制从主内存获取/更新值?
- reactjs - 无法使用 reducer 访问我的数据存储中的值
- html - 我怎样才能有一个无响应的背景?
- c++ - 使用 c++ gslice 隐藏 valarray 中的特定元素
- python - MemoryError:无法为形状为 (1, 128, 128) 且数据类型为 float32 的数组分配 64.0 KiB
- python - 将列表中的每个值与另一个列表中的两个值进行比较