从firebase检索返回null的数据

时间:2018-06-09 16:05:36

标签: android firebase firebase-realtime-database

我正在尝试从Firebase数据库读取数据并使用数据库字段初始化对象,但它返回null。

模特课。

public class Event {
private String Nome;
private String data;
private String descrizione;
private String luogo;
private String mese;
private String ora_fine;
private String ora_inizio;
private String type;
private String programma;
private String img ;

public Event()
{

}

public Event(String nome, String data, String descrizione, String luogo, String mese, String ora_fine, String ora_inizio, String type, String mprogramma, String mimg) {
    Nome = nome;
    this.data = data;
    this.descrizione = descrizione;
    this.luogo = luogo;
    this.mese = mese;
    this.ora_fine = ora_fine;
    this.ora_inizio = ora_inizio;
    this.type = type;
    this.programma=mprogramma;
    this.img=mimg;
}

public String getNome() {
    return Nome;
}

public String getData() {
    return data;
}

public String getDescrizione() {
    return descrizione;
}

public String getLuogo() {
    return luogo;
}

public String getMese() {
    return mese;
}

public String getOra_fine() {
    return ora_fine;
}

public String getOra_inizio() {
    return ora_inizio;
}

public String getType() {
    return type;
}

public String getProgramma() {
    return programma;
}

public String getImg() {
    return img;
}

public Date toDate()
{
    DateFormat format = new SimpleDateFormat("dd/MM/yyyy", Locale.ITALIAN);
    Date date = null;
    try {
        date = format.parse(data);
    } catch (ParseException e) {
        e.printStackTrace();
    }
    return date;
}
}

主要活动

    public class MainActivity extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener
{
    private DrawerLayout mDrawerLayout;
    private ActionBarDrawerToggle mToggle;
    private ImageView vangelo;
    private ImageView santo;
    private ImageView evento;
    private ImageView notizia;
     private DatabaseReference vangeloRef;
     private DatabaseReference santoRef;
     private DatabaseReference eventoRef;
     private DatabaseReference notiziaRef;


    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mDrawerLayout= (DrawerLayout) findViewById(R.id.drawer_layout); //finds the drawer layout view
        mToggle= new ActionBarDrawerToggle(this, mDrawerLayout,R.string.Open, R.string.Close); //creates the action bar
        mDrawerLayout.addDrawerListener(mToggle); //adds the action bat to the layout
        mToggle.syncState(); //syncs the toggle state with the actual drawer layout

        DatabaseReference RootRef=FirebaseDatabase.getInstance().getReference();
        eventoRef=RootRef.child("MainEvent");




        vangelo=(ImageView)findViewById(R.id.vangelo);
        santo=(ImageView)findViewById(R.id.santo);
        evento=(ImageView)findViewById(R.id.evento);
        notizia=(ImageView)findViewById(R.id.notizia);

        vangelo.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

            }
        });
        santo.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

            }
        });
        evento.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {


                    }







        });
        notizia.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

            }
        });


        ActionBar actionBar = getSupportActionBar();
        if (actionBar != null)
            actionBar.setDisplayHomeAsUpEnabled(true);
        NavigationView mNavigationView = (NavigationView) findViewById(R.id.nav_view);
        if (mNavigationView != null) {
            mNavigationView.setNavigationItemSelectedListener(this);
        }


    }


@Override
public  void onStart()
{
    super.onStart();
    eventoRef.addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            Event ev= dataSnapshot.getValue(Event.class);
            Log.v("Done",ev.getData());
        }

        @Override
        public void onCancelled(DatabaseError databaseError) {

        }
    });

}


    @Override

    public boolean onOptionsItemSelected(MenuItem Item)
    {
        if(mToggle.onOptionsItemSelected(Item))
        {
            return true;
        }
        return super.onOptionsItemSelected(Item);

    }
   @Override
    public boolean onNavigationItemSelected(MenuItem Item)
    {
        int id = Item.getItemId();
        Intent intent;
        if (id == R.id.preghiere)
        {
            intent= new Intent(this, Preghiere.class);
            intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
            this.startActivity(intent);
        }
        else if ( id== R.id.bans)
        {
            intent= new Intent(this, Bans.class);
            intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
            this.startActivity(intent);
        }
        else if (id== R.id.canzoni)
        {
            intent= new Intent(this, Canzoni.class);
            intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
            this.startActivity(intent);
        }
        else if (id==R.id.calendario)
        {
            intent= new Intent(this, Calendario.class);
            intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
            this.startActivity(intent);
        }
        else if (id== R.id.per_riflettere)
        {
            intent= new Intent(this, perRiflettere.class);
            intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
            this.startActivity(intent);
        }
        else if( id== R.id.home)
        {
            intent= new Intent(this, this.getClass());
            intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
            this.startActivity(intent);
        }
        DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        drawer.closeDrawer(GravityCompat.START);
        return true;
    }

}

数据库树

tree

我正在尝试使用数据库中的“MainEvent”字段实例化单个事件对象,但代码返回null。我怎么解决?

1 个答案:

答案 0 :(得分:0)

您必须按如下方式检索数据:

 eventoRef.addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(DataSnapshot dataSnapshot) {
            for (DataSnapshot snapshot : dataSnapshot.getChildren()) {
                Event ev= snapshot.getValue(Event.class);  
             }
            }

            @Override
            public void onCancelled(DatabaseError databaseError) {

            }
        });