Connecting two entities in Firebase in Firebaserecycleadapter - android

I have a Cardview which is working already. Here is the code:
#Override
public void onStart() {
super.onStart();
FirebaseRecyclerAdapter<Model, ViewHolder> firebaseRecyclerAdapter =
new FirebaseRecyclerAdapter<Model, ViewHolder>(Model.class,R.layout.row,ViewHolder.class,mref)
{
#Override
protected void populateViewHolder(ViewHolder viewHolder, Model model, int position) {
String userid = model.getUserid().toString();
viewHolder.setDetails(getActivity().getApplicationContext(),model.getPetname(),"Reason:" + model.getReason(),model.getPetpicture(),"Breed:" + model.getBreed(),
"Age: " + model.getAge(),"Size: " + model.getPetsize() +"yrs old","Color:" + model.getPetcolor(),model.getUserid(),model.datepost);
}
};
recyclerView.setAdapter(firebaseRecyclerAdapter);
}
That model.getUserid() get the user Id of the user who posted that it display UID given by Firebase. Now what I want is to convert that UID to name which I have from other Entity User:
enter image description here
enter image description here
So in short I want to populate my Cardview but instead of Userid I want to convert it to name.

what you can do is that after retrieving that userId, you fetch the name using a value event listener as follows
DatabaseReference userRef = FirebaseDatabase.getInstance().getReference("User");
String userId = "the_user_id";
userRef.child(userId).addListenerForSingleValueEvent(new ValueEventListener() {
#Override
public void onDataChange(#NonNull DataSnapshot dataSnapshot) {
if(dataSnapshot.exists())
{
String name = dataSnapshot.getValue(User.class).getName();
}
}
#Override
public void onCancelled(#NonNull DatabaseError databaseError) {
}
});`

Related

Retrieving data from particular nodes firebase Database

I am developing an android application and now I am stuck in retrieving data from particular nodes like I want to retrieve only one value from each nodes. The database structure shows below.
How can I retrieve the second unique id that created by Firebase database?
First create a POJO class to get the values you want, it should be writen the same way as you have them in Firebase.
public class AppointmentsPojo {
private String appointmentStuts;
public AppointmentsPojo(){
}
public String getAppointmentStuts() {
return appointmentStuts;
}
public void setAppointmentStuts(String appointmentStuts) {
this.appointmentStuts = appointmentStuts;
}
}
Then just loop inside Appointments to get each appointmentStuts
mDatabase.child("Appointments").addListenerForSingleValueEvent(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
//Looping inside Appointments to get each appointmentsStuts
for(DataSnapshot snapshot: dataSnapshot.getChildren()){
AppointmentsPojo ap = snapshot.getValue(AppointmentsPojo.class);
//Getting each appointmentStuts
String appointmentStuts = ap.getAppointmentStuts();
//To get each father of those appointments
String key = snapshot.getKey();
Log.e("Data: " , "" + appointmentStuts );
}
}
#Override
public void onCancelled(DatabaseError databaseError) {
System.out.println("The read failed: " + databaseError.getCode());
}
});
Where mDatabase is
DatabaseReference mDatabase;
mDatabase = FirebaseDatabase.getInstance().getReference();
To get the value of appointmentStuts only from the first object, please use the following code:
DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
Query query = rootRef.child("Appointments").child(PostKey).limitToFirst(1);
ValueEventListener valueEventListener = new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
for(DataSnapshot ds : dataSnapshot.getChildren()) {
String appointmentStuts = ds.child("appointmentStuts").getValue(String.class);
Log.d(TAG, appointmentStuts);
}
}
#Override
public void onCancelled(#NonNull DatabaseError databaseError) {
Log.d(TAG, databaseError.getMessage());
}
};
query.addListenerForSingleValueEvent(valueEventListener);

How get the list of all data from firebase query when passing query in Firebase Recycler Adapter

I have a firebase query. I have added "addValueEventListener" to it. "onDataChange" in "dataSnapshot", I see all 3 the child data which should be fetch by the query.
Now, I am passing this query reference in Firebase Recycler Adapter to create recycler view but I see only one data on the view not all 3childs data.
// This is how I am creating the query reference.
currentUser = firebaseAuth.getCurrentUser();
mDatabase = FirebaseDatabase.getInstance().getReference();
ordersRef = mDatabase.child("orders");
ordersQuery = ordersRef.orderByChild("userId").equalTo(currentUser.getUid().toString());
ordersQuery.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
isOrdersEmpty = ! dataSnapshot.hasChildren();
Log.i("ORDERS: ", "You have orders: " + isOrdersEmpty + dataSnapshot);
for (DataSnapshot snapshot : dataSnapshot.getChildren()) {
Log.i("ORDER: ", "Your ORDER : " + snapshot);
}
}
#Override
public void onCancelled(DatabaseError databaseError) {
}
});
After this, I pass this query reference in Firebase Recycler Adapter
FirebaseRecyclerAdapter<Orders, OrdersActivity.OrdersViewHolder> FBRA = new FirebaseRecyclerAdapter<Orders, OrdersViewHolder>(
Orders.class,
R.layout.orders_list,
OrdersActivity.OrdersViewHolder.class,
ordersQuery
) {
#Override
protected void populateViewHolder(OrdersViewHolder viewHolder, Orders model, int position) {
viewHolder.setOrderId(model.getOrderId());
viewHolder.setTxnAmount(model.getTxnAmount());
viewHolder.setTxnId(model.getTxnId());
final String orderId = getRef(position).getKey().toString();
viewHolder.mView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent orderDetailActivity = new Intent(OrdersActivity.this, OrderDetailActivity.class);
orderDetailActivity.putExtra("orderId", orderId);
startActivity(orderDetailActivity);
}
});
}
};
mOrdersRecyclerView.setAdapter(FBRA);
When I see the view in the app, It only shows one data while it should show 3 data.

How to get a specific item inside the key using android

How to only retrieve email addresses inside the key, not all the items. I have an items Name and Email, but I only want to retrieve EMAIL.
example:
Email: HOTMAIL
Email: GMAIL
first make database reference as
mReference= FirebaseDatabase.getInstance().getReferenceFromUrl(FIREBASE_URL_USERS);
here FREBASE_URL_USERS is a url of users key you can get it from firebase console.
it is a combination of your unique_fiirebase_root_url / User01
then get snapshot of data as
mDatabseReference.addListenerForSingleValueEvent(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
dataSnapshot.getValue("Email");
// use log to check whetehr you are getting email or not
#Override
public void onCancelled(DatabaseError databaseError) {
// log error and do something
}
});
Assuming that the user emails are unique, you can query the users records by its email value as follows:
final String email = "hani.mehdi#email.com";
ValueEventListener onFindListener = new ValueEventListener() {
#Override
public void onCancelled(DatabaseError error) {
Log.e(TAG, error.getMessage());
}
#Override
public void onDataChange(DataSnapshot snapshot) {
Map<String, User> result = snapshot.getValue(Map.class);
if (result.contains(email) {
User user = result.get(email);
Log.i(TAG, user.toString());
} else {
Log.d(TAG, "Not found: " + email);
}
}
};
DatabaseReference userReference = database.getReference("users")
.orderByChild("email")
.equalTo(email)
.addListenerForSingleValueEvent(onFindListener);

How to fetch all data after adding new data in firebase database

This is my code for adding person object in firebase.
FirebaseDatabase database = FirebaseDatabase.getInstance();
DatabaseReference myRef = database.getReference("Person");
buttonSave.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
String name = editTextName.getText().toString().trim();
String address = editTextAddress.getText().toString().trim();
//Creating Person object
Person person = new Person();
person.setName(name);
person.setAddress(address);
new Firebase(Config.FIREBASE_URL).child("Person").push().setValue(person);
}
});
myRef.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
for (DataSnapshot postSnapshot : dataSnapshot.getChildren()) {
//Getting the data from snapshot
Person person = postSnapshot.getValue(Person.class);
//Adding it to a string
String string = "Name: " + person.getName() + "\nAddress: " + person.getAddress() + "\n\n";
//Displaying it on textview
textViewPersons.setText(string);
}
}
#Override
public void onCancelled(DatabaseError databaseError) {
System.out.println("The read failed: " + databaseError.getMessage());
}
});
Adding data is working fine and every add creating a new key for person.
But addValueEventListener is showing me only last added entry. I want all the entries data.
Can anyone help me on this?
Every time anything changes in the Person node, you get notified through onDataChange(). In there you loop over the people and then for each call:
textViewPersons.setText(string);
So you're constantly replacing the contents of the text view with the information from each person. After each loop it will end up showing just the information for the last user. You can easily see that the code loops through all people by adding:
System.out.println(string);
With this you'll see each person in logcat, but only the last person in the text view.
One solution is to either use a ListView or RecyclerView, both of which handle lists of items. Another (quicker to implement) way to show all users it to append the strings in the text view:
textViewPersons.setText(textViewPersons.getText()+string+"\n");
Try this if it helps:
String dataviewer=new String();
Firebase firebase=new Firebase(Config.FIREBASE_URL);
buttonSave.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
String name = editTextName.getText().toString().trim();
String address = editTextAddress.getText().toString().trim();
//Creating Person object
Person person = new Person();
person.setName(name);
person.setAddress(address);
new Firebase(Config.FIREBASE_URL).child("Person").push().setValue(person);
}
});
firebase.child("Person").addChildEventListener(new ChildEventListener() {
// Retrieve values as they are added to Firebase
#Override
public void onChildAdded(DataSnapshot snapshot, String previousChildKey) {
Map<String,Object> vals=(Map<String,Object>)snapshot.getValue();
String name_person=String.valueOf(vals.get("name"));
String address_person=String.valueOf(vals.get("address"));
dataviewer+="Name: " + name_person + "\nAddress: " + address_person + "\n\n";
}
#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(FirebaseError firebaseError) {
}
});

Get the pushed ID for specific value in firebase android

I want to retrive the id that generated by firebase when I pushed value to it like next
I want to retrieve "-KGdKiPSODz7JXzlgl9J" this id for that email
I tried by getKey() but it return "users"
and when user get value it return the whole object from the id to profile picture and that won't make me get it as User object in my app
how solve this ?
Firebase users = myFirebaseRef.child("users");
users.orderByChild("email").equalTo("z#m.com").addListenerForSingleValueEvent(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
dataSnapshot.getKey();
Log.d("User",dataSnapshot.getRef().toString());
Log.d("User",dataSnapshot.getValue().toString());
}
#Override
public void onCancelled(FirebaseError firebaseError) {
Log.d("User",firebaseError.getMessage() );
}
});
You can read the key from push() without pushing the values. Later you can create a child with that key and push the values for that key.
// read the index key
String mGroupId = mGroupRef.push().getKey();
....
....
// create a child with index value
mGroupRef.child(mGroupId).setValue(new ChatGroup());
mGroupId contains the key which is used to index the value you're about to save.
UPDATE 1:
it can obtain also by one line
String key = mDatabase.child("posts").push().getKey();
//**************************************************************//
after searching and trying a lot of things i came to 2 ways to do that
.
1. first one to get the key when i upload the post to the server via this function
public void uploadPostToFirebase(Post post) {
DatabaseReference mFirebase = mFirebaseObject
.getReference(Constants.ACTIVE_POSTS_KEY)
.child(post.type);
mFirebase.push().setValue(post);
Log.d("Post Key" , mFirebase.getKey());
}
i used it in my code to get the key after i have already pushed it to node for it in my database
public void getUserKey(String email) {
Query queryRef = databaseRef.child(Constants.USERS_KEY)
.orderByChild(Constants.USERS_EMAIL)
.equalTo(email);
queryRef.addChildEventListener(new ChildEventListener() {
#Override
public void onChildAdded(DataSnapshot dataSnapshot, String s) {
//TODO auto generated
}
#Override
public void onChildChanged(DataSnapshot dataSnapshot, String s) {
//TODO auto generated;
}
#Override
public void onChildRemoved(DataSnapshot dataSnapshot) {
//TODO auto generated;
}
#Override
public void onChildMoved(DataSnapshot dataSnapshot, String s) {
//TODO auto generated
}
#Override
public void onCancelled(DatabaseError databaseError) {
//TODO auto generated
}
});
}
When you fire a Firebase query there can potentially be multiple results. So when you ask for the value of a query, Firebase returns a list of items. Even if there is only one matching item, it will be a list of one item.
So you will have to handle this list in your code:
users.orderByChild("email").equalTo("z#m.com").addListenerForSingleValueEvent(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
for (DataSnapshot child: dataSnapshot.getChildren()) {
Log.d("User key", child.getKey());
Log.d("User ref", child.getRef().toString());
Log.d("User val", child.getValue().toString());
}
}
In Java - Android Studio, you can get the unique pushed ID as the item is written to the db...
Per "Firebase's: Save Data on Android": You can use the reference to the new data returned by the push() method to get the value of the child's auto-generated key or set data for the child. Calling getKey() on a push() reference returns the value of the auto-generated key.
To get the reference at write time, instead of loading DATA with a single push()...
use push() to create a blank record in the database, return value is the record's reference.
use .getKey() to get the Key for that record.
use .setValue(DATA) to fill in the blank record
here's an example:
FirebaseDatabase fb_db_instance = FirebaseDatabase.getInstance();
DatabaseReference db_ref_Main = fb_db_instance.getReference("string_db_Branch_Name");
hashMap_record = new HashMap<String, String>(); //some random data
hashMap_record.put("key_Item1", "string_Item1");
hashMap_record.put("key_Item2", "string_Item2");
DatabaseReference blankRecordReference = db_ref_Main ;
DatabaseReference db_ref = blankRecordReference.push(); //creates blank record in db
String str_NEW_Records_Key = db_ref.getKey(); //the UniqueID/key you seek
db_ref.setValue( hashMap_record); //sets the record
I solved it to get id you need to use firebase url
String uid = firebaseRef.child("users").push().getKey();
Log.i("uid", uid);
this will give you the key KG.....
For those using TVAC Tutorials(https://www.youtube.com/watch?v=cSMMWOHkP68&list=PLGCjwl1RrtcTXrWuRTa59RyRmQ4OedWrt&index=16)
You can get Key using onClick method as follows:
viewHolder.mView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
String key = getRef(position).getKey();
}
});
Just add new field to Users like userID, when you create new User add uid and than you can receive it by reading query
I'm doing it like this.
String userID;// in Users
Firebase users = myFirebaseRef.child("users");
users.orderByChild("email").equalTo("z#m.com").addListenerForSingleValueEvent(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
Users user= dataSnapshot.getChildren().iterator().next().getValue(Users.class);
Log.d("User",user.getUserID());
}
#Override
public void onCancelled(FirebaseError firebaseError) {
Log.d("User",firebaseError.getMessage() );
}
});
I had faced this problem and I found the solution,
you can get it using this code:
dataSnapshot.getChildren().iterator().next().getKey()
// Unity, code in C#
{
reference = FirebaseDatabase.DefaultInstance.RootReference;
string s = reference.Push().Key;
reference.Child(s).Child(Username).SetValueAsync(Username);
Debug.Log(s);
}
Another Option to get the unique Post-id from firebase is by getting key (dataSnapshot.getKey()) in #Override method public void onChildAdded and maintaining it locally for example
private void attachDatabaseReadListener() {
if (mTaskEventListener == null) {
mTaskEventListener = new ChildEventListener() {
#Override
public void onChildAdded(DataSnapshot dataSnapshot, String s) {
Task friendlyMessage = dataSnapshot.getValue(Task.class);
friendlyMessage.setId(dataSnapshot.getKey());
System.out.println("friendlyMessage = " + friendlyMessage);
DummyContent.ITEMS.add(new DummyContent.DummyItem("" + DummyContent.ITEMS.size()+1,friendlyMessage.getStatus(),friendlyMessage.getSummary()));
}
public void onChildChanged(DataSnapshot dataSnapshot, String s) {}
public void onChildRemoved(DataSnapshot dataSnapshot) {}
public void onChildMoved(DataSnapshot dataSnapshot, String s) {}
public void onCancelled(DatabaseError databaseError) {}
};
mUserDatabaseReference.addChildEventListener(mTaskEventListener); // remove it
}
}
dataSnapshot.getKey will set the unique post id in Task Instance and can be used later to perform any update operation.
FirebaseDatabase firebaseDatabase = FirebaseDatabase.getInstance();
DatabaseReference databaseReference1 = firebaseDatabase.getReference("users");
databaseReference1.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(#NonNull DataSnapshot dataSnapshot) {
for(DataSnapshot dataSnapshot1 : dataSnapshot.getChildren()) {
String key = dataSnapshot1.getKey();
Log.d(TAG, "onCreate: key :" + key);
String email = dataSnapshot1.child("email").getValue(String.class);
String roomno =dataSnapshot1.child("address").getValue(String.class);
Log.d(TAG, "onDataChange: email: " + email);
Log.d(TAG, "onDataChange: address: " + address)
}
}
#Override
public void onCancelled(#NonNull DatabaseError databaseError) {
}
});
Use this
mFirebaseDatabase=mFirebaseDatabase.getInstance().getReference("tablename");
Query query = mFirebaseDatabase.orderByChild("tablenme").getRef();
query.orderByChild("Id").addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(#NonNull DataSnapshot dataSnapshot) {
for (DataSnapshot dataSnapshot1 : dataSnapshot.getChildren()) {
String id =dataSnapshot1.child("Id").getKey();
String Name = dataSnapshot1.child("Name").getValue().toString();
String lastName= dataSnapshot1.child("lastname").getValue().toString();
flatDataGets.add(Name+"-"+lastname);
}
ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(RagisterActivity.this, R.layout.support_simple_spinner_dropdown_item, DataGets);
arrayAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
mRegisterSpinner.setAdapter(arrayAdapter);
mRegisterSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
flatName =DataGets.get(position);
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
}
#Override
public void onCancelled(#NonNull DatabaseError databaseError) {
}
});
Yes, you can retrieve the node or main child name by adding its name in that particular child as a key and value {main_child_name}.
Simply do similar:
HashMap<String, Object> hashLcl = new HashMap<>();
hashLcl.put("admin", firebaseUser.getUid());
hashLcl.put("name", textPrm);
DatabaseReference referenceLcl = FirebaseDatabase.getInstance().getReference();
String keyLcl = referenceLcl.child("GroupChats").push().getKey();
hashLcl.put("key", keyLcl);
Task task = referenceLcl.child("GroupChats").child(keyLcl).setValue(hashLcl);
task.addOnSuccessListener(aVoid -> {
//the data is added and now we are sure to do something related
});
This is the result:
String id = Objects.requireNonNull(task.getResult().getUser()).getUid();

Categories

Resources