Reputation: 109
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:
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.
Upvotes: 0
Views: 133
Reputation: 80914
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()
Upvotes: 2