Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to retrieve data from a Push firebase data

i used push to save data on firebase and it automatically saves with its own push id, now am trying to retrieve the data to use for history but i cant because of that id. this is what i have

 mDatabase = FirebaseDatabase.getInstance().getReference();
        mDatabase.child(Config.MESSAGE).child(userName).addListenerForSingleValueEvent(
                new ValueEventListener() {
                    @Override
                    public void onDataChange(DataSnapshot dataSnapshot) {
                        //Getting the data from snapshot
                        Message message = dataSnapshot.getValue(Message.class);



                        for (int i = 0; i < 11; i++) {

                            message.setMessage(message.getMessage() + i);
                            message.setTitle(message.getTitle() + i);
                            message.setTime(message.getTime() + i);

                            System.out.println("we in" + message.getTitle());
                            /******** Take Model Object in ArrayList **********/
                            CustomListViewValuesArr.add(message);

                            Resources res =getResources();

                            /**************** Create Custom Adapter *********/
                            adapter=new CustomAdapter(CustomListView, CustomListViewValuesArr,res);
                            list.setAdapter(adapter);
                        }
                    }

                        @Override
                        public void onCancelled(DatabaseError databaseError) {
                            //  Log.w(TAG, "getUser:onCancelled", databaseError.toException());
                            // ...
                        }

                    });
                }

and this is what the data looks like

-KRjrOKWwCa6zmhyyhvf
    body: "Hi how you doing"
    time: "test"
    title: "push"

-KRjjiiuwCa6zmhKVsro
    body: "Hi how you doing"
    time: "test"
    title: "push"

-KRjrOef45a6zmhKbytr
    body: "Hi how you doing"
    time: "test"
    title: "push"

i keep getting null everytime

like image 389
henry Avatar asked Oct 29 '22 20:10

henry


1 Answers

It looks like you have a list of messages for a user. But your code seems to treat the result as a single message. That will not work.

If this is indeed what's going on, try:

public void onDataChange(DataSnapshot dataSnapshot) {
    for (DataSnapshot messageSnapshot: dataSnapshot.getChildren()) {
        Message message = messageSnapshot.getValue(Message.class);

We recently added a sample about this to the Firebase documentation on querying data.

like image 91
Frank van Puffelen Avatar answered Nov 14 '22 13:11

Frank van Puffelen