简体   繁体   中英

How to pass firebase data from one activity to another?

I want to pass data from one activity to another, first I download it from Firebase Realtime Database and then pass it to another activity and how to use it in it.

my data like this.(image from the inte.net)

  cam_firebase.addChildEventListener(new ChildEventListener() {
        @Override
        public void onChildAdded(@NotNull DataSnapshot dataSnapshot, String s) {





            // long count = dataSnapshot.getChildrenCount();
            double longt = dataSnapshot.child("longt").getValue(Double.class);
            double lat = dataSnapshot.child("lat").getValue(Double.class);
            String type = dataSnapshot.child("type").getValue(String.class);
            List<Model> list = new ArrayList<>();
            list.add(new Model());
            list.get(0).setLat(lat);
            list.get(0).setLongt(longt);
            cameras_info.onCallback();

        }

        @Override
        public void onChildChanged(DataSnapshot dataSnapshot, String s) {

        }

        @Override
        public void onChildRemoved(DataSnapshot dataSnapshot) {
        }

        @Override
        public void onChildMoved(DataSnapshot dataSnapshot, String s) {

        }

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

        }


    });

在此处输入图像描述

Assuming that pXGb...lGE2 is the UID of the logged-in user, to be able to read the data that corresponds to your schema, then please use the following lines of code:

String uid = FirebaseAuth.getInstance().getCurrentUser().getUid();
DatabaseReference db = FirebaseDatabase.getInstance().getReference();
DatabaseReference uidRef = db.child("ProgressReportss").child(uid);
uidRef.get().addOnCompleteListener(new OnCompleteListener<DataSnapshot>() {
    @Override
    public void onComplete(@NonNull Task<DataSnapshot> task) {
        if (task.isSuccessful()) {
            DataSnapshot snapshot = task.getResult();
            for (DataSnapshot ds : snapshot.getChildren()) {
                String content = ds.child("content").getValue(String.class);
                String timestamp = ds.child("timestamp").getValue(String.class);
                String title = ds.child("title").getValue(String.class);
                Log.d("TAG", content + "/" + timestamp + "/" + title);
            }
        } else {
            Log.d("TAG", task.getException().getMessage()); //Never ignore potential errors!
        }
    }
});

The result in the logcat will be:

Test 2/30-11-2018/Test 2
Test 3/30-11-2018/Test 3
Test 4/30-11-2018/Test 4

That's how you read it. If you need to send the data to another activity, inside the onComplete() use the logic in the following answer:

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM