从firebase中获取所有表值null对象参考firebase数据库 [英] get all table values from firebase null object reference firebase database

查看:96
本文介绍了从firebase中获取所有表值null对象参考firebase数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

public class ShowBPGraphActivity extends AppCompatActivity {
public GraphView graphView;
public LineGraphSeries <DataPoint>lineGraphSeries; 
public String systolicbp;
public String diastolicbp;
public String timebp;
public String datebp;

public DatabaseReference db;
public BPFragmentTable bpFragmentTable = new BPFragmentTable(  );

 @Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate( savedInstanceState );
    setContentView( R.layout.activity_show_bp_graph );
    ButterKnife.bind( this );

     db = FirebaseDatabase.getInstance().getReference("bpfragmentTable");

    final GraphView graph = (GraphView) findViewById( R.id.graphView );

    final List<BPFragmentTable> bpFragmentTableList = new ArrayList<>();

    //Get datasnapshot at your "users" root node
    DatabaseReference ref = FirebaseDatabase.getInstance().getReference().child("bpfragmentTable");
    ref.addValueEventListener(new ValueEventListener() {
                @Override
                public void onDataChange(DataSnapshot dataSnapshot) {
                    List<BPFragmentTable> bpfragmentTableList = new ArrayList<>();

                    for(DataSnapshot dataSnapshot1 :dataSnapshot.getChildren()){

                        BPFragmentTable bpfragmentTable = dataSnapshot1.getValue(BPFragmentTable.class);

                        systolic = bpfragmentTable.getSystolicbp();
                        diastolic = bpfragmentTable.getDiastolicbp();
                        date = bpfragmentTable.getDatebp();
                        time = bpfragmentTable.getTimebp();
                        //bpfragmentTable.setSystolicbp(systolic);
                        //bpfragmentTable.setDiastolicbp(diastolic);
                        //bpfragmentTable.setDatebp(date);
                        //bpfragmentTable.setTimebp(time);
                        bpfragmentTableList.add(bpfragmentTable);
                        // Toast.makeText(MainActivity.this,""+name,Toast.LENGTH_LONG).show();
                    }

                @Override
                public void onCancelled(DatabaseError databaseError) {
                    //handle databaseError
                }
            });
// please help me why i get null value in systolic diastolic date and time

            System.out.println( "systolicbp" + systolicbp );
            System.out.println( "diastolicbp" + diastolicbp );
            System.out.println( "datebp" + datebp );
            System.out.println( "timebp" + timebp );

    try {
        @SuppressLint("SimpleDateFormat") SimpleDateFormat formatter = new SimpleDateFormat("hh:mm a\nMMM dd yyyy");
        dateValued = formatter.parse(datebp);
        dateInMills = dateValued.getTime();
    } catch (ParseException e) {
        e.printStackTrace();
    }

    final long xlong = dateInMills;
    final int ysystolicInt  = Integer.parseInt( systolicbp );
    final int ydiastolicInt = Integer.parseInt( diastolicbp );

这是图片中的错误

我想获取所有值bpfragmentTable并将所有值存储在字符串变量中,并使用该变量在图形视图中绘制图形.我想从图片中的表BPFragment表中获取所有值:

I want to get bpfragmentTable all values and store all values in the string variable and use that variable to draw a graph in the graph view. I want to get all values from the table-BPFragment table in the picture:

这是BPFragmentTable.class的getter和setter的类

and this is the class of getter and setter of BPFragmentTable.class

public class BPFragmentTable {

public String bpid;
public String systolicbp;
public String diastolicbp;
public String datebp;
public String timebp;

public BPFragmentTable(){}
public BPFragmentTable(String bpid,String systolicbp,String diastolicbp,String datebp,String timebp) {

     this.bpid = bpid;
     this.systolicbp = systolicbp;
     this.diastolicbp = diastolicbp;
     this.datebp = datebp;
     this.timebp = timebp;

}

public String getBpid() {
    return bpid;
}

public void setBpid(String  bpid) {
    this.bpid = bpid;
}

public String getSystolicbp() {
    return systolicbp;
}

public void setSystolicbp(String systolicbp) {
    this.systolicbp = systolicbp;
}

public String getDiastolicbp() {
    return diastolicbp;
}

public void setDiastolicbp(String diastolicbp) {
    this.diastolicbp= diastolicbp;
}

public String getDatebp() {
    return datebp;
}

public void setDatebp(String datebp) {
    this.datebp= datebp;
}

public String getTimebp() {
    return timebp;
}

public void setTimebp(String timebp) {
    this.timebp= timebp;
} 
}

为什么我会得到空对象引用?

Why do I get null object reference?

推荐答案

数据是从Firebase异步加载的.在您调用formatter.parse(datebp)时,onDataChange方法尚未运行,并且尚未设置datebp.您将需要将数据的解析移至 onDataChange:

Data is loaded from Firebase asynchronously. By the time you call formatter.parse(datebp) the onDataChange method hasn't run yet, and datebp hasn't been set. You will need to move the parsing of the data into onDataChange:

DatabaseReference ref = FirebaseDatabase.getInstance().getReference().child("bpfragmentTable");
ref.addValueEventListener(new ValueEventListener() {
    @Override
    public void onDataChange(DataSnapshot dataSnapshot) {
        List<BPFragmentTable> bpfragmentTableList = new ArrayList<>();

        for(DataSnapshot dataSnapshot1 :dataSnapshot.getChildren()){

            BPFragmentTable bpfragmentTable = dataSnapshot1.getValue(BPFragmentTable.class);

            systolic = bpfragmentTable.getSystolicbp();
            diastolic = bpfragmentTable.getDiastolicbp();
            date = bpfragmentTable.getDatebp();
            time = bpfragmentTable.getTimebp();
            bpfragmentTableList.add(bpfragmentTable);

            System.out.println( "systolicbp" + systolicbp );
            System.out.println( "diastolicbp" + diastolicbp );
            System.out.println( "datebp" + datebp );
            System.out.println( "timebp" + timebp );

            try {
                @SuppressLint("SimpleDateFormat") SimpleDateFormat formatter = new SimpleDateFormat("hh:mm a\nMMM dd yyyy");
                dateValued = formatter.parse(datebp);
                dateInMills = dateValued.getTime();
            } catch (ParseException e) {
                e.printStackTrace();
            }

        }

    @Override
    public void onCancelled(DatabaseError databaseError) {
        throw databaseError.toException(); // don't ignore errors
    }
});

对于刚接触Firebase的开发人员来说,这种异步性质是一个常见的陷阱.由于异步API在现代编程中极为常见,因此我强烈建议您快速了解它们.有关一些好的入门资料,请参见:

This asynchronous nature is a common pitfall for developers new to Firebase. Since asynchronous APIs are extremely common in modern programming, I highly recommend you learn about them quickly. For some good introductory material, see:

  • getContactsFromFirebase() method return an empty list (which also shows how to use an interface to get the values out of onDataChange)
  • this blog post on asynchronous APIs
  • Setting Singleton property value in Firebase Listener (where I explained how in some cases you can get synchronous data loading, but usually can't)

这篇关于从firebase中获取所有表值null对象参考firebase数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆