Error while receiving Firebase data

Asked

Viewed 255 times

1

I’m having problems with my TCC, I’m not getting the data from Firebase and display them in a listview, it presents error, I’m using the ValueEventListener to try to get the data... to listview this in a fragment. Does anyone know how to solve this problem ???

ERROR:

FATAL EXCEPTION: main Process: Matheus.example.com.topaziovilleapp, PID: 13075 com.google.firebase.database.Databaseexception: Can’t Convert Object of type java.lang.String to type Matheus.example.com.topaziovilleapp.Modelo.Noticias at com.google.android.gms.Internal.zzbqi.zze(Unknown Source) at com.google.android.gms.Internal.zzbqi.zzb(Unknown Source) at com.google.android.gms.Internal.zzbqi.zza(Unknown Source) at com.google.firebase.database.Datasnapshot.getValue(Unknown Source) at Matheus.example.com.topaziovilleapp.Fragments.fragmentNoticias$1.onDataChange(fragmentNoticias.java:61) at com.google.android.gms.Internal.zzbmz.zza(Unknown Source)

Fragment

public class fragmentNoticias extends Fragment {
private ListView listView;
private ArrayAdapter adapter;
private ArrayList<String> noticias;
private DatabaseReference databaseReference;
private FirebaseAuth autenticacao;
private String usuariologado;

public fragmentNoticias() {
    // Required empty public constructor
}


@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    View view = inflater.inflate(R.layout.fragment_noticias, container, false);

    noticias = new ArrayList<>();
    listView = (ListView) view.findViewById(R.id.lv_noticias);
    adapter = new ArrayAdapter(
            getActivity(),
            android.R.layout.simple_list_item_1,
            noticias
    );
    listView.setAdapter(adapter);
    autenticacao = FirebaseConfig.getAutenticacao();
    databaseReference = FirebaseConfig.getFirebase().child("Noticias");
    databaseReference.addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            noticias.clear();
            for (DataSnapshot dados: dataSnapshot.getChildren()){
                Noticias noticia = dados.getValue(Noticias.class);
                noticias.add(noticia.getTitulo());

            }
            adapter.notifyDataSetChanged();

        }

        @Override
        public void onCancelled(DatabaseError databaseError) {

        }
    });
    return view;
}

}

Model Class

public class Noticias {
private String titulo;
private String noticia;
private String data;
private String imagem;

public Noticias() {
}

public String getTitulo() {
    return titulo;
}

public void setTitulo(String titulo) {
    this.titulo = titulo;
}

public String getNoticia() {
    return noticia;
}

public void setNoticia(String noticia) {
    this.noticia = noticia;
}

public String getData() {
    return data;
}

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

public String getImagem() {
    return imagem;
}

public void setImagem(String imagem) {
    this.imagem = imagem;
}

Image of Firebase BD

}inserir a descrição da imagem aqui

2 answers

0

The problem is that your "News" node is not an array. What happens is that you are iterating over the node values. So

@Override
    public void onDataChange(DataSnapshot dataSnapshot) {
        noticias.clear();
        for (DataSnapshot dados: dataSnapshot.getChildren()){
            //Noticias noticia = dados.getValue(Noticias.class);
            //noticias.add(noticia.getTitulo());

           //dados.getValue() vai retornar: data, imagem, noticia, titulo como atributos.
        }
        adapter.notifyDataSetChanged();

    }

The correct thing would be to change the structure of Noticias.

being like this:

News - uid - date - image - news - title

For this to happen, in the creation of news, you need to give a "push()" in the reference.

Ex:

HashMap<String, Object> map = new HashMap<>(); 
map.put("data", data);
map.put("imagem", imagem);
map.put("noticia", noticia);
map.put("titulo", titulo);
FirebaseDatabase.getInstance().getReference("Noticias").push().setValue(objetonoticia)

0


Your onDataChange already receives a DataSnapshot as a Child-Node. You just need to use the getValue of DataSnaphot without using a looping:

@Override
    public void onDataChange(DataSnapshot dataSnapshot) {
        noticias.clear();
        Noticias noticia = dados.getValue(Noticias.class);
        noticias.add(noticia.getTitulo());

        adapter.notifyDataSetChanged();

    }

Browser other questions tagged

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