Delete firebase record

Asked

Viewed 956 times

1

Hello, I read the firebase records and insert an array of objects. The Json class has only business fields and does not have the key field. To delete a record I evaluate that the Best approach would be to change the Json class and insert the key field. Is this the best approach? Thank you.

//I wish to delete the record in this method

 listviewCompras.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
                    @Override
                    public boolean onItemLongClick(AdapterView<?> arg0, View arg1,
                                                   int pos, long id) {
                        // TODO Auto-generated method stub

                        Log.v("long clicked","pos: " + pos);

                        return true;
                    }
                })

;

// Code retrieving database data.

ref.addValueEventListener(new ValueEventListener() {
                            @Override
                            public void onDataChange(DataSnapshot snapshot) {

                                //habilitando o botao pata evitsr dula entras
                                buttonSave.setEnabled(true);

                                ParcelaCartao parcela = new ParcelaCartao();
                                ArrayList<ParcelaCartao> array = new ArrayList<>();
                                for (DataSnapshot childSnapshot : snapshot.getChildren())
                                {

                                    parcela = childSnapshot.getValue(ParcelaCartao.class);
                                    array.add(parcela);
                                }                   

                                monta_listview(array);

Class Parcelcartao

import com.google.firebase.database.*;
import java.util.*;
import java.text.*;
/**
 * Created by Belal on 2/23/2016.
 */
//@JsonIgnoreProperties
@IgnoreExtraProperties
public class ParcelaCartao
{

    private String descricao;
    private long parcelas;
    private double valor;
    private String datacompra;
    private String dataultparcela;

    public ParcelaCartao(String descricao, long parcelas ,double valor, String datacompra, String dataultparcela)
    {
        setDescricao(descricao);
        setParcelas(parcelas);
        setValor(valor);
        setDatacompra(datacompra);  
        setDataultparcela(dataultparcela);
    }


    public ParcelaCartao() {
        /*Blank default constructor essential for Firebase*/
    }

    public void setParcelas(long parcelas)
    {
        this.parcelas = parcelas;
    }

    public long getParcelas()
    {
        return parcelas;
    }



    public String getDescricao()
    {
        return descricao;
    }

    public void setDescricao(String descricao)
    {
        this.descricao = descricao;
    }


    public double getValor()
    {
        return valor;
    }

    public void setValor(double valor)
    {
        this.valor = valor;
    }

    public String getDatacompra()
    {
        return datacompra;
    }


    public void setDatacompra(String data_compra)
    {
        this.datacompra = data_compra;

    }


    public String getDataultparcela()
    {
        return dataultparcela;
    }

    public void setDataultparcela(String datault_parcela)
    {
        this.dataultparcela = datault_parcela;

    }


    @Override
    public String toString() {
        return "Compra: " +  descricao +
            "\n num_parcelas: " + parcelas + " valor: " + valor + 
            "\n data compra: " + datacompra + 
            "\n data ult parcela: " + dataultparcela;
    }   
    }
  • could put a part of the code to better understand your problem

  • Hello entered the class. Note that it does not have the key field.

  • I don’t know what the structure of your project looks like, but maybe the code of the screen where the delete method is

  • Hello entered the methods of data recovery and deletion.

1 answer

1

You can add your key to your class, but not store that key in the database. Because if you do, the key will take up more space in your database and will be a (unnecessary) repetition of the key that is already there.

To do this, add the annotation @Exclude for getters and key setters:

    private String chave;

    @Exclude
    public String getChave(){
        return chave;
    }

    @Exclude
    public void setChave(String novaChave){
        chave = novaChave;
    }

And boot this key when you are reading the data:

                                for (DataSnapshot childSnapshot : snapshot.getChildren())
                                {

                                    parcela = childSnapshot.getValue(ParcelaCartao.class);
                                    parcela.setChave(childSnapshot.getKey());
                                    array.add(parcela);
                                }

Browser other questions tagged

You are not signed in. Login or sign up in order to post.