首页 > 解决方案 > 不允许用户添加相同的命名子 Firebase Android?

问题描述

这是我的 Firebase 数据库结构

我试图将孩子(程序)添加到上述参考学生中。

所以问题是当我检索密钥并将它们添加到字符串的 ArrayList 时。

并检查它是否与它给出的输入文本匹配;像例子; 在照片中,我们有三个程序 bps,cs,mm。

它给了我匹配,不匹配,不匹配的结果我想要的是它是否像上面那样匹配。

代码

        // retreiving the edittext to String
        String prnamez = programmeName.getEditableText().toString().trim().toLowerCase();

        // checking if the program already exists
        FirebaseAuth autha = FirebaseAuth.getInstance();

        DatabaseReference dnn = FirebaseDatabase.getInstance()
                .getReference("Admin").child(autha.getUid()).child("INSTITUTE")
                .child("STUDENTS");

        // arraylist to add the keys of the programme
        ArrayList<String> pgName = new ArrayList<>();


        // value event listener
        dnn.addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(@NonNull DataSnapshot snapshot) {
                if (snapshot.exists()) {

                    progressBar.setVisibility(View.VISIBLE);

                // going through all the keys and adding them to the arraaylist of pgName
                    for (DataSnapshot regis : snapshot.getChildren()) {

                        // retrieved the programme Name
                        pgName.add(regis.getKey().toString());


                    }

                    // going through the lenght of the array list
                    for (int i = 0; i < pgName.size(); i++) {

                       // if the entered programme matches already existing programme name
                        // then show this custom Toast

                        if (pgName.get(i).equals(prnamez)) {
                            // custom Toast
                            StyleableToast.makeText(Programme_students.this,
                                    "Matches", R.style.exampleToast).show();
                        } else {

                            StyleableToast.makeText(Programme_students.this,
                                    "not Matches", R.style.exampleToast).show();
                            
                            // WANT TO ADD NEW PROGRAMME IN THIS CASE 
                        }


                    }


                }
            }

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

            }
        });
    }
});

标签: androidfirebasefirebase-realtime-database

解决方案


推荐阅读