John
John

Reputation: 65

How do i generate a unique id for a new node connected to a users id in firebase

Ive created an android application that saves data to firebase.Once users create accounts,they can edit their profiles and the following data reference is what im cuurently using to store the data in firebase:

myRef =FirebaseDatabase.getInstance().getReference().child("Users").child(firebaseAuth.getCurrentUser().getUid());

The data that users are prompted for in the user profile are:

username,summary,details,status,profession and a profile image

Using myref,im saving the values to firebase from a form

    final String username, userSummary,userStatus,userProfession,userDetail;

    myRef.child("username").setValue(username);
                    myRef.child("summary").setValue(userSummary);
                    myRef.child("status").setValue(userStatus);
                    myRef.child("details").setValue(userDetail);
                    myRef.child("profession").setValue(userProfession);

Inorder to save the userProfession in a new node in firebase. Ive managed to create and save profession in a new node as follows:Using a new reference(proffref) and saving the data to the profession node

proffRef = FirebaseDatabase.getInstance().getReference().child("Professions").child(firebaseAuth.getCurrentUser().getUid());

getting the values and saving them to database

proffRef.child("userid").setValue(firebaseAuth.getCurrentUser().getUid());
                    proffRef.child("proffid").setValue(imageUrl.toString());

But now what im trying to do is generate a unique id for each profession so that i can parse this as a separate list on the app.I want the new node(profession )to be linked to the userid so that when a user clicks on a category containing designers...then they get to see a list of all the designers.Also the list is being parsed in a tab fragment.I need to be able to click on a category and go to a list of items in an activity.But my main issue is,how to generate a unique id for profession?

Upvotes: 1

Views: 3739

Answers (2)

RileyManda
RileyManda

Reputation: 2651

You need to use push().

Assign a new id to your text element or whatever object you are trying to use in your app to represent/hold your id:

profid = findViewById(R.id.proffid );

Then assign this to your string:

proffessionid = profid.getText().toString().trim();

Then use push() to store and generate a unique id:

 proffRef.push().setValue(proffessionid);

For each entry under your proffRef,a new id will be generated.

You can also create a the node in such a way that the userid is directly attached to the new profession node as follows:

proffRef = FirebaseDatabase.getInstance().getReference().child("Professions").child(firebaseAuth.getCurrentUser().getUid());
  proffRef.child("userid").setValue(firebaseAuth.getCurrentUser().getUid()
                  proffRef.child("profession").setValue(userProfession);

Then use push to generate a unique key when user saves the form.

proffRef.push().setValue(proffessionid);

I am not sure wether you want to display this id or not.But if you dont want to display the id,i would suggest you declare proffID in your pojo class/modal class and then use GetProffID.Alternatively,attach the id to your textview then set its visibility to invisible. So in this instance,profid would be attached to your textview as follows:

<TextView
            android:id="@+id/proffid"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:visibility="invisible"
             />

For aslong as you use the same reference for the node you are creating,all data the user enters in the form will be saved to that specific root node(Professions).

Another way to do this is to bind the id directly to the EditText:

Define your variables:

EditText userProfessionEdit;

give your variable an id for your UI

 userProfessionEdit = findViewById(R.id.UserProfession);

Get the String:

userProfession = userProfessionEdit.getText().toString().trim();

Bind the String to your id,generate the unique id.......:

 String value = userProfessionEdit.getText().toString();
 String key = profid.getText().toString();
 DatabaseReference  childRef =  proffRef.child(key);
 childRef.push().setValue(value);

This will generate a unique id for each input without giving the id the child node of profid.

Upvotes: 1

Doug Stevenson
Doug Stevenson

Reputation: 317828

The push() method on DatabaseReference returns a new DatabaseReference with a unique key under the original reference. You can then set() whatever data you want on that reference to write more data at that new location.

If you don't want to use push() for some reason, you can always just generate a UUID using UUID.randomUUID(), then call toString() on that.

Upvotes: 1

Related Questions