This is my database structure:
I want to get datas from a firebase database and save it inside an arraylist object to show the atributes of the object in a Textview on Android.
Ive tried this:
public class NewsViewHolder extends RecyclerView.ViewHolder {
private View mView;
private Context context;
TextView titularDetalle;
public NewsViewHolder(final View itemView) {
super(itemView);
titularDetalle = (TextView) itemView.findViewById(R.id.txtCabeceraDetalle);
mView = itemView;
context = itemView.getContext();
itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
DatabaseReference myRef;
myRef = FirebaseDatabase.getInstance().getReference("noticias");
myRef.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
for (DataSnapshot noticia : dataSnapshot.getChildren()){
clsNoticias noticias = noticia.getValue(clsNoticias.class);
ArrayList<clsNoticias> arrayNoticias = new ArrayList<clsNoticias>();
arrayNoticias.add(noticias);
titularDetalle.setText(noticias.getTitular());
}
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
Intent intent = new Intent(context, ActivityDetalle.class);
//intent.putExtra("titular",titularDetalle);
view.getContext().startActivity(intent);
}
});
}
First of all, I get a reference of all datas inside the database and im trying to get it, but I dont know well how to make this.
Moreover, when Ive done this it throws this error:
java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference at es.laramira.atellez.moroninfo.Models.NewsViewHolder$1$1.onDataChange(NewsViewHolder.java:48) at com.google.firebase.database.Query$1.onDataChange(Unknown Source) at com.google.android.gms.internal.zzbpx.zza(Unknown Source) at com.google.android.gms.internal.zzbqx.zzZT(Unknown Source) at com.google.android.gms.internal.zzbra$1.run(Unknown Source) at android.os.Handler.handleCallback(Handler.java:751) at android.os.Handler.dispatchMessage(Handler.java:95) at android.os.Looper.loop(Looper.java:154) at android.app.ActivityThread.main(ActivityThread.java:6119) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)
This action is inside onClick on a item.
Someone knows how to fixed it?
titularDetalleis notnull- Pavneet_Singh