更新子项的值时生成的新节点

时间:2017-02-28 14:24:48

标签: node.js firebase firebase-realtime-database android-recyclerview

我正在使用Firebase创建一个类似Instagram的应用程序,我很难按照我的意愿进行配置文件设置活动。 Json树看起来像这样

{
  "Users" : {
    "1" : {
      "image" : "http://something.com/something",
      "name" : "person 1"
    },
    "2" : {
      "image" : "http://someone.com/someone",
      "name" : "person 2"
    },
    "d5nDSA5QyKU7j5XWMedYZVXTqBA2" : {
      "image" : "https://firebasestorage.googleapis.com/v0/b/parkir-ngasal.appspot.com/o/Profile_images%2Fcropped-1156087565.jpg?alt=media&token=28549773-a95d-4105-9287-e4e7e454113e",
      "name" : "Erik Adiguno"
    }
  },
  "posts" : {
    "post 1" : {
      "content" : "test content 1",
      "dPicture" : "http://something.com/something",
      "picture" : "http://postimage.com/postimage",
      "title" : "test title 1",
      "uid" : 1,
      "username" : "person 1"
    },
    "post 2" : {
      "content" : "test content 2",
      "dPicture" : "http://someone.com/someone",
      "picture" : "http://postimage2.com/postimage2",
      "title" : "test title 2",
      "uid" : 2,
      "username" : "person 2"
    }
  }
}

因此,配置文件设置活动只是将当前登录的用户和push()值标识为“图像”和“名称”子项......但正如您可能已经猜到的那样,它(当然)不会更新来自“posts”节点的“username”和“dPicture”子节点。请参阅以下代码

 private void startSetUpAccount() {
                final String name = mNameField.getText().toString().trim();
                final String user_id = mAuth.getCurrentUser().getUid();

                if(!TextUtils.isEmpty(name) && mImageUri !=null) {
                    mProgress.setMessage("Almost There...");
                    mProgress.show();
                    mProgress.setCancelable(false);
                    mProgress.setCanceledOnTouchOutside(false);
                    StorageReference filepath = mStorageImage.child(mImageUri.getLastPathSegment());
                    filepath.putFile(mImageUri).addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
                        @Override
                        public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {

                            final String downloadUri = taskSnapshot.getDownloadUrl().toString();
                            //data creation
                            mDatabaseUsers.child(user_id).child("name").setValue(name);
                            mDatabaseUsers.child(user_id).child("image").setValue(downloadUri);

                            mProgress.dismiss();

                            Toast.makeText(SetupActivity.this, "Successfully updated username and profile picture", Toast.LENGTH_LONG).show();

                            Intent mainActivityIntent = new Intent(SetupActivity.this, MainActivity.class);
                            mainActivityIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                            startActivity(mainActivityIntent);

                        }
                    });
                }

    }

我尝试了两种让我失望的方法:

  1. 我尝试通过“uid”孩子查询排序等于当前登录用户的ID ...然后我被卡住了因为我无法获得对孩子的引用

  2. 我尝试使用“name”和“dPicture”从“Users”节点而不是“posts”节点中检索值,但是你不能将两个节点放在recyclerview适配器中吗?

  3. 我基本上被困住了,任何帮助都会非常感激!

0 个答案:

没有答案