I have tried too many times but not works this code .How to update specific fields in Firebase database which is mentioned in structure
Here is my structure:
Blog
-LOkCTZQtuMIPT_c9ESK
desc: "wow"
id: "-LOkCTZQtuMIPT_c9ESK"
image:"firebase image"
title:"gh"
uid:"6757576gfgHh6"
So how i can update the desc,image,title these particular fields only with the help of id
Here is my code:
mCurrentUser = mAuth.getCurrentUser();
mDatabase = FirebaseDatabase.getInstance().getReference().child("Blog");
mDatabaseUser = FirebaseDatabase.getInstance().getReference().child("users").child(mCurrentUser.getUid());
private void startPosting() {
mProgress.setMessage("Posting...");
final String title_val = mPostTitle.getText().toString().trim();
final String desc_val = mNameFieldUpdate.getText().toString().trim();
if (!TextUtils.isEmpty(title_val) && !TextUtils.isEmpty(desc_val) && mImageUri != null) {
mProgress.show();
final StorageReference filepath = mStorage.child("Blog_Images").child(mImageUri.getLastPathSegment());
filepath.putFile(mImageUri).addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
@Override
public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
@SuppressWarnings("VisibleForTests")
final Uri downloadUri = taskSnapshot.getDownloadUrl();
final String id = mDatabase.getKey();
mDatabaseUser.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
mDatabase.child(id).child("title").setValue(title_val);
mDatabase.child(id).child("desc").setValue(desc_val);
mDatabase.child(id).child("image").setValue(downloadUri.toString());
mProgress.dismiss();
Intent mainIntent = new Intent(Update_Post.this, Main2Activity.class);
mainIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(mainIntent);
finish();
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});