Problems creating Child in Firebase

Asked

Viewed 283 times

0

I’m trying to add dishes to my firebase database but I’m having some problems so far I don’t know why it doesn’t work. and makes this mistake:

Process: meals.com.meals, PID: 1585
java.lang.RuntimeException: java.lang.reflect.InvocationTargetException
   at com.google.android.gms.internal.zh.zzas(Unknown Source)
   at com.google.android.gms.internal.zg.zzao(Unknown Source)
   at com.google.android.gms.internal.zg.zzar(Unknown Source)
   at com.google.android.gms.internal.zh.zzas(Unknown Source)
   at com.google.android.gms.internal.zg.zzao(Unknown Source)
   at com.google.android.gms.internal.zg.zzar(Unknown Source)
   at com.google.android.gms.internal.zh.zzas(Unknown Source)
   at com.google.android.gms.internal.zg.zzao(Unknown Source)
   at com.google.android.gms.internal.zg.zzar(Unknown Source)
   at com.google.android.gms.internal.zh.zzas(Unknown Source)
   at com.google.android.gms.internal.zg.zzao(Unknown Source)
   at com.google.android.gms.internal.zg.zzar(Unknown Source)
   at com.google.android.gms.internal.zh.zzas(Unknown Source)
   at com.google.android.gms.internal.zg.zzao(Unknown Source)
   at com.google.android.gms.internal.zg.zzar(Unknown Source)
   at com.google.android.gms.internal.zh.zzas(Unknown Source)
   at com.google.android.gms.internal.zg.zzao(Unknown Source)
   at com.google.android.gms.internal.zg.zzar(Unknown Source)
   at com.google.android.gms.internal.zh.zzas(Unknown Source)
   at com.google.android.gms.internal.zg.zzao(Unknown Source)
   at com.google.android.gms.internal.zg.zzar(Unknown Source)
   at com.google.android.gms.internal.zh.zzas(Unknown Source)
   at com.google.android.gms.internal.zg.zzao(Unknown Source)
   at com.google.android.gms.internal.zg.zzar(Unknown Source)
   at com.google.android.gms.internal.zh.zzas(Unknown Source)
   at com.google.android.gms.internal.zg.zzao(Unknown Source)
   at com.google.android.gms.internal.zg.zzar(Unknown Source)
   at com.google.android.gms.internal.zh.zzas(Unknown Source)
   at com.google.android.gms.internal.zg.zzao(Unknown Source)
   at com.google.android.gms.internal.zg.zzar(Unknown Source)
   at com.google.android.gms.internal.zh.zzas(Unknown Source)
   at com.google.android.gms.internal.zg.zzao(Unknown Source)
   at com.google.android.gms.internal.zg.zzar(Unknown Source)
   at com.google.android.gms.internal.zh.zzas(Unknown Source)
   at com.google.android.gms.internal.zg.zzao(Unknown Source)
   at com.google.android.gms.internal.zg.zzar(Unknown Source)
   at com.google.android.gms.internal.zh.zzas(Unknown Source)
   at com.google.android.gms.internal.zg.zzao(Unknown Source)
   at com.google.android.gms.internal.zg.zzar(Unknown Source)
   at com.google.android.gms.internal.zh.zzas(Unknown Source)
   at com.google.android.gms.internal.zg.zzao(Unknown Source)
   at com.google.android.gms.internal.zg.zzar(Unknown Source)
   at com.google.android.gms.internal.zh.zzas(Unknown Source)
   at com.google.android.gms.internal.zg.zzao(Unknown Source)
   at com.google.android.gms.internal.zg.zzar(Unknown Source)
   at com.google.android.gms.internal.zh.zzas(Unknown Source)
   at com.google.android.gms.internal.zg.zzao(Unknown Source)
   at com.google.android.gms.internal.zg.zzar(Unknown Source)
   at com.google.android.gms.internal.zh.zzas(Unknown Source)
   at com.google.android.gms.internal.zg.zzao(Unknown Source)
   at com.google.android.gms.internal.zg.zzar(Unknown Source)
   at com.google.android.gms.internal.zh.zzas(Unknown Source)
   at com.google.android.gms.internal.zg.zzao(Unknown Source)
   at com.google.android.gms.internal.zg.zzar(Unknown Source)
   at com.google.android.gms.internal.zh.zzas(Unknown Source)
   at com.google.android.gms.internal.zg.zzao(Unknown Source)
   at com.google.android.gms.internal.zg.zzar(Unknown Source)
   at com.google.android.gms.internal.zh.zzas(Unknown Source)
   at com.google.android.gms.internal.zg.zzao(Unknown Source)
   at com.google.android.gms.internal.zg.zzar(Unknown Source)
   at com.google.android.gms.internal.zh.zzas(Unknown Source)
   at com.google.android.gms.internal.zg.zzao(Unknown Source)
   at com.google.android.gms.internal.zg.zzar(Unknown Source)
   at com.google.android.gms.internal.zh.zzas(Unknown Source)
   at com.google.android.gms.internal.zg.zzao(Unknown Source)
at com.google.android.gms.in

For you to see what I am doing I will also post the code of the Activity and the dish class.

public class CriarPrato extends AppCompatActivity {

private Spinner NomeRestaurante;
private EditText NomePrato1;
private EditText PrecoPrato1;
private Button GerarPrato;
private String IDrestaurante;

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


    NomeRestaurante = (Spinner) findViewById(R.id.spinnerNomeRestaurantePrato);
    NomePrato1 = (EditText) findViewById(R.id.editTextNomePrato);
    PrecoPrato1 = (EditText) findViewById(R.id.editTextPrecoPrato);
    GerarPrato = (Button) findViewById(R.id.buttonPrato);

    Query queryPrato = ConfiguracaoFirebase.getReferencia().child("Restaurante").orderByChild("localRestaurante");

    queryPrato.addListenerForSingleValueEvent(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            //  final List<String> localRest = new ArrayList<String>();
            final List<String> NomeRestauranteList = new ArrayList<String>();

            for (DataSnapshot data : dataSnapshot.getChildren()) {
                Restaurante restaurante = data.getValue(Restaurante.class);
                //   localRest.add(restaurante.getLocalRestaurante().toString());
                NomeRestauranteList.add(restaurante.getNome().toString());
            }

            //LocalRestaurante = (Spinner) findViewById(R.id.SpinerLocalPrato);
            // ArrayAdapter<String> areas = new ArrayAdapter<String>(getApplicationContext(), R.layout.spinner_item, localRest);
            // areas.setDropDownViewResource(R.layout.spinner_item);
            // LocalRestaurante.setAdapter(areas);

            NomeRestaurante = (Spinner) findViewById(R.id.spinnerNomeRestaurantePrato);
            ArrayAdapter<String> NomeRestAdapter = new ArrayAdapter<String>(getApplicationContext(), R.layout.spinner_item, NomeRestauranteList);
            NomeRestAdapter.setDropDownViewResource(R.layout.spinner_item);
            NomeRestaurante.setAdapter(NomeRestAdapter);
        }

        @Override
        public void onCancelled(DatabaseError databaseError) {

        }
    });

    Query query = ConfiguracaoFirebase.getReferencia().child("Restaurante").orderByChild("nome").equalTo("McDonald's");

    query.addListenerForSingleValueEvent(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            for (DataSnapshot data : dataSnapshot.getChildren()) {
                IDrestaurante = data.getKey();
            }

        }

        @Override
        public void onCancelled(DatabaseError databaseError) {

        }
    });

    GerarPrato.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Prato prato = new Prato();
            prato.setID(IDrestaurante);
            prato.setNomePrato(NomePrato1.getText().toString());
            prato.setPrecoPrato(PrecoPrato1.getText().toString());

            prato.Salvar(IDrestaurante);
        }
    });
}

Note: I know that I am not still using the spinner, I had some problems to get the value of it so I put in hand the value, to do some tests.

public class Prato {

    private String ID;
    private String NomePrato;
    private String PrecoPrato;

    public Prato() {
    }

    public void Salvar(String key){
        DatabaseReference reference = ConfiguracaoFirebase.getReferencia();
        reference.child("Restaurante").child(key).child("Cardapio").child(getID()).setValue(this);
    }
    @Exclude
    public String getID() {
        return ID;
    }

    public void setID(String key) {
        reference = ConfiguracaoFirebase.getReferencia().child("Restaurante").child(key).child("Cardapio");
        this.ID = reference.push().getKey();
    }

    public String getNomePrato() {
        return NomePrato;
    }

    public void setNomePrato(String nomePrato) {
        NomePrato = nomePrato;
    }

    public String getPrecoPrato() {
        return PrecoPrato;
    }

    public void setPrecoPrato(String precoPrato) {
        PrecoPrato = precoPrato;
    }

    public DatabaseReference getReference() {
        return reference;
    }

    public void setReference(DatabaseReference reference) {
        this.reference = reference;
    }

    private DatabaseReference reference;

}

Follow an image of the database organization for better understanding

Eu quero adicionar em cada restaurante um nó chamado Cardápio e dentro deste nó Cardápio vou colocar meu produtos criando nós Com os id's e dentro deste id's vai ter as variáveis: nome do prato e valor no prato.

I want to add in each restaurant a node called Menu and within this node Menu I will put my products creating nodes With the id’s and within this is’s will have the variables: name of the dish and value on the dish.

  • Can you show us a Realtimedatabase firebase print on the console ? So it’s a little easier to understand the structure of your bank’s children nodes

  • Surely I will edit and put

  • @Eduardorafaelmoraes I made the change!!! Tks

1 answer

0

I don’t know if I got it right but from what I got from your story, I’d make the bank with that structure: inserir a descrição da imagem aqui .... in saving your model class would look like this:

public void Salvar(String key){
    DatabaseReference reference = ConfiguracaoFirebase.getReferencia();
    reference.child("Restaurante")
          .child("Iguatemi")
          .child("cardapio")
          .child(nomeLoja)
          .push()
          .child(prato.getNomeRestaurante)//opcional, assim vc teria o nome do bobs seguido de nós com o nome do prato e o valor
          .setValue(this)
}

That, thinking that the Iguatemi is a mall and has some shops in it, in case it is a restaurant you could eliminate the . Child(houseName), and to return the data:

Query queryPrato = ConfiguracaoFirebase.getReferencia().child("Restaurante").child(nomeRestaurante);

I believe this makes it easier to receive the nodes in the valueEventListener.

  • Eduardo I understood your answer, it’s a good idea but I wanted to understand why my save is not working. In what I saw the problem this in saving even seems that it bursts the amount Mb that one can pass to save. But it doesn’t make sense because they are info TXT.

Browser other questions tagged

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