0

I tried to insert data that the child is taken when filling the edittext. The code is below

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

    nama=(EditText)findViewById(R.id.nama);
    alamat=(EditText)findViewById(R.id.alamat);
    KTP=(EditText)findViewById(R.id.KTP);
    KK=(EditText)findViewById(R.id.KK);
    telepon=(EditText)findViewById(R.id.telepon);
    usia=(EditText)findViewById(R.id.usia);
    kode=(EditText)findViewById(R.id.kode);
    pass=(EditText)findViewById(R.id.pass);
    daftar=(Button)findViewById(R.id.daftar);

    member=new member();
    reff=FirebaseDatabase.getInstance().getReference().child(String.valueOf(kode));

    reff.addValueEventListener(new ValueEventListener() {

        @Override
        public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
            if(dataSnapshot.exists())
            maxid=(dataSnapshot.getChildrenCount());
        }

        @Override
        public void onCancelled(@NonNull DatabaseError databaseError) {

        }
    });

    daftar.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            int age=Integer.parseInt(usia.getText().toString().trim());
            long telp=Long.parseLong(telepon.getText().toString().trim());
            long kel=Long.parseLong(KK.getText().toString().trim());
            long pen=Long.parseLong(KTP.getText().toString().trim());
            member.setNama(nama.getText().toString().trim());
            member.setAlamat(alamat.getText().toString().trim());
            member.setPass(pass.getText().toString().trim());
            member.setUsia(age);
            member.setKK(kel);
            member.setKTP(pen);
            member.setTelepon(telp);

            reff.child(String.valueOf(maxid+1)).setValue(member);
        }
    });

}

in this code, the child where the data should be inserted using "Kode" that we fill in the layout form. the warning was "Invalid Firebase Database path: androidx.appcompat.widget.AppCompatEditText"

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807

1 Answers1

0

Your kode variable is an EditText view in your UI, which is not a valid path in the database. You probably want to use the value that the user entered into the EditText, which you can do with:

String value = kode.getText().toString();    
reff=FirebaseDatabase.getInstance().getReference().child(value);

Also see:

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
  • the code fix the error, but it failed to get the string for my child array and end up creating new child everytime I insert the data. tried to fix reff.child(String.valueOf(maxid+1)).setValue(member); to reff.child(value).child(String.valueOf(maxid+1)).setValue(member); also adding the 'Final' to String value = kode.getText().toString(); but ended up deleted all the data and replace it to new one. – Arcadia Vany May 26 '20 at 06:03
  • If the behavior is now different, this change fixes the problem you posted about. You may well have other problems, in which case I recommend posting about those in a separate question with their own [minimal, complete/standalone reproduction of *that* problem](http://stackoverflow.com/help/mcve). – Frank van Puffelen May 26 '20 at 23:45