如何从firebase获取数据并将其保存在arraylist对象中?

时间:2017-06-01 13:35:31

标签: android firebase nullpointerexception firebase-realtime-database

这是我的数据库结构:enter image description here

我想从firebase数据库中获取数据并将其保存在arraylist对象中,以在Android上的Textview中显示该对象的属性。

我试过这个:

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


            }
        });

    }

首先,我得到了数据库中所有数据的参考,我试图得到它,但我不知道如何做到这一点。

此外,当我完成此操作时会抛出此错误:

  

java.lang.NullPointerException:尝试调用虚方法&#39; void   android.widget.TextView.setText(java.lang.CharSequence中)&#39;在null   对象参考                                                                                    在   es.laramira.atellez.moroninfo.Models.NewsViewHolder $ 1 $ 1.onDataChange(NewsViewHolder.java:48)                                                                                    在com.google.firebase.database.Query $ 1.onDataChange(未知来源)                                                                                    在com.google.android.gms.internal.zzbpx.zza(未知来源)                                                                                    在com.google.android.gms.internal.zzbqx.zzZT(未知来源)                                                                                    在com.google.android.gms.internal.zzbra $ 1.run(未知来源)                                                                                    在android.os.Handler.handleCallback(Handler.java:751)                                                                                    在android.os.Handler.dispatchMessage(Handler.java:95)                                                                                    在android.os.Looper.loop(Looper.java:154)                                                                                    在android.app.ActivityThread.main(ActivityThread.java:6119)                                                                                    at java.lang.reflect.Method.invoke(Native Method)                                                                                    在   com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:886)                                                                                    在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)

此操作位于onClick上。

有人知道如何解决它吗?

3 个答案:

答案 0 :(得分:0)

您必须先初始化TextView:

titularDetalle = (TextView) findViewById (R.id.YOUR_TEXT_VIEW_ID);
titularDetalle.setText(...);

这是您在TextView的布局XML中定义的ID。

答案 1 :(得分:0)

您的titularDetalle变量为空! 您需要为其定义分配空间。

其中一种方法是调用

titularDetalle = (TextView) findViewById (R.id.TEXT_VIEW_ID);

尝试致电titularDetalle.setText(noticias.getTitular());

之前

答案 2 :(得分:0)

在这种情况下,问题与Firebase无关。该错误表明titularDetalle为空,因此无法在其上调用.setText()

查看RecyclerView项目的布局XML,并确保android:id与使用titularDetalle方法初始化findViewById()时指定的ID相同。