Search code examples
javaandroidgoogle-cloud-firestoretextview

How to get the EditText username into .whereEqualTo. FireStore?


How can I as an Admin, Look for another user by typing its name(/Naam), and project/set its "tegoed" into a textview?

[EDIT: I do get a TextView when hardcoded "Anne" at WhereEqualTo, But I have no idea how to make this work with an EditText where I can fill in someones name]

My Firestore is build like this , both fields are Strings for now. but the numbers will be numbers after testing

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

    Naamtgd = findViewById(R.id.etNaamtgd);
    Tegoed = findViewById(R.id.etTgd);
    tvTegoed = findViewById(R.id.tvTgd);
    btnZoek = findViewById(R.id.btnZoek);
    btnVulAan = findViewById(R.id.btnVulAan);
    btnTerug = findViewById(R.id.btnTerug);
    NAAM = Naamtgd.getText().toString();
    mAuth = FirebaseAuth.getInstance();
    fStore = FirebaseFirestore.getInstance();


    btnZoek.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {

            fStore.collection("Gebruikers")
                    .whereEqualTo("Naam", "Anne")
                    .get()
                    .addOnCompleteListener(task -> {
                        if (task.isSuccessful()) {
                            Toast.makeText(Tegoedaanvullen.this, "Gelukt?", Toast.LENGTH_SHORT).show();

                            for (QueryDocumentSnapshot document : task.getResult()) {
                             tvTegoed.setText(document.getString("Tegoed"));


                            }
                        }
                        else{
                            Toast.makeText(Tegoedaanvullen.this, "Mislukt", Toast.LENGTH_SHORT).show();


                        }
                    });
        }
    });

I think this might be interesting, when I hard-code "ANNE" the word : Value , appears. It doesnt when I put in the String: NAAM;

click here for the image

NEW LOGS


Solution

  • I found the problem. With help of the Logs!

    String NAAM = Edittext.getText.toString was typed before the button was pressed, in OnCreate.

    So, it already being filled at the second I opened the page.

    Now, it's in the .setOnClicklistener, and it works!