简体   繁体   中英

How to edit a child value when under a firebase auto generated key

I am trying to edit the value of "shopB" in my database. I am doing so inside of a view holder, by use of a dialog box with an edit text field and an approve button. When a user clicks on a specific transaction, they're given a dialog box where they enter the value for shopB and then click Approve.

I am struggling to do this as I cannot access that value because of the uniquely generated key that firebase has. I have many posts with similar problems to mine but as I am doing this inside of a view holder I do not see how I can use DataSnapshot. Any help would be greatly appreciated as I am getting very lost.

Database Structure: 在此输入图像描述

viewHolder.setItemClickListener(new ItemClickListener() {
                        @Override
                        public void onClick(android.view.View view, int position, boolean isLongClick) {
                            Toast.makeText(Request.this, "Receiving: " + shopA, Toast.LENGTH_SHORT).show();
                            ThisDialog = new Dialog(Request.this);
                            ThisDialog.setContentView(R.layout.dialog_template);
                            final EditText Write = (EditText) ThisDialog.findViewById((R.id.write));
                            Button Approve = (Button) ThisDialog.findViewById(R.id.approve);

                            Write.setEnabled(true);
                            Approve.setEnabled(true);

                            Approve.setOnClickListener(new View.OnClickListener() {
                                @Override
                                public void onClick(View v) {
                                    String newShopB = Write.getText().toString().replace(".", " ");
                                    transaction.child("key").child("shopB").setValue(newShopB);
                                    Toast.makeText(Request.this, "CustB Approval", Toast.LENGTH_SHORT).show();
                                    ThisDialog.cancel();
                                }
                            });
                            ThisDialog.show();
                        }
                    });

This would be my code but obviously where I have "transaction.child("key")" does not find the key.

Oh and transaction is defined earlier in my code as

database = FirebaseDatabase.getInstance();
start = database.getReference("Transaction");
transaction = start.child(passedEmail);

Here's a possible solution to workaround on your project:

start.addListenerForSingleValueEvent(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
          for (DataSnapshot child : dataSnapshot.child(key).getChildren()) {

                //insert additional codes here
              }
            }
          });

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM