how to delete a firebase record from android studio?

0

.I've done an app where you insert data from android studio to firebase with 3 EditText so

and what I need is to delete data but I do not know how to do it, the truth is that I am still new and I would need help ... the deletion could be just by consulting only the id or all the data

this is my MainActivity class

public class MainActivity extends AppCompatActivity {
private EditText Alias;
private EditText Nombre;
private EditText ID;

public static int javo=0;
public static ArrayList<Ccontacto> ccontactos = new ArrayList<Ccontacto>();

private Button btnagregarfb,btnlistafb,btnEliminar;
private FirebaseDatabase database;
private DatabaseReference databaseReference;

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

    database = FirebaseDatabase.getInstance();
    ID =(EditText) findViewById(R.id.txtId);
    Nombre=(EditText) findViewById(R.id.txtNombre);
    Alias =(EditText) findViewById(R.id.txtAlias);

    databaseReference = database.getReference("Contactos");
    databaseReference = database.getReference("Contactos");
    database.getReference().getRoot().addChildEventListener(new ChildEventListener() {
  @Override
  public void onChildAdded(DataSnapshot dataSnapshot, String s) {
      Toast.makeText(getApplicationContext(),"Contacto agregado", Toast.LENGTH_LONG).show();
  }
  @Override
  public void onChildChanged(DataSnapshot dataSnapshot, String s) {
  }
  @Override
  public void onChildRemoved(DataSnapshot dataSnapshot) {
  }
  @Override
  public void onChildMoved(DataSnapshot dataSnapshot, String s) {
  }
  @Override
  public void onCancelled(DatabaseError databaseError) {
  }});
    btnagregarfb = (Button) findViewById(R.id.btnagregarfb);
    btnagregarfb.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            javo++;
            int idcontacto = javo;
            String nombre = null;
            String alias=null;
            nombre = Nombre .getText().toString();
            alias = Alias.getText().toString();
            Ccontacto c = new Ccontacto(idcontacto,nombre,alias);
            databaseReference.push().setValue(c);
        }
    });
    btnlistafb = (Button)findViewById(R.id.btnlistafb);
    btnlistafb.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            Intent intent = new Intent (MainActivity.this,MainLista.class);
            startActivity(intent);
        }
    });
    btnEliminar = (Button) findViewById(R.id.btnEliminar);
    btnEliminar.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
 //Aqui es el evento de click de mi boton eliminar
        }
    });

}}

and this is my constructor class

public class Ccontacto {
int idcontacto = 0;
String nombre = "";
String alias = "";

public Ccontacto() {
}

public Ccontacto(int idcontacto, String nombre, String alias) {
    this.idcontacto = idcontacto;
    this.nombre = nombre;
    this.alias = alias;
}
public int getIdcontacto() {
    return idcontacto;
}

public void setIdcontacto(int idcontacto) {
    this.idcontacto = idcontacto;
}

public String getNombre() {
    return nombre;
}

public void setNombre(String nombre) {
    this.nombre = nombre;
}

public String getAlias() {
    return alias;
}

public void setAlias(String alias) {
    this.alias = alias;
}}
    
asked by Hara8kuda 21.06.2018 в 23:01
source

1 answer

0

To be able to delete a record it is necessary to know its key so before doing the setValue() you have to obtain the key by getKey() . You would have something like this:

String key = databaseReference.push().getKey();
Ccontacto c = new Ccontacto(idcontacto, nombre, alias);
databaseReference.child(key).setValue(c);

You have to save the key and associate it with the contact, because with that you can delete the record.

databaseReference.child(key).removeValue();

You can also delete the record by passing'null' on the'etValue () '.

databaseReference.child(key).setValue(null);

But all through the key . I see that you have a button to "list" the contacts, I suggest that you delete the contact from there and not from the registration window. Firebase provides ways to get the list of your records and in those events you can know the key of each contact. Check this link: How to work with data lists in Android

    
answered by 22.06.2018 / 19:19
source