I'm having problems with my TCC, I'm not able to receive data from Firebase
and display them in a listview, it has an error, I'm using ValueEventListener
to try to get the data ... listview
is in fragment
. Anyone know how to solve this problem ???
ERROR:
FATAL EXCEPTION: main Process: matheus.example.com.topaziovilleapp, PID: 13075 com.google.firebase.database.DatabaseException: Can not convert object of type java.lang.String to type matheus.example.com.topaziovilleapp.Modelo.News 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.fragmentNews $ 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;
}
}
Class Model
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;
}