Android Firestore检查字段是否存在

时间:2019-02-03 18:10:50

标签: java android google-cloud-firestore

我需要帮助解决问题。 我是Firestore的新手,我来自MySQL。 我有一个注册表,用户可以在其中注册(Restaurateur),该应用程序可以正常运行,数据可以完美保存。 但是,问题在于,当用户注册时,如果餐馆的名称与数据库中已经存在的另一个餐馆相同,我将无法阻止注册。

在代码中首先检查是否存在相同的文档,如果不存在,我要检查餐厅的名称,如果它与另一个名称相同,则阻止所有内容并停止记录。

我试图通过在IF条件下插入dbristoratore.collection (" Restaurateurs "). WhereEqualTo (" name_RistoranteR ", nameRistoranteR);来做到这一点,但是什么也没有。 但是,如果我以某种方法回忆起它可以起作用,但是问题出在,如果餐馆名称是免费的,我不知道该如何切换到注册。 代码是这个

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_r_r_start);

        nomeristoratore =(EditText)findViewById(R.id.editText7);
        cognomeristoratore =(EditText)findViewById(R.id.editText8);
        nomeristorante = (EditText)findViewById(R.id.editText10);
        data_nascitaristoratore =(EditText)findViewById(R.id.editText9);
        id_fkristoratore =(EditText)findViewById(R.id.editText11);
        numero_telefonoristoratore =(EditText)findViewById(R.id.editText12);

        rGroupristoratore = (RadioGroup)findViewById(R.id.radiogroup2);

        registraristoratore = (Button)findViewById(R.id.button4);

        registraristoratore.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                registrazioneRistoratore();
            }

            private void registrazioneRistoratore() {
                final String nomeR = nomeristoratore.getText().toString();
                final String cognomeR = cognomeristoratore.getText().toString();
                final String nomeRistoranteR = nomeristorante.getText().toString();
                final String dataR = data_nascitaristoratore.getText().toString();
                final String id_fk_R = id_fkristoratore.getText().toString();
                final String numero_telefono_R = numero_telefonoristoratore.getText().toString();
                final String sessoR = ((RadioButton) findViewById(rGroupristoratore.getCheckedRadioButtonId())).getText().toString();








                if (nomeR.isEmpty()){
                    nomeristoratore.setError("Inserisci il tuo nome");
                    nomeristoratore.requestFocus();
                    return;
                }
                if (cognomeR.isEmpty()){
                    cognomeristoratore.setError("Inserisci il tuo cognome");
                    cognomeristoratore.requestFocus();
                    return;
                }
                if (dataR.isEmpty()){
                    data_nascitaristoratore.setError("Inserisci la tua data di nascita");
                    data_nascitaristoratore.requestFocus();
                    return;
                }
                if (dataR.length() != 10){
                    data_nascitaristoratore.setError("Formato data errato");
                    data_nascitaristoratore.requestFocus();
                    return;
                }
                if (nomeRistoranteR.isEmpty()){
                    nomeristorante.setError("Inserisci il nome del ristorante");
                    nomeristorante.requestFocus();
                    return;

                }

                dbristoratore.collection("Ristoratori").document(id_fk_R).get().addOnCompleteListener(new OnCompleteListener<DocumentSnapshot>() {
                    @Override
                    public void onComplete(@NonNull Task<DocumentSnapshot> task) {
                        if (task.getResult().exists()) {
                            Toast.makeText(getApplicationContext(), "Sei già registrato", Toast.LENGTH_LONG).show();


                            // cognome.setError("Team with same name already exists");
                            //cognome.requestFocus();


                        } else if(("nome_ristoranteR".equals(nomeRistoranteR))
                        ) {
                            dbristoratore.collection("Ristoratori").whereEqualTo("nome_ristoranteR", nomeRistoranteR);
                            Toast.makeText(getApplicationContext(), "Use another Name restaurant", Toast.LENGTH_LONG).show();                        }

                            else {

                        //If team with same name doesn't exist, save it
                        dbristoratore.collection("Ristoratori").document(id_fk_R).get().addOnCompleteListener(new OnCompleteListener<DocumentSnapshot>() {
                            @Override
                            public void onComplete(@NonNull Task<DocumentSnapshot> task) {
                                DocumentSnapshot documentSnapshot = task.getResult();


                                //Check if user is already in a team
                                //TeamMap data1 = new TeamMap(teamNameText, teamCountryText, Username);
                                RistoratoreMap data2 = new RistoratoreMap(nomeR, cognomeR, dataR, nomeRistoranteR, numero_telefono_R, id_fk_R, sessoR);
                                dbristoratore.collection("Ristoratori").document(id_fk_R).set(data2).addOnCompleteListener(new OnCompleteListener<Void>() {

                                    // Map<String, Object> data = new HashMap<>();
                                    // data.put("username", Username);
                                    //  db.collection("TeamUsers").document(teamNameText).set(data);
                                    //  Map<String, Object> data2 = new HashMap<>();
                                    //  data2.put("User's team", teamNameText);

                                    //  db.collection("Users").document(teamNameText).set(data2).addOnCompleteListener(new OnCompleteListener<Void>() {
                                    @Override
                                    public void onComplete(@NonNull Task<Void> task) {
                                        if (task.isSuccessful()) {
                                            startActivity(new Intent(R_r_start.this, onboarding_r.class));

                                            Toast.makeText(getApplicationContext(), "Registrato con successo", Toast.LENGTH_LONG).show();
                                        } else {
                                            Toast.makeText(getApplicationContext(), "Errore, prova a disinstallare l'app e installarla di nuovo", Toast.LENGTH_LONG).show();

                                        }
                                    }
                                });

                            }
                        });
                    }



                }


                    });}});}}

如果您对如何帮助我有任何想法,我希望我已经很好地解释了自己。

谢谢enter image description here

0 个答案:

没有答案