{
"random_key 1" : {
"id": 0,
"text": "This is text"
},
"random_key 2" : {
"id": 1,
"text": "This is text"
}
}
If I'm storing my data like this, and I want to get the node where id is equal to 0. How can I do that?
The above is the child of issue, which is a child of root.
In your case you would have to setup a Query like this:
DatabaseReference reference = FirebaseDatabase.getInstance().getReference();
Query query = reference.child("issue").orderByChild("id").equalTo(0);
query.addListenerForSingleValueEvent(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
if (dataSnapshot.exists()) {
// dataSnapshot is the "issue" node with all children with id 0
for (DataSnapshot issue : dataSnapshot.getChildren()) {
// do something with the individual "issues"
}
}
}
#Override
public void onCancelled(DatabaseError databaseError) {
}
});
#Linxy's answer is correct but since you'll be reading a list of items from the database, it's better to use a child event listener instead of the value event listener.
DatabaseReference reference = FirebaseDatabase.getInstance().getReference();
Query query = reference.child("issue").orderByChild("id").equalTo(0);
query.addChildEventListener(new ChildEventListener() {
#Override
public void onChildAdded(DataSnapshot dataSnapshot, String s) {
//Do something with the individual node here`enter code here`
}
#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) {
}
});
This code works for me
Query query = mFirebaseDatabase.child("issue").orderByChild("id").equalTo(0)
query.addListenerForSingleValueEvent(new ValueEventListener() {
#Override
public void onDataChange(final DataSnapshot dataSnapshot) {
for (DataSnapshot data : dataSnapshot.getChildren()) {
//If email exists then toast shows else store the data on new key
if (!data.getValue(User.class).getEmail().equals(email)) {
mFirebaseDatabase.child(mFirebaseDatabase.push().getKey()).setValue(new User(name, email));
} else {
Toast.makeText(ChatListActivity.this, "E-mail already exists.", Toast.LENGTH_SHORT).show();
}
}
}
#Override
public void onCancelled(final DatabaseError databaseError) {
}
});
How I can use this SQL query in firebase?
SELECT column1, column2, ...
FROM table_name
WHERE condition1 OR condition2 OR condition3 ...;
For an easy-to-use cross platform integration of Firebase you can also have a look at V-Play Engine for mobile apps
FirebaseDatabase {
id: firebaseDb
Component.onCompleted: {
//use query parameter:
firebaseDb.getValue("public/bigqueryobject", {
orderByKey: true, //order by key before limiting
startAt: "c", //return only keys alphabetically after "c"
endAt: "m", //return only keys alphabetically before "m"
limitToFirst: 5, //return only first 5 sub-keys
})
}
}
Related
I am trying to search a "username" in firebase database but it always returns the else statement
mDatabaseref = FirebaseDatabase.getInstance().getReference("user_info");
mDatabaseref.addListenerForSingleValueEvent(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
if (dataSnapshot.child("username").child(usernamedatabasesend).exists())
{
Log.i("USERINFO","USER EXISTS");
}
else
{
Log.i("USERINFO","USER DOES NOT EXISTS");
}
}
#Override
public void onCancelled(DatabaseError databaseError) {
}
});
mDatabaseref = FirebaseDatabase.getInstance().getReference("user_info");
mDatabaseref.push().setValue(uic);
the usernamedatabasesend is the Edittext value to send it to the database to check if that same value the user is entering is existing on the db or not
The Database node is like this
"user_info" : {
"-L-7QPKXFyoN-GlPxTTN" : {
"email" : "",
"name" : "",
"password" : "",
"username" : "ujjwalbassi"
},
"-L-7QPMyzXCqpWT0YLPM" : {
"email" : "",
"name" : "",
"password" : "",
"username" : "ujjwalbassi"
}
}
****UPDATE*********
This is the new code
mDatabaseref.orderByChild("username").addListenerForSingleValueEvent(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
for(DataSnapshot dataSnapshot1: dataSnapshot.getChildren())
{
userInfo userinfoclass = dataSnapshot1.getValue(userInfo.class);
String usernamegotunamn = userinfoclass.getUsername().toString();
if(usernamegotunamn.equals(usernamedatabasesend))
{
Log.i("YESONO","USEREXISTS"+"\n"+usernamegotunamn+"\n"+usernamedatabasesend);
}
else {
mDatabaseref.push().setValue(uic);
Log.i("YESONO", "USERDOESNOTEXIST");
}
}
}
#Override
public void onCancelled(DatabaseError databaseError) {
}
});
The if else is working but if the "IF" is true then else works with it too. but it shows that if the user exists or not.
Try this.
mDatabaseref.orderByChild("username").equalTo("ujjwalbassi").addListenerForSingleValueEvent(
new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
//data will be available on dataSnapshot.getValue();
}
#Override
public void onCancelled(DatabaseError databaseError) {
Log.w(TAG, "getUser:onCancelled", databaseError.toException());
}
});
Reference : How to Search for data in Firebase Android
To check if the user exists by looping, you'll need to loop through each DataSnapshot and see if the username matches. To do this you need to, first, get a DataSnapshot of all the users, and then loop through each one:
mDatabaseref.addListenerForSingleValueEvent(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
for(DataSnapshot user : dataSnapshot){
if(user.child("username").getValue(String.class).equals(usernamedatabasesend)){
//The username matches!
Log.i("USERINFO","USER EXISTS");
return
}
}
}
#Override public void onCancelled(DatabaseError databaseError) {}
});
This isn't the best practice (Will get slow if you have a ton of users) but it is definitely a working solution. I recommend using #lovekush-vishwakarma's answer as a faster solution.
You cannot use exists() method to check whether a value exists or not. If you want to use exists() method, then you should consider change your database structure a little bit. Instead of having as a unique identifier the id that is generated by the push() method, just use the user name. Your database structure should look like this:
"user_info" : {
"ujjwalbassi" : {
"email" : "",
"name" : "",
"password" : "",
},
"anotherUserName" : {
"email" : "",
"name" : "",
"password" : "",
}
}
The important thing here is, when you add a new user to the database to check the userame for uniqueness. To verify if a user exists, you can use the following code:
DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
DatabaseReference userNameRef = rootRef.child("user_info").child(usernamedatabasesend);
ValueEventListener eventListener = new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
if(dataSnapshot.exists()) {
//do something
} else {
//do something else
}
}
#Override
public void onCancelled(DatabaseError databaseError) {}
};
userNameRef.addListenerForSingleValueEvent(eventListener);
Another approach will be to filter the database after usernamedatabasesend and get all the users with the same user name. This is not a good practice, to have users in your database which have the same user name. If you want to go with this, you can use orderByChild() method in a query like this:
DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
Query userNameQuery = rootRef.child("user_info").orderByChild("username").equalTo(usernamedatabasesend);
ValueEventListener eventListener = new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
if(dataSnapshot.exists()) {
//do something
} else {
//do something else
}
}
#Override
public void onCancelled(DatabaseError databaseError) {}
};
userNameQuery.addListenerForSingleValueEvent(eventListener);
If the user name contains a dot ., then you need to encode it in order to use it as a key in the database. To encode the user name please use the following mothod:
static String encodeUserName(String userName) {
return userName.replace(".", ",");
}
And to get it back, please use the following code:
static String decodeUserName(String userName) {
return userName.replace(",", ".");
}
I want to get particular value using from DataSnapshot.
I am attaching the screenshot here so kindly check and help me to get particular value from Realtime Database.
Actually I am implementing chat application in which I want to get value of user from group_list.
Here is my code.
private void loadTotalGroupList() {
referenceMainUrl = FirebaseDatabase.getInstance().getReferenceFromUrl("https://pure-coda-174710.firebaseio.com");
referenceGroupList = referenceMainUrl.child("group_list");
//Check if child is available or not.
referenceGroupList.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
if (dataSnapshot.exists()) {
Log.e("dataSnapshot"," ==>"+dataSnapshot);
Map<String, Object> newPost = (Map<String, Object>) dataSnapshot.getValue();
Log.e("newPost"," ==>"+newPost);
Log.e("user: ","==>" + newPost.get("user")); // Here I am getting null value
} else {
Log.e("Child not found", " >>>");
}
}
#Override
public void onCancelled(DatabaseError databaseError) {
}
});
}
and log showing like this. DataSnapshot { key = group_list, value = {First Group={-KtBH9gnTszNxcXjNu9A={message=assaasas, user=sakib}}} }
I have resolved my issue by using addChildEventListener
referenceGroupList.addChildEventListener(new ChildEventListener() {
#Override
public void onChildAdded(DataSnapshot dataSnapshot, String prevChildKey) {
Log.e("dataSnapshot KEY", " ==>" + dataSnapshot.getKey());
}
#Override
public void onChildChanged(DataSnapshot dataSnapshot, String prevChildKey) {
}
#Override
public void onChildRemoved(DataSnapshot dataSnapshot) {
}
#Override
public void onChildMoved(DataSnapshot dataSnapshot, String prevChildKey) {
}
#Override
public void onCancelled(DatabaseError databaseError) {
}
});
Here dataSnapshot.getKey() returns all sub child of it.
You're listening to the location /group_list in your code. That means a a snapshot from that location will contain the following:
/First Group
/-KtBH9gnTszNxcXjNu9A
message = "..."
user = "..."
If you want to get the user value from that location, you'll have to dig into it using each intermediate path:
dataSnapshot.child("First Group").child("-KtBH9gnTszNxcXjNu9A").child("user").getValue()
Or more simply:
dataSnapshot.child("First Group"/-KtBH9gnTszNxcXjNu9A/user").getValue()
You can't skip the middle paths in the snapshot. Alternatively, you may want to listen to a location closer to the value you want:
referenceMainUrl.child("group_list/First Group/-KtBH9gnTszNxcXjNu9A");
This code runs in a loop and gives you values for all the 'user's
DatabaseReference dRef = FirebaseDatabase.getInstance().getReference().child("group_list").child("First_Group");
dRef.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
for(DataSnapshot snapshot : dataSnapshot.getChildren()){
String userName = snapshot.child("user").getValue(String.class);
}
}
#Override
public void onCancelled(DatabaseError databaseError) {
}
});
I am using firebase auth ui in order to create users with mail and password but after the registration I forward them to a new activity in order to choose I username which needs to be unique. My idea was to search the whole DB in order to see if that username exists already or not. Here is how I am trying to do this but no luck till now:
submitBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Log.d("UserName", "Click: ");
DatabaseReference mDatabase = FirebaseDatabase.getInstance().getReference("USERS");
mDatabase.orderByChild("username").equalTo("name1").addListenerForSingleValueEvent(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
for (DataSnapshot snapshot: dataSnapshot.getChildren()) {
//Here you can get all data
Log.d("UserName", "onDataChange: "+snapshot.toString());
}
}
#Override
public void onCancelled(DatabaseError databaseError) {
}
});
}
});
I have also tried this one:
submitBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Log.d("UserName", "Click: ");
DatabaseReference mFirebaseDatabaseReference = FirebaseDatabase.getInstance().getReference();
Query query = mFirebaseDatabaseReference.child("USERS").orderByChild("username").equalTo("name1");
ValueEventListener valueEventListener = new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
for (DataSnapshot postSnapshot : dataSnapshot.getChildren()) {
//TODO get the data here
Log.d("UserName", "onDataChange: " + dataSnapshot.toString());
}
}
#Override
public void onCancelled(DatabaseError databaseError) {
}
};
query.addValueEventListener(valueEventListener);
}
});
The only reason your code is not working is because "username" is not a direct child of the USERS table.
Your direct children will have random ids , which you can iterate trough with ChildEventListener like this:
DatabaseReference mDatabase = FirebaseDatabase.getInstance().getReference("USERS");
mDatabase.addChildEventListener(new ChildEventListener() {
#Override
public void onChildAdded(DataSnapshot dataSnapshot, String previousChildName) {
User user = dataSnapshot.getValue(User.class); // pojo
if(user.getUsername().equals("name1") {
// do something
}
}
...
)
How do I search for users based on their usernames? I have looked at numerous SO posts on this matter but am still unable to achieve what I want to do.. I have tried to apply what I saw in those posts and is shown below:
DatabaseReference usersRef = FirebaseDatabase.getInstance().getReference("users");
usersRef.orderByChild("username")
.startAt(queryText)
.endAt(queryText+"\uf8ff");
usersRef.addListenerForSingleValueEvent(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot snapshot) {
searchList = new ArrayList<>();
for (DataSnapshot postSnapshot: snapshot.getChildren()) {
User user = postSnapshot.getValue(User.class);
Log.d("USER: ", "" + user.getUsername());
searchList.add(user);
}
adapter = new UserCardAdapter(getContext(), searchList);
recyclerView.setAdapter(adapter);
}
#Override
public void onCancelled(DatabaseError databaseError) {
Log.e("onQueryTextChange: " ,databaseError.getMessage());
}
});
However, all users are still retrieved. I have seen the usage of startAt() and endAt() supposedly work for others on other posts but I cannot manage to get it to work for me..
This is how the user data is stored:
User Data Structure
You almost done right but you should add addListenerForSingleValueEvent after the database reference that already apply orderBy() , startAt(), endAt() like this.
usersRef.orderByChild("username")
.startAt(queryText)
.endAt(queryText+"\uf8ff")
.addListenerForSingleValueEvent(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot snapshot) {
searchList = new ArrayList<>();
for (DataSnapshot postSnapshot: snapshot.getChildren()) {
User user = postSnapshot.getValue(User.class);
Log.d("USER: ", "" + user.getUsername());
searchList.add(user);
}
adapter = new UserCardAdapter(getContext(), searchList);
recyclerView.setAdapter(adapter);
}
#Override
public void onCancelled(DatabaseError databaseError) {
Log.e("onQueryTextChange: " ,databaseError.getMessage());
}
});
Your use of orderBy(), startAt(), and endAt() is correct according to the documentation.
But the addListener method must be applied directly to the object returned by the chain of orderByChild(), startAt(), and endAt() methods, and not in a new statement on the DatabaseReference retrieved with ... .getReference("users").
If you use a ChildEventListener:
DatabaseReference usersRef = FirebaseDatabase.getInstance().getReference("users");
usersRef.orderByChild("username")
.startAt(queryText)
.endAt(queryText+"\uf8ff");
.addChildEventListener(new ChildEventListener() {
List<User> searchList = new ArrayList<>();
#Override public void onChildAdded(DataSnapshot dataSnapshot, String s) {
User user = dataSnapshot.getValue(User.class);
Log.d("USER: ", "" + user.getUsername());
searchList.add(user);
}
#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) {}
});
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();