I am trying to get the value of channel_id from Firebase of the current user logged in. Below is the Image of the database:
I am getting null value from it from the code given below.
FirebaseDatabase database = FirebaseDatabase.getInstance();
DatabaseReference dbref =
FirebaseDatabase.getInstance().getReference("Users/Clients");
dbref.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(#NonNull DataSnapshot dataSnapshot) {
String val =String.valueOf(dataSnapshot.getValue());
Toast.makeText(getBaseContext(),""+val,Toast.LENGTH_LONG).show();
}
#Override
public void onCancelled(#NonNull DatabaseError databaseError) {
}
});
To get the current user information, try the following:
FirebaseDatabase database = FirebaseDatabase.getInstance();
FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser();
String userId=user.getUid();
DatabaseReference dbref = FirebaseDatabase.getInstance().getReference("Users/Clients").child(userId);
dbref.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(#NonNull DataSnapshot dataSnapshot) {
String names=dataSnapshot.child("name").getValue().toString();
}
#Override
public void onCancelled(#NonNull DatabaseError databaseError) {
}
});
Here you retrieve the current user id, then you add it to the path and retrieve the data.
Try this answer
FirebaseDatabase database = FirebaseDatabase.getInstance();
FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser();
String userId=user.getUid();
DatabaseReference dbref =
FirebaseDatabase.getInstance().getReference("Users/Clients").child(userId);
dbref.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(#NonNull DataSnapshot dataSnapshot) {
String channel_Id=dataSnapshot.child("channel_id").getValue().toString();
}
#Override
public void onCancelled(#NonNull DatabaseError databaseError) {
}
});
#Peter Haddad's answer will work great, there's also an alternative which is to attach the listener directly to the name property with:
FirebaseDatabase database = FirebaseDatabase.getInstance();
FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser();
String userId=user.getUid();
DatabaseReference dbref = FirebaseDatabase.getInstance().getReference("Users/Clients/"+userId+"/name");
dbref.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(#NonNull DataSnapshot dataSnapshot) {
String names=dataSnapshot.getValue(String.class);
}
#Override
public void onCancelled(#NonNull DatabaseError databaseError) {
}
});
This last example has the advantage that it only retrieves the name property and nothing else, even if you later add more child properties under userId. This method will save you bandwidth.
Related
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) {
}
});
I want to show data of all users from firebase without users id in firebase RecyclerView. How can it be possible? I am working on my first firebase project and I am new in Firebase. Here is my code but it shows just login user data.
java
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_dash_board);
recyclerView=findViewById(R.id.recycler);
mAuth=FirebaseAuth.getInstance();
FirebaseUser mUser=mAuth.getCurrentUser();
final String uId=mUser.getUid();
mDatabase= FirebaseDatabase.getInstance().getReference().child("UserName").child(uId);
mDatabase.keepSynced(true);
LinearLayoutManager linearLayoutManager=new LinearLayoutManager(this);
linearLayoutManager.setReverseLayout(true);
linearLayoutManager.setStackFromEnd(true);
recyclerView.setHasFixedSize(true);
recyclerView.setLayoutManager(linearLayoutManager);
}
#Override
protected void onStart() {
super.onStart();
FirebaseRecyclerAdapter<Data, DashBoard.MyViewHoler>adapter= new FirebaseRecyclerAdapter<Data, MyViewHoler>(Data.class, R.layout.item_data, DashBoard.MyViewHoler.class,mDatabase) {
#Override
protected void populateViewHolder(MyViewHoler myViewHoler, Data data, int i) {
myViewHoler.setName(data.getNamee());
myViewHoler.setCity(data.getCityy());
myViewHoler.setCell(data.getCelll());
myViewHoler.setFund(data.getFundd());
}
};
recyclerView.setAdapter(adapter);
}
firebase
To solve this, please use the following lines of code:
String uid = FirebaseAuth.getInstance().getCurrentUser().getUid();
DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
DatabaseReference uidRef = rootRef.child("UserName").child(uid);
ValueEventListener valueEventListener = new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
for(DataSnapshot ds : dataSnapshot.getChildren()) {
String name = ds.child("name").getValue(String.class);
Log.d(TAG, name);
}
}
#Override
public void onCancelled(#NonNull DatabaseError databaseError) {
Log.d(TAG, databaseError.getMessage()); //Don't ignore errors!
}
};
uidRef.addListenerForSingleValueEvent(valueEventListener);
The result in the logcat will be:
Azar khan
khan
...
If you want to use Firebase-UI library to display your element in a list, please see my answer from the following post:
How can I retrieve data from Firebase to my adapter
You should reference your database to UserName and then iterate through each user to get data. Check below:
DatabaseReference databaseReference = FirebaseDatabase.getInstance().getReference().child("UserName");
databaseReference.addListenerForSingleValueEvent(new ValueEventListener() {
#Override
public void onDataChange(#NonNull DataSnapshot dataSnapshot) {
for(DataSnapshot userSnapshot: dataSnapshot.getChildren()) {
for(DataSnapshot childSnapshot: userSnapshot.getChildren()) {
Data data = childSnapshot.getValue(Data.class);
}
}
}
#Override
public void onCancelled(#NonNull DatabaseError databaseError) {
}
});
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) {
}
});
I have been making a project using firebase for a delivery system app.
Problem: In this image, I need to get these two keys(or parent I think) and store them in a string or if possible I just want to know if I can check if the Id in user has a duplicate. How to check if these key has duplicate or exist and store it on a string if it does exist.
Updated code,
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_manageordergrocery);
mRecyclerView2= findViewById(R.id.recyclerview1);
mRecyclerView2.setHasFixedSize(true);
mRecyclerView2.setLayoutManager(new LinearLayoutManager(this));
mModel2 = new ArrayList<>();
mAuth = FirebaseAuth.getInstance();
FirebaseUser user = mAuth.getCurrentUser();
mAdapter2=new ViewHolder1(Manageordergrocery.this, mModel2);
mRecyclerView2.setAdapter(mAdapter2);
mDatabaseReference2= FirebaseDatabase.getInstance().getReference("Users");
mDatabaseReference2.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(#NonNull DataSnapshot dataSnapshot) {
for(DataSnapshot postSnapshot:dataSnapshot.getChildren())
{
String myParentNode = postSnapshot.getKey();
mDatabaseReference2 = FirebaseDatabase.getInstance().getReference();
mDatabaseReference2.orderByKey().equalTo(myParentNode).addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(#NonNull DataSnapshot dataSnapshot) {
String saveNode = dataSnapshot.getKey();
mDatabaseReference2 = FirebaseDatabase.getInstance().getReference(saveNode).child("Ordered");
mDatabaseReference2.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(#NonNull DataSnapshot dataSnapshot) {
for(DataSnapshot postSnapshot:dataSnapshot.getChildren()) {
Model1 model2 = postSnapshot.getValue(Model1.class);
mModel2.add(model2);
}
mAdapter2.notifyDataSetChanged();
}
#Override
public void onCancelled(#NonNull DatabaseError databaseError) {
}
});
}
#Override
public void onCancelled(#NonNull DatabaseError databaseError) {
}
});
}
}
#Override
public void onCancelled(#NonNull DatabaseError databaseError) {
Toast.makeText(Manageordergrocery.this, databaseError.getMessage(), Toast.LENGTH_LONG).show();
}
});
}
Can someone help me, please? I'm still new in firebase and don't know how to do it.
To obtain the two keys you can do the following:
mDatabaseReference2 = FirebaseDatabase.getInstance().getReference("User");
mDatabaseReference2.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(#NonNull DataSnapshot dataSnapshot) {
for(DataSnapshot postSnapshot:dataSnapshot.getChildren())
{
String myParentNode = postSnapshot.getKey();
mDatabaseReference = FirebaseDatabase.getInstance().getReference();
mDatabaseReference.orderByKey().equalTo(myParentNode).addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(#NonNull DataSnapshot dataSnapshot) {
for(DataSnapshot posts :dataSnapshot.getChildren())
{
String key = posts.getKey();
if(key.equals(myParentNode))
Toast.makeText(Manageordergrocery.this,"equals", Toast.LENGTH_LONG).show();
}
}
#Override
public void onCancelled(#NonNull DatabaseError databaseError) {}
});
}
}
#Override
public void onCancelled(#NonNull DatabaseError databaseError) {
Toast.makeText(Manageordergrocery.this, databaseError.getMessage(), Toast.LENGTH_LONG).show();
}
});
Here first you add a reference to node User then you loop inside that node and retrieve the key using getKey(). After that you attach another listener to the root location and using orderByKey().equalTo(myParentNode) you can check if you have two keys with the same value and do the required.
I have a FragmenPageAdpater class where I am trying to get info about the current user so that I can load the right fragment for this specific user.
The ValueEventListener never gets triggered and it keeps skipping the body part.
public SessionListAdapter(FragmentManager fragmentManager) {
super(fragmentManager);
mAuth = FirebaseAuth.getInstance();
FirebaseDatabase myData = FirebaseDatabase.getInstance();
final DatabaseReference dataUser = myData.getReference().child("Users").child(mAuth.getCurrentUser().getUid());
dataUser.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
if(dataSnapshot.child("type").getValue(String.class).equals("Tutor")) //if(readType.equals("Tutor")) if user is a Tutor
user = dataSnapshot.getValue(Tutor.class);
else
user = dataSnapshot.getValue(Student.class);
}
#Override
public void onCancelled(#NonNull DatabaseError databaseError) {
}
});
}
Here is the necessary portion of my database structure.
You need to use addListenerForSingleValueEvent like the following. I am referring to this Firebase documentation.
dataUser.addListenerForSingleValueEvent(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
if(dataSnapshot.child("type").getValue(String.class).equals("Tutor"))
user = dataSnapshot.getValue(Tutor.class);
else
user = dataSnapshot.getValue(Student.class);
}
#Override
public void onCancelled(DatabaseError databaseError) {}
});
Hope that helps!