Firebase 实时数据库更新(添加另一个对象)

时间:2020-12-28 20:30:01

标签: android firebase firebase-realtime-database

我有代码

public class CowDet extends AppCompatActivity {
DatabaseReference databaseReference;


private EditText postTitleEdit;
private EditText postTextEdit;
private TextView key1;
private Button editButton;
private Button deleteButton;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_cow_det);

    postTextEdit = findViewById(R.id.postTextEdit);
    postTitleEdit = findViewById(R.id.postTitleEdit);
    editButton = findViewById(R.id.editButton);
    deleteButton = findViewById(R.id.deleteButton);
    key1 = findViewById(R.id.key);


    postTitleEdit.setText(getIntent().getStringExtra("title"));
    postTextEdit.setText(getIntent().getStringExtra("desc"));

    String key = getIntent().getExtras().get("key").toString();
    key1.setText(key);
    databaseReference = FirebaseDatabase.getInstance().getReference("Mblog").child(key);

}

public void update_button(View view){

    databaseReference.child("title").setValue(postTitleEdit.getText().toString());

    databaseReference.child("desc").setValue(postTextEdit.getText().toString());


}

要更新到 firebase,我从我的 RecyclerAdapter 获取密钥

view.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //Toast.makeText(context,"Signed in", Toast.LENGTH_LONG).show();
                final Intent intent;
                switch (getAdapterPosition()){
                    default:
                        intent = new Intent(context, CowDet.class);
                        Blog blog = blogList.get(getAdapterPosition());

                        intent.putExtra("title", blog.getTitle());
                        intent.putExtra("desc", blog.getDesc());
                        intent.putExtra("key", blog.getUserid());

我遇到的问题是,当我尝试更新 title 和 desc 时,会在 firebase 上创建一个新字段,而不是实际更新它。 我认为这可能与引用 Childs 相关。

我不确定如何引用每个字段的 ID:

id

0 个答案:

没有答案