简体   繁体   中英

Why Firebase Realtime Database is not updated?

I have created a list. When I tried to update the values, the values captured correctly in Edittext. However, when I click the update button after correction, it doesn't work and force closing the app.

My update code is given below

public class UpdateActivity extends AppCompatActivity {

private EditText PName, PLocation;
private FirebaseDatabase mDatabase;
private DatabaseReference mDbRef;
private Button upbtn;

private String prID;
private String pname,plocation;

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

    prID = getIntent().getStringExtra("ProjID");
    pname = getIntent().getStringExtra("pname");
    plocation = getIntent().getStringExtra("plocation");

    PName = findViewById(R.id.uProjName);
    PName.setText(pname);
    PLocation = findViewById(R.id.uProjLocation);
    PLocation.setText(plocation);
    PType = findViewById(R.id.uProjType);

    upbtn = findViewById(R.id.ubutton);
   mDbRef = mDatabase.getInstance().getReference().child("Projects");

   upbtn.setOnClickListener(new View.OnClickListener() {
       @Override
       public void onClick(View v) {
           DatabaseReference updateProject = mDbRef.child(prID);
           updateProject.child("pname").setValue(PName);
           updateProject.child("plocation").setValue(PLocation);
           Toast.makeText(UpdateActivity.this, "Project updated Successfully",Toast.LENGTH_LONG).show();
   finish();
       }
   });
}
}

I am a learner. Please help me where my code went wrong?

Check out this example. The process is async so you have to add listener to determine if database updated or not

mDatabase.child("achildName").child("anotherChildName").setValue(YOUR_DATA)
        .addOnSuccessListener(new OnSuccessListener<Void>() {
            @Override
            public void onSuccess(Void aVoid) {
                // Write was successful!
                // ...
            }
        })
        .addOnFailureListener(new OnFailureListener() {
            @Override
            public void onFailure(@NonNull Exception e) {
                // Write failed
                // ...
            }
        });

Beside that, First, make sure you added Internet Permission in Manifest.xml And Then Check Realtime Database in Firebase Console and Check Security Rules.

To update realtime database https://firebase.google.com/docs/database/android/read-and-write#updating_or_deleting_data

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