Firebase retrieve information once user is logged in - java

I have the following database and need some help retrieving the name and address from the user which is logged in currently.
I wrote the following code but the information is not showing in the textview
public class UserProfileActivity extends AppCompatActivity {
DatabaseReference userReference = FirebaseDatabase.getInstance().getReference();
TextView userName;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_user_profile);
userName = (TextView) findViewById(R.id.userName);
ValueEventListener eventListener = new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
for (DataSnapshot ds : dataSnapshot.getChildren()) {
String name = ds.child("name").getValue(String.class);
userName.setText(name);
}
}
#Override
public void onCancelled(FirebaseError firebaseError) {
}
};
}
}
This is what is currently happening when implementing the ValueEventListener

try this:
FirebaseUser user=FirebaseAuth.getInstance().getCurrentUser();
String userid=user.getUid();
DatabaseReference reference = FirebaseDatabase.getInstance().getReference().child(userid);
reference.addListenerForSingleValueEvent(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
String address=dataSnapshot.child("address").getValue().toString();
String name=dataSnapshot.child("name").getValue().toString();
userName.setText(name);
}
#Override
public void onCancelled(DatabaseError databaseError) {
}
});
Since your datasnapshot is on the child(userid) which is direct child under root node then you don't need to iterate to be able to get the values of the current user.

Related

get the url values from Firebase Realtime database

I have the above format in firebase realtime database
I need to get data in arraylist
public class EmojiView extends AppCompatActivity {
FirebaseUser firebaseUser;
DatabaseReference reference;
ArrayList<String> images = new ArrayList<>();
private void getImagesfromDB() {
firebaseUser = FirebaseAuth.getInstance().getCurrentUser();
reference = FirebaseDatabase.getInstance().getReference("stickers");
reference.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(#NonNull DataSnapshot dataSnapshot) {
for (DataSnapshot snapshot : dataSnapshot.getChildren()){
String data = snapshot.getValue(String.class);
images.add(data);
}
}
#Override
public void onCancelled(#NonNull DatabaseError error) {
}
});
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_emoji_view);
getImagesfromDB();
}
}
I get the urls in data variable in OnDataChangemethod, but i am not able to assign those values to images arraylist
Try this:
Change your reference to this:
reference = FirebaseDatabase.getInstance().getReference("stickers").getChildren();
And then loop through the snapshots (which should be the children of "stickers") this way:
reference.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(#NonNull DataSnapshot snapshot) {
for (DataSnapshot item : snapshot){
stickers.add(item.getValue(String.class));
}
}
#Override
public void onCancelled(#NonNull DatabaseError error) {
}
});

Get the parent node of a node (Firebase)

i'm a beginner in android application, could anyone help me and show me how could i retrieve the generated id value node of the current user Id ? please find below my code. Thanks
FirebaseAuth firebaseAuth = FirebaseAuth.getInstance();
FirebaseDatabase mDatabase = FirebaseDatabase.getInstance();
DatabaseReference mDb = mDatabase.getReference();
FirebaseUser user = firebaseAuth.getCurrentUser();
final String userKey = user.getUid();
mDb.child("students").child(currentuser).getRef().addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
String reference = dataSnapshot.getRef().toString();
String userID = dataSnapshot.child("level").getValue(String.class);
textViewStudentName.setText(reference);
}
#Override
public void onCancelled(DatabaseError databaseError) {
}
});
Try below to get level:
DatabaseReference fireReference = FirebaseDatabase.getInstance().getReference("students");
fireReference.addListenerForSingleValueEvent(new ValueEventListener() {
#Override
public void onDataChange(#NonNull DataSnapshot dataSnapshot) {
for(DataSnapshot childSnapshot: dataSnapshot.getChildren()) {
if(childSnapshot.hasChild(userKey)) {
String userID = childSnapshot.child(userKey).child("level").getValue(String.class);
break;
}
}
}
#Override
public void onCancelled(#NonNull DatabaseError databaseError) {
}
});

Retrieve Multiple Child branches Firebase

Ok so I have made a CardView hold one database item(in this case a football player) and adapted it in a RecyclerView.
My Firebase Database looks like this:
Players:
|---Goalkeepers:
|--GoalkeeperName
|--(And some other data fields)
|---Defenders:
|---Midfielders:
|---Attackers
My database reference looks like this:
playersDatabase = FirebaseDatabase.getInstance().getReference("Players").child("Goalkeepers");
If I leave the reference as ("Players"), it will give me 4 empty fields representing the Goalkeeper, Defenders, Midfielders and Attackers branch.
How do I display all data from all Position branches such as Goalkeeper, Defenders, Midfielders and Attackers?
Also is there any way of displaying all the players in an organised way with headings in my layout so it looks like this?
Goalkeepers(heading)
Goalkeeper1
Goalkeeper2
Defenders (heading)
Defender1
Defender2
This is the code from the actual app:
public class PlayerList extends AppCompatActivity {
private RecyclerView mPlayerList;
DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
DatabaseReference playersRef = rootRef.child("Players");
#Override
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_squad_list);
//playersDatabase = FirebaseDatabase.getInstance().getReference("Players").child("Goalkeepers");
playersRef.keepSynced(true);
//new code
ValueEventListener valueEventListener = new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
for(DataSnapshot ds : dataSnapshot.getChildren()) {
String typeOfPlayer = ds.getKey();
Log.d("TAG", typeOfPlayer);
for(DataSnapshot dSnapshot : dataSnapshot.child(typeOfPlayer).getChildren()) {
String player = dSnapshot.getKey();
Log.d("TAG", " " + player);
}
}
}
#Override
public void onCancelled(DatabaseError databaseError) {}
};
playersRef.addListenerForSingleValueEvent(valueEventListener);
mPlayerList = (RecyclerView) findViewById(R.id.my_recycle_view);
mPlayerList.setHasFixedSize(true);
mPlayerList.setLayoutManager(new LinearLayoutManager(this));
}
#Override
protected void onStart() {
super.onStart();
FirebaseRecyclerAdapter<Player, PlayerViewHolder>firebaseRecyclerAdapter = new FirebaseRecyclerAdapter<Player, PlayerViewHolder>(Player.class, R.layout.player_row, PlayerViewHolder.class, playersRef) {
#Override
protected void populateViewHolder(PlayerViewHolder viewHolder, Player model, int position) {
viewHolder.setPlayerName(model.getPlayerName());
viewHolder.setPlayerSurname(model.getPlayerSurname());
viewHolder.setPlayerPosition(model.getPlayerPosition());
}
};
mPlayerList.setAdapter(firebaseRecyclerAdapter);
}
public static class PlayerViewHolder extends RecyclerView.ViewHolder{
View mView;
public PlayerViewHolder(View itemView){
super(itemView);
mView = itemView;
}
public void setPlayerName (String playerName){
TextView textViewName = (TextView) mView.findViewById(R.id.textViewName);
textViewName.setText(playerName);
}
public void setPlayerSurname (String playerSurname){
TextView textViewName = (TextView) mView.findViewById(R.id.textViewSurname);
textViewName.setText(playerSurname);
}
public void setPlayerPosition (String playerPosition){
TextView textViewName = (TextView) mView.findViewById(R.id.textViewPosition);
textViewName.setText(playerPosition);
}
}
}
Any help is appreciated. Thanks
To display those players accordingly to the following database structure:
Goalkeepers
Goalkeeper1
Goalkeeper2
Defenders
Defender1
Defender2
Please use the following code:
DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
DatabaseReference playersRef = rootRef.child("Players");
ValueEventListener valueEventListener = new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
for(DataSnapshot ds : dataSnapshot.getChildren()) {
String typeOfPlayer = ds.getKey();
Log.d("TAG", typeOfPlayer);
DatabaseReference playerRef = playersRef.child(typeOfPlayer);
ValueEventListener eventListener = new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
for(DataSnapshot dSnapshot : dataSnapshot.getChildren()) {
String player = dSnapshot.getKey();
Log.d("TAG", " " + player);
}
}
#Override
public void onCancelled(DatabaseError databaseError) {}
};
playerRef.addListenerForSingleValueEvent(eventListener);
}
}
#Override
public void onCancelled(DatabaseError databaseError) {}
};
playersRef.addListenerForSingleValueEvent(valueEventListener);
Edit:
According to your edited post, please use the following code:
DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
DatabaseReference playersRef = rootRef.child("Players");
ValueEventListener valueEventListener = new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
for(DataSnapshot ds : dataSnapshot.getChildren()) {
String typeOfPlayer = ds.getKey();
Log.d("TAG", typeOfPlayer);
for(DataSnapshot dSnapshot : dataSnapshot.child(typeOfPlayer).getChildren()) {
String player = dSnapshot.getKey();
Log.d("TAG", " " + player);
}
}
}
#Override
public void onCancelled(DatabaseError databaseError) {}
};
playersRef.addListenerForSingleValueEvent(valueEventListener);
Your result must be:
Attackers
Defenders
Goalkeepers
Goalkeeper1
Midfielders
Try this code ..
private void readData(){
DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
rootRef.child("Players").addListenerForSingleValueEvent(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
for(DataSnapshot ds : dataSnapshot.getChildren()) {
String typeOfPlayer = ds.getKey();
Log.d("TAG", typeOfPlayer);
for(DataSnapshot dSnapshot : dataSnapshot.child(typeOfPlayer).getChildren()) {
String player = dSnapshot.getKey();
Log.d("TAG", " " + player);
}
}
}
#Override
public void onCancelled(DatabaseError databaseError) {
}
});
}
and i hope add internet permission into manifest file..
more information refer this link
https://www.simplifiedcoding.net/firebase-realtime-database-crud/

How to Retrieve Data and display the user name from Firebase When pushed?

I'm trying to Retrieve and display the user name with this code but Every node have a unique Id.
Firebase MainFirebase;
private Button btnProfile;
private Button btnLibrary;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btnProfile = (Button) findViewById(R.id.btnProfile);
btnLibrary = (Button) findViewById(R.id.btnLibrary);
final TextView FullName = (TextView) findViewById(R.id.TVFullName);
Firebase.setAndroidContext(getApplicationContext());
MainFirebase = new Firebase("https://avastus-mcphil.firebaseio.com/Name");
MainFirebase.addValueEventListener(new com.firebase.client.ValueEventListener() {
#Override
public void onDataChange(com.firebase.client.DataSnapshot dataSnapshot) {
String myChildText = dataSnapshot.getValue(String.class);
FullName.setText(myChildText);
}
#Override
public void onCancelled(FirebaseError firebaseError) {
FullName.setText("Error Found!");
}
});
What is the best way to retrieve the pushed data?
this is what my Database look like:
-L4ampm6pwMeWA9X7A20
Email: "mcphil#haha.com"
Name: "Avastus"
Try this:
DatabaseReference ref=FirebaseDatabase.getInstance().getReference();
ref.addValueEventListener(new ValueEventListener(){
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
for(DataSnapshot datas: dataSnapshot.getChildren()){
String keys=datas.getKey();
String displayname=datas.child("Name").getValue().toString();
}
}
#Override
public void onCancelled(FirebaseError firebaseError) {
}
});
Assuming the push id is under the root node.
Added retrieving name assuming the name in your database is Name.

how to resolve data overriding when getting data from firebase database

I have this database:
In Activity A, I click on a class(listitem) and then pass the id of that class to this activity like this:-
so = getIntent().getStringExtra("send_id");
So right now I have the ClassId in this activity inside so. Then I run this query:
myref= FirebaseDatabase.getInstance().getReference().child("ClassStudent");
Query fk=myref.orderByChild("ClassId").equalTo(so);
fk.addChildEventListener(new ChildEventListener() {
#Override
public void onChildAdded(DataSnapshot dataSnapshot, String s) {
student=dataSnapshot.child("Studentid").getValue().toString();
//a breakpoint here gives student=oyvEbB0128Y90ADzXnL0UwCUy0Z2
}
#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(DatabaseError databaseError) {
}
}
So in the query I want all the students id that have that classid orderByChild("ClassId").equalTo(so)
Then I create a FirebaseRecycleradapter
FirebaseRecyclerAdapter<Info,ViewHolder> recyclerAdapter=new FirebaseRecyclerAdapter<Info,ViewHolder>(Info.class, R.layout.row, ViewHolder.class, fk) {
#Override
protected void populateViewHolder(final ViewHolder holder, Info model, int position) {
holder.title.setText(student); //a breakpoint here gives student = 7AnhQ7pRwhXDRqytjSUkoJrOB253
}
}
Now the data is appearing(on phone screen) like this:
Result
7AnhQ7pRwhXDRqytjSUkoJrOB253
7AnhQ7pRwhXDRqytjSUkoJrOB253
The above student id is appearing twice, instead it should be:
It should be
7AnhQ7pRwhXDRqytjSUkoJrOB253
oyvEbB0128Y90ADzXnL0UwCUy0Z2
Since both those students are in that class.
I have also overridden, getItemId and getItemViewType
To solve this, please use the following code:
DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
DatabaseReference classStudentRef = rootRef.child("ClassStudent");
ValueEventListener eventListener = new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
for(DataSnapshot ds : dataSnapshot.getChildren()) {
String Studentid = ds.child("Studentid").getValue(String.class);
Log.d("TAG", Studentid);
}
}
#Override
public void onCancelled(DatabaseError databaseError) {}
};
classStudentRef.addListenerForSingleValueEvent(eventListener);
The output will be:
7AnhQ7pRwhXDRqytjSUkoJrOB253
oyvEbB0128Y90ADzXnL0UwCUy0Z2
Edit: To get the name of the student of the coreponding stundet if you need to query your database twice. So, please use the following code:
DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
DatabaseReference classStudentRef = rootRef.child("ClassStudent");
ValueEventListener eventListener = new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
for(DataSnapshot ds : dataSnapshot.getChildren()) {
String Studentid = ds.child("Studentid").getValue(String.class);
Log.d("TAG", Studentid);
DatabaseReference studentIdRef = rootRef.child("Student").child(Studentid);
ValueEventListener valueEventListener = new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
String name = ds.child("name").getValue(String.class);
Log.d("TAG", name);
}
#Override
public void onCancelled(DatabaseError databaseError) {}
};
studentIdRef.addListenerForSingleValueEvent(valueEventListener);
}
}
#Override
public void onCancelled(DatabaseError databaseError) {}
};
classStudentRef.addListenerForSingleValueEvent(eventListener);

Categories