Issue
I am creating a simple app using android studio and firebase realtime database. When a user registered his data, it will be saved under Uid that firebase provides. Then i have created another node inside Uid called "PATH" and i want to store some data that user provides inside that. To do that i want to create specific id or something so that it won't be replaced every time that user enters a new data. (Check the screenshot)
I have tried to do this by using Datasnapshot but it doesn't respond to the click. This is what i tried.
add_new.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if( !Validate_text() ){
return;
}
else{
//Toast.makeText(getApplicationContext(), picker.getDayOfMonth() + "-" + picker.getMonth() + "-" + picker.getYear(), Toast.LENGTH_SHORT).show();
// redirect to path
String ach = milestone.getEditText().getText().toString().trim();
int day = picker.getDayOfMonth();
int month = picker.getMonth();
int year = picker.getYear();
String date = day + "/" + month + "/" + year ;
reference.child(uid).child("PATH").addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot snapshot) {
for( DataSnapshot random_key : snapshot.getChildren()) {
String key = random_key.getKey();
Map<String, Object> updates = new HashMap<String, Object>();
updates.put("Achievement : ", ach);
updates.put("Date", date);
reference.child(uid).child("PATH").child(key).updateChildren(updates).addOnSuccessListener(new OnSuccessListener<Void>() {
@Override
public void onSuccess(Void unused) {
Toast.makeText(getApplicationContext(), "Your achievement has been updated.", Toast.LENGTH_LONG).show();
finish();
startActivity(new Intent(AddPath.this, Path.class));
}
});
}
}
@Override
public void onCancelled(@NonNull DatabaseError error) {
Log.v(TAG, error.getMessage());
}
});
}
}
});
If anyone have idea what i have done wrong here or can help, you are very welcome.
Solution
I would prefer that you construct your database json something like this.
-users
-userUid
-name: "Ticherhaz"
-others: "Master"
-userUid2
-name: "Pyke"
-others: "Bronze"
-path
-userUid
-pathUid
-achievement: "Eat without using hand"
-date: "12/02/2022"
-pathUid2
-achievement: "Wake up early"
-date: "12/02/2022"
-userUid2
-pathUid3
-achievement: "Feeding team"
-date: "13/02/2022"
Since you say you want to create random Uid, you can use Firebase pushId
. You can refer here, Firebase Push ID.
add_new.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if( !Validate_text() ){
return;
}
else{
String ach = milestone.getEditText().getText().toString().trim();
int day = picker.getDayOfMonth();
int month = picker.getMonth();
int year = picker.getYear();
String date = day + "/" + month + "/" + year ;
Map<String, Object> updates = new HashMap<String, Object>();
updates.put("Achievement : ", ach);
updates.put("Date", date);
final String pushId = FirebaseDatabase.getInstance().getReference().push().getKey(); //Create new random Id
FirebaseDatabase.getInstance().getReference().child("path").child(uid).child(pushId).setValue(updates).addOnCompleteListener(new OnCompleteListener<Void>() {
@Override
public void onComplete(@NonNull Task<Void> task) {
Toast.makeText(getApplicationContext(), "Your achievement has been updated.", Toast.LENGTH_LONG).show();
finish();
startActivity(new Intent(AddPath.this, Path.class));
}
});
}
}
});
Answered By - Ticherhaz FreePalestine
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.