首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >从返回null的firebase中检索数据

从返回null的firebase中检索数据
EN

Stack Overflow用户
提问于 2018-06-10 00:05:36
回答 1查看 37关注 0票数 1

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

模型类。

代码语言:javascript
复制
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;
}
}

主要活动

代码语言:javascript
复制
    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。我该怎么解决呢?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-06-10 00:28:35

您必须检索数据,如下所示:

代码语言:javascript
复制
 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) {

            }
        });
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/50776113

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档