Error receiving data from Firebase

1

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

Firebase BD Image

}

    
asked by anonymous 06.06.2017 / 15:59

2 answers

0

Your onDataChange already receives a DataSnapshot as a child-node . You only need to use 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();

    }
    
06.06.2017 / 16:41
0

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

@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 the News.

It looks like this:

News     - uid         date         - Image         - news         - title

To do this, in the creation of the 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)
    
08.06.2017 / 00:07