Stayheuh
Stayheuh

Reputation: 147

How to show data from the cloud firestore in user's phone in android studio?

enter image description here

Here is my actual database in the cloud firestore. I want to show these datas in a simple way, just lines of text. I was trying but I only come this far. The request actually reaches to the firestore. But can't fetch the data.
Also proof that it actually works in logcat:

enter image description here

package com.example.mezuniyet2nat;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import android.nfc.Tag;
import android.os.Bundle;
import android.util.Log;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
import com.google.android.gms.tasks.OnCompleteListener;
import com.google.android.gms.tasks.Task;
import com.google.firebase.FirebaseApp;
import com.google.firebase.firestore.FirebaseFirestore;
import com.google.firebase.firestore.QueryDocumentSnapshot;
import com.google.firebase.firestore.QuerySnapshot;
import java.util.ArrayList;
public class MainActivity extends AppCompatActivity {
ListView listele;
String [] dataz;
//these are not used, I ignored them later.


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        final String TAG = "bisey";
        TextView theTextView= (TextView) findViewById(R.id.tekst);


        FirebaseFirestore db = FirebaseFirestore.getInstance();
        db.collection("tablo")
                .get()
                .addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
                    @Override
                    public void onComplete(@NonNull Task<QuerySnapshot> task) {
                        if (task.isSuccessful()) {
                            for (QueryDocumentSnapshot document : task.getResult()) {
                                Log.d(TAG, document.getId() + " => " + document.getData());

                                String   bisey =  FirebaseFirestore.getInstance().getNamedQuery("adSoyad").toString();

                                theTextView.setText(bisey);
                            }
                        } else {
                            Log.w(TAG, "Error getting documents.", task.getException());
                        }
                    }
                });
        }
}

Upvotes: 0

Views: 662

Answers (1)

Frank van Puffelen
Frank van Puffelen

Reputation: 599491

Replace this line:

String   bisey =  FirebaseFirestore.getInstance().getNamedQuery("adSoyad").toString();

With this:

String bisey = document.get("adSoyad").toString();

I highly recommend keeping the reference documentation handy, like in this case for the DocumentSnapshot class.

Upvotes: 1

Related Questions