Problems to list firebase data in android

1

Good morning! I'm having trouble listing data from a certain node in my application ... when I go to list it according to the codes below I have the following exception return: "com.google.firebase.database.DatabaseException: Can not convert object of type java.lang.String to type com.ramattecgmail.rafah.herdeirosapp.Models.Cardapio "as the error no informs which object exactly it can not convert I could not solve the problem. Here is how the structure in firebase is:

list_cardapio.xml

<?xmlversion="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout    xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/fundo_cardapio">


<TextView
    android:id="@+id/tv_refeicao_lista"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Refeição"
    app:layout_constraintTop_toTopOf="parent"
    android:layout_marginTop="16dp"
    android:layout_marginLeft="24dp"
    app:layout_constraintLeft_toLeftOf="parent"
    android:textSize="20sp"/>

<TextView
    android:id="@+id/tv_ingredientes_lista"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="O que teremos?"
    android:layout_marginTop="8dp"
    app:layout_constraintTop_toBottomOf="@+id/tv_refeicao_lista"
    android:layout_marginLeft="24dp"
    app:layout_constraintLeft_toLeftOf="parent" />

 </android.support.constraint.ConstraintLayout>

ActivityCardapio.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.ramattecgmail.rafah.herdeirosapp.Activitys.CardapioActivity"
android:orientation="vertical">

<android.support.design.widget.AppBarLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:theme="@style/AppTheme.AppBarOverlay">

    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:background="?attr/colorPrimary"
        app:popupTheme="@style/AppTheme.PopupOverlay" />

</android.support.design.widget.AppBarLayout>

<ListView
    android:id="@+id/lv_cardapio"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:divider="@color/linha_cardapio"
    android:dividerHeight="0.3dp"/>


</LinearLayout>

Cardapio.java (models)

public class Cardapio {
//ATRIBUTOS
private String refeicao;
private String ingredientes;
private String data;

public Cardapio(){

}

public void salvarCardapio(){

    //Pegando o ano atual
    Date ano = Calendar.getInstance().getTime();
    DateFormat format = new SimpleDateFormat("yyyy");
    String anoRetiro = format.format(ano);

    //pegando o ano atual para salvar no nó retiro
    DatabaseReference reference = ConfiguracaoFirebase.getFirebaseReference();
    reference.child("RETIRO")
            .child(anoRetiro)
            .child("CARDAPIO")
            .child(getData())
            .child(getRefeicao())
            .setValue(this);

}

public String getRefeicao() {
    return refeicao;
}

public void setRefeicao(String refeicao) {
    this.refeicao = refeicao;
}

public String getIngredientes() {
    return ingredientes;
}

public void setIngredientes(String ingredientes) {
    this.ingredientes = ingredientes;
}

public String getData() {
    return data;
}

public void setData(String data) {
    this.data = data;
}

}

CardapioAdapter.java

public class CardapioAdapter extends ArrayAdapter<Cardapio> {
private ArrayList<Cardapio> arrayList;
private Context context;

public CardapioAdapter(@NonNull Context c, @NonNull ArrayList<Cardapio> objects) {
    super(c, 0, objects);
    this.arrayList = objects;
    this.context = c;
}

@NonNull
@Override
public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
    View view = null;

    //Validando e criando a lista
    if (arrayList != null){
        LayoutInflater inflater = (LayoutInflater) context.getSystemService(context.LAYOUT_INFLATER_SERVICE);

        //Montando a view a partir do XML
        view = inflater.inflate(R.layout.lista_cardapio, parent, false);

        //Recuperando os componentes para exibição
        TextView refeicao = view.findViewById(R.id.tv_refeicao_lista);
        TextView ingredientes = view.findViewById(R.id.tv_ingredientes_lista);

        Cardapio cardapio = arrayList.get(position);
        refeicao.setText(cardapio.getRefeicao());
        ingredientes.setText(cardapio.getIngredientes());

    }
    return view;
}
}

And last but not least, the activity that should display the list CardapioActivity.java

public class CardapioActivity extends AppCompatActivity {
//ATRIBUTOS
ListView listView;

private ArrayList<Cardapio> arrayList;
private ArrayAdapter adapter;
private DatabaseReference reference;
private ValueEventListener valueEventListener;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_cardapio);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);
    getSupportActionBar().setDisplayHomeAsUpEnabled(true);

    //INSTANCIANDO OS COMPONENTES
    arrayList = new ArrayList<>();
    listView = (ListView) findViewById(R.id.lv_cardapio);
    adapter = new CardapioAdapter(CardapioActivity.this, arrayList);
    listView.setAdapter(adapter);

    //Pegando o ano atual
    Date ano = Calendar.getInstance().getTime();
    DateFormat format = new SimpleDateFormat("yyyy");
    String anoRetiro = format.format(ano);

    reference = ConfiguracaoFirebase.getFirebaseReference()
            .child("RETIRO")
            .child(anoRetiro)
            .child("CARDAPIO")
            .child("24")
            .child("11")
            .child("Almoço");

    valueEventListener = new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            arrayList.clear();

            //Listando os eventos
            for (DataSnapshot data: dataSnapshot.getChildren()){
                Cardapio cardapio = data.getValue(Cardapio.class);
                arrayList.add(cardapio);
            }
            adapter.notifyDataSetChanged();
        }

        @Override
        public void onCancelled(DatabaseError databaseError) {

        }
    };
}

/******************************** METODOS ********************************/
@Override
protected void onStart() {
    super.onStart();
    reference.addValueEventListener(valueEventListener);
}

@Override
protected void onStop() {
    super.onStop();
    reference.removeEventListener(valueEventListener);
}

}

No reference it has the last child just for testing, so I want to list all meals for the day I go through a parameter (in this case, the 24 and 11 will be passed dynamically), and you can see that in the class Cardapio Model where I do the recording through a dialogFragment I instantiate the date and the firebase alone separates the day of the month in its nodes as image ... Thank you!

    
asked by anonymous 04.10.2017 / 15:14

1 answer

0

Apparently the problem was related to the fact that when I saved the data I did not add any kind of key, just pure data on the nodes, so I added an encryption just on the node that I needed to bring the children to as the image:

In this way the same code presented above the account to bring the data from the cardapio that I needed, I replied for example, if someone has problems to list the data of the firebase, maybe adding an encryption on the child node is enough if you are listing them with dataSnapsho.getchildren

    
07.10.2017 / 15:37