Retrive data from Firebase and output in a textview Code Answer

Hello Developer, Hope you guys are doing great. Today at Tutorial Guruji Official website, we are sharing the answer of Retrive data from Firebase and output in a textview without wasting too much if your time.

The question is published on by Tutorial Guruji team.

I need some help with my android Project. I try to make an Android forum (“student edition”) but my lack of knowledge catch up with me. I have the fallowing Firebase database structure: enter image description here

My Forum.class looks like this:

Button post_btn,fetch;
EditText message;
DatabaseReference forum1, comments;
TextView  post_update;

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

    post_btn = findViewById(R.id.post_btn);
    message = findViewById(R.id.comment_area);
    fetch=findViewById(R.id.btnFetch);
    post_update=findViewById(R.id.tvValue);
    final String post_title = "Student " + Session.LiveSession.user.getFn() + " " + Session.LiveSession.user.getSn() + " says: ";


    forum1 = FirebaseDatabase.getInstance().getReference("_forum1_");

    post_btn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
          ;
            Message comment = new Message( post_title,message.getText().toString());
            forum1.child(forum1.push().getKey()).setValue(comment);



        }
    });


    fetch.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            forum1.child("_forum1_").addListenerForSingleValueEvent(new ValueEventListener() {
                @Override
                public void onDataChange(DataSnapshot dataSnapshot) {
                    String value = dataSnapshot.getValue(String.class);
                    post_update.setText(value);
                }

                @Override
                public void onCancelled(DatabaseError databaseError) {
                }
            });
        }
    });

  }//end of onCreate
} //end of Class

pst_btn is working and when a student is posting, I push the data from “message” alongside with “post_title”(witch is constructed according to user) into the Firebase.

I will like some help with my “fetch” method. I try to make a button that extracts the data from firebase into a “textview”

My question is: How can I place following inside my textview: “post_title”+”message”??

Example of desired output:

Student Willy Weiss says: ssss

Student Willy Weiss says: qqq

Student Willy Weiss says: dfsfsfsf

Can some one help me with this? I have zero knowledge about Arrays or HashMaps, so if you recommend something like this , please leave some example code so I can learn/figure out what you recommend.

Thank you so much.

Answer

Try the following:

 fetch.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                forum1.child("_forum1_").addListenerForSingleValueEvent(new ValueEventListener() {
                    @Override
                    public void onDataChange(DataSnapshot dataSnapshot) {
                      for(DataSnapshot ds : dataSnapshot.getChildren()){
                        String msg = ds.child("message").getValue(String.class);
                        String value = ds.child("post_title").getValue(String.class);
                        post_update.setText(value +" "+ msg);
                     }
                    }

                    @Override
                    public void onCancelled(DatabaseError databaseError) {
                    }
                });
            }
        });

Iterate using dataSnapshot.getChildren() and then retrieve both the message and post_title and pass them to setText()

We are here to answer your question about Retrive data from Firebase and output in a textview - If you find the proper solution, please don't forgot to share this with your team members.

Related Posts

Tutorial Guruji