0

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) {

                    }
                });
Manik
  • 153
  • 4
  • 15
  • is the photo being uploaded? – cutiko Dec 19 '17 at 20:46
  • yep everything is uploading but it creating new post now overwriting the post – Manik Dec 19 '17 at 20:50
  • Is because you are getting the key from the blog, instaed of getting the key from the blog child – cutiko Dec 19 '17 at 20:53
  • Hey cutiko can you tell how to get key from the blog child in this code – Manik Dec 19 '17 at 20:56
  • ok @PeterHaddad i am trying to implement this – Manik Dec 19 '17 at 21:08
  • Usually you can get it because it was previously seleceted like clicked from a list but if there is some practice code check this https://stackoverflow.com/questions/32886546/how-to-get-all-child-list-from-firebase-android – cutiko Dec 20 '17 at 02:36

2 Answers2

1

Do this:

DatabaseReference ref = FirebaseDatabase.getInstance().getReference().child("Blog");    
ref.addListenerForSingleValueEvent(new ValueEventListener(){
   @Override
  public void onDataChange(DataSnapshot dataSnapshot){
       for(DataSnapshot data: dataSnapshot.getChildren()){
            String uid=data.child("uid").getValue().toString();
            if(uid.equals((mCurrentUser.getUid()){
            String keyid=data.getKey();
            ref.child(keyid).child("title").setValue(newtitle);
            ref.child(keyid).child("image").setValue(newurl);
            ref.child(keyid).child("desc").setValue(newdesc);
           }
     }
   }

Have the location of the listener at child("Blog") and then iterate inside of it and get the key which is keyid. Then to update the values simple point it to the right location and update each one.

Peter Haddad
  • 78,874
  • 25
  • 140
  • 134
  • hey Peter i used email auth for posting data by auth users so i think this line is required mDatabaseUser = FirebaseDatabase.getInstance().getReference().child("users").child(mCurrentUser.getUid()); – Manik Dec 19 '17 at 21:12
  • not accepting directly keyid in this code mDatabaseUser = FirebaseDatabase.getInstance().getReference().child("Blog").child(key_id_here); – Manik Dec 19 '17 at 21:16
  • ya i know i used id here instead of key_id_here as mentioned but not solving the issue – Manik Dec 19 '17 at 21:18
  • hey it effect all the post in the blog structure . All changes by updating one post i think due to mAuth may be – Manik Dec 19 '17 at 21:42
  • but all post are from different users so there be not change all the post can tell how to fix this – Manik Dec 19 '17 at 21:45
  • Sorry Peter i will not do this again id is mentioned above String id = data.getKey(); I swear follow your rules but it is changing all the data in a structure – Manik Dec 19 '17 at 21:48
  • Sorry Peter i am irritating you but I am new on Firebase I meant to say that if you make a update on particular post (image,title,desc) and rest other post by users seems to be same not that particular selected post only this issue happening – Manik Dec 19 '17 at 22:08
  • yep Peter Sir Current user and that single one post by the user i.e. why i told maybe mAuth will work here but don't know how – Manik Dec 19 '17 at 22:12
0

You're using getKey() on your base blog DB reference (/Blog). You need to create a new node within that reference with push(), and then getKey() on your newly created child.

final String id = mDatabase.push().getKey();

urgentx
  • 3,832
  • 2
  • 19
  • 30