Delete firebase log

0

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

// I want 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 that retrieves data from the database.

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);
  

ClassPrice Class

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;
    }   
    }
    
asked by anonymous 20.10.2017 / 22:47

1 answer

0

You can add your key to your class, but do not save this key in the database. Because if you save, 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 @Exclude annotation to the key getters and setters:

    private String chave;

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

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

And initialize 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);
                                }
    
24.02.2018 / 17:27