从firebase

时间:2017-11-30 17:38:49

标签: android firebase firebase-realtime-database

我想从firebase获取选定的数据。实际上我使用listview来获取值。在所选项目上,我需要更多来自数据的信息。但不幸的是,我无法获得选定的数据。 那个相关的代码。

New_Deal_List.java

lvDealList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
            NewDeal_Database newDeal_database = dealList.get(i);

            Intent intent = new Intent(getApplicationContext(), Deal_Detail_one.class);

            mEditor.putString(getString(R.string.COOKER_ID), newDeal_database.getuId());
            mEditor.commit();
            passingtoDealDaysCheckValues(newDeal_database.getDealId());


            startActivity(intent);
        }
    });


    }

将交易ID传递给此函数。

    dealDaysRefrence.addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            for(DataSnapshot dealSnapshot : dataSnapshot.getChildren())
            {
                NewDeal_Database newDeal_database = dealSnapshot.child(DealId).getValue(NewDeal_Database.class);

                try{
                    Toast.makeText(New_Deal_List.this,dealSnapshot.child(DealId).getValue(NewDeal_Database.class).toString(),Toast.LENGTH_SHORT).show();
                    if(newDeal_database.getMonday()== true ){
                        Toast.makeText(getApplicationContext(),DEAL_DAYS_MONDAY,Toast.LENGTH_SHORT).show();
                        DEAL_DAYS_MONDAY = "Available";
                    }else{
                        Toast.makeText(getApplicationContext(),DEAL_DAYS_MONDAY,Toast.LENGTH_SHORT).show();
                        DEAL_DAYS_MONDAY = "Not-Available";
                    }
                    if(dealSnapshot.child(DealId).getValue(NewDeal_Database.class).getTuesday() == true){
                        DEAL_DAYS_TUESDAY = "Available";
                        Toast.makeText(getApplicationContext(),DEAL_DAYS_TUESDAY,Toast.LENGTH_SHORT).show();

                    }else{
                        DEAL_DAYS_TUESDAY = "Not-Available";
                        Toast.makeText(getApplicationContext(),DEAL_DAYS_TUESDAY,Toast.LENGTH_SHORT).show();
                    }
                    if(dealSnapshot.child(DealId).getValue(NewDeal_Database.class).getWednesday() == true){
                        DEAL_DAYS_WEDNESDAY = "Available";
                    }else{
                        DEAL_DAYS_WEDNESDAY = "Not-Available";
                    }
                    if(dealSnapshot.child(DealId).getValue(NewDeal_Database.class).getThursday() == true){
                        DEAL_DAYS_THURSDAY = "Available";
                    }else{
                        DEAL_DAYS_THURSDAY = "Not-Available";
                    }
                    if(dealSnapshot.child(DealId).getValue(NewDeal_Database.class).getFriday() == true){
                        DEAL_DAYS_FRIDAY = "Available";
                    }else{
                        DEAL_DAYS_FRIDAY = "Not-Available";
                    }

                }catch (Exception e){
                    e.printStackTrace();
                }finally {

                    mEditor.putString(getString(R.string.DEAL_DAYS_MONDAY), DEAL_DAYS_MONDAY);
                    mEditor.commit();

                    mEditor.putString(getString(R.string.DEAL_DAYS_TUESDAY), DEAL_DAYS_TUESDAY);
                    mEditor.commit();

                    mEditor.putString(getString(R.string.DEAL_DAYS_WEDNESDAY), DEAL_DAYS_WEDNESDAY);
                    mEditor.commit();

                    mEditor.putString(getString(R.string.DEAL_DAYS_THURSDAY), DEAL_DAYS_THURSDAY);
                    mEditor.commit();

                    mEditor.putString(getString(R.string.DEAL_DAYS_FRIDAY), DEAL_DAYS_THURSDAY);
                    mEditor.commit();
                }
            }
        }

        @Override
        public void onCancelled(DatabaseError databaseError) {

        }
    });

在上面的代码中,if和else条件没有获得所需的值。

android工作室也给我建议。检查suggetion快照

这是NewDeal_Database.java类,我在其中设置值。

public NewDeal_Database(Boolean Monday, Boolean Tuesday, Boolean Wednesday, Boolean Thursday, Boolean Friday){
    this.Monday = Monday;
    this.Tuesday = Tuesday;
    this.Wednesday = Wednesday;
    this.Thursday = Thursday;
    this.Friday = Friday;





public Boolean getMonday() {
    return Monday;
}

public Boolean getTuesday() {
    return Tuesday;
}

public Boolean getWednesday() {
    return Wednesday;
}

public Boolean getThursday() {
    return Thursday;
}

public Boolean getFriday() {
    return Friday;

}

我在New_Deal_Time.java中将数据插入firebase 使用此代码New_Deal_Time.java dealDaysRefrence.child(id).child(DealId).setValue(dealdays);

firebase快照

enter image description here

建议快照

enter image description here

1 个答案:

答案 0 :(得分:1)

首先,您的NewDeal_Database.java class应如下所示:

public class NewDeal_Database {
    public NewDeal_Database(Boolean monday, Boolean tuesday, Boolean wednesday, Boolean thursday, Boolean friday){
        this.monday = monday;
        this.tuesday = tuesday;
        this.wednesday = wednesday;
        this.thursday = thursday;
        this.friday = friday;


    public NewDeal_Database {} //Needed for Firebase


    public Boolean getMonday() {
        return monday;
    }

    public Boolean getTuesday() {
        return tuesday;
    }

    public Boolean getWednesday() {
        return wednesday;
    }

    public Boolean getThursday() {
        return thursday;
    }

    public Boolean getFriday() {
        return friday;
    }
}

要获取该数据,请使用以下代码:

ValueEventListener eventListener = new ValueEventListener() {
    @Override
    public void onDataChange(DataSnapshot dataSnapshot) {
        for(DataSnapshot ds : dataSnapshot.getChildren()) {
            String eightToten = ds.child("eightToten").getValue(Boolean.class);
            String nineToeleven = ds.child("nineToeleven").getValue(Boolean.class);
            String sixToeight = ds.child("sixToeight").getValue(Boolean.class);
            String twelveTotwo = ds.child("twelveTotwo").getValue(Boolean.class);
            Log.d("TAG", eightToten + " / " + nineToeleven + " / " + sixToeight + " / " + twelveTotwo);
        }
    }

    @Override
    public void onCancelled(DatabaseError databaseError) {}
};
dealDaysRefrence.child(id).addListenerForSingleValueEvent(eventListener);