在onCreate中调用方法

时间:2017-04-17 18:00:22

标签: android firebase methods firebase-realtime-database

我的活动中有两种方法。我试图在我的视图中显示这两种方法的结果。我被困在这里。我很感激你的帮助。

我试图在onCreate方法中调用它们,但我得到一个空屏幕:

这是我的代码:

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main3);
        Firebase.setAndroidContext(this);

        if (FirebaseAuth.getInstance().getCurrentUser() == null) {
            startActivity(new Intent(MainActivity.this, Login.class));
        } else {
            if (FirebaseAuth.getInstance().getCurrentUser() != null) {
                String uid = FirebaseAuth.getInstance().getCurrentUser().getUid();
            }
        }
        firebaseAuth = FirebaseAuth.getInstance();
        textView = (TextView) findViewById(R.id.textView);
        textView2 = (TextView) findViewById(R.id.textView2);
        textView4 = (TextView) findViewById(R.id.textView4);
        textView5 = (TextView) findViewById(R.id.textView5);
        textView3 = (TextView) findViewById(R.id.textView3);
        loss = (FloatingActionButton) findViewById(R.id.loss);
        profit = (FloatingActionButton) findViewById(R.id.profit);
        statement = (FloatingActionButton) findViewById(R.id.statement);
        salereport = (FloatingActionButton) findViewById(R.id.salereport);
        sale = (FloatingActionButton) findViewById(R.id.sale);
        exp = (FloatingActionButton) findViewById(R.id.exp);
        total = (FloatingActionButton) findViewById(R.id.total);
        loss.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                startActivity(new Intent(MainActivity.this, Katakastatement.class));
            }
        });
        profit.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                firebaseAuth.signOut();
                //closing activity
                finish();
                startActivity(new Intent(MainActivity.this, Login.class));
            }
        });
        statement.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                startActivity(new Intent(MainActivity.this, KavisuniTotal.class));
            }
        });
        sale.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                startActivity(new Intent(MainActivity.this, KatakaSales.class));
            }
        });
        exp.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                startActivity(new Intent(MainActivity.this, Katakaexp.class));
            }
        });
        salereport.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                startActivity(new Intent(MainActivity.this, kavisunistatement.class));
            }
        });
        total.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                startActivity(new Intent(MainActivity.this, LukenyaSales.class));
            }
        });


    }


    private boolean Expenditu() {



        FirebaseUser user = firebaseAuth.getCurrentUser();
        if (user != null) {
            DatabaseReference dref = FirebaseDatabase.getInstance().getReference();
            String loc = user.getUid();

            dref = dref.child(loc + "Expenditure");
            dref.addValueEventListener(new com.google.firebase.database.ValueEventListener() {
                @Override
                public void onDataChange(DataSnapshot snapshot) {
                    String Name;
                    String Locatio;
                    int totalAmount = 0;
                    for (DataSnapshot postSnapshot : snapshot.getChildren()) {
                        DogExpenditure dogExpenditure = postSnapshot.getValue(DogExpenditure.class);

                        totalAmount += dogExpenditure.getAmount();


                        textView4.setText(Integer.toString(totalAmount));

                    }
                }

                @Override
                public void onCancelled(DatabaseError databaseError) {
                }

                public void onCancelled(FirebaseError firebaseError) {
                    System.out.println("The read failed: " + firebaseError.getMessage());
                }
            });
        }
        Expenditu();
return Expenditu();

    }

    private boolean  sale() {

        FirebaseUser user = firebaseAuth.getCurrentUser();
        DatabaseReference dref = FirebaseDatabase.getInstance().getReference();
        String loc = user.getUid();
        dref = dref.child(loc + "Sales");
        dref.addValueEventListener(new com.google.firebase.database.ValueEventListener() {
            @Override
            public void onDataChange(DataSnapshot snapshot) {
                String Name;
                String Locatio;
                int totalAmounte = 0;
                for (DataSnapshot postSnapshot : snapshot.getChildren()) {
                    DogExpenditure dogExpenditure = postSnapshot.getValue(DogExpenditure.class);
                    totalAmounte += dogExpenditure.getAmount();

                    textView5.setText(Integer.toString(totalAmounte));
                }
            }
            @Override
            public void onCancelled(DatabaseError databaseError) {
            }
            public void onCancelled(FirebaseError firebaseError) {
                System.out.println("The read failed: " + firebaseError.getMessage());
            }
        });
        sale();
            return sale();
    }
    public void Names() {
        DatabaseReference dreff = FirebaseDatabase.getInstance().getReference();
        FirebaseUser user = firebaseAuth.getCurrentUser();
        String loc = user.getUid();
        dreff = dreff.child(loc);
        dreff.addValueEventListener(new com.google.firebase.database.ValueEventListener() {
            @Override
            public void onDataChange(DataSnapshot snapshot) {
                String Name;
                String Profit;
                String Locatio;
                for (DataSnapshot postSnapshot : snapshot.getChildren()) {
                    Farm farm = postSnapshot.getValue(Farm.class);
                    Name = farm.getName();
                    Locatio = farm.getLocation();
                }
            }
            @Override
            public void onCancelled(DatabaseError databaseError) {
            }
            public void onCancelled(FirebaseError firebaseError) {
                System.out.println("The read failed: " + firebaseError.getMessage());
            }
        });
        System.out.println("ess");
    }

2 个答案:

答案 0 :(得分:0)

使用

Leaving_Date[, bin := c(7, 14, 30)[ findInterval(n, c(0, 7, 14, 30)) ]]

   Id       Date       n bin
1:  1 2017-01-01 14 days  30
2:  2 2017-02-03  1 days   7
3:  3 2017-01-01 16 days  30
4:  4 2017-03-10 NA days  NA
5:  3 2017-02-09  1 days   7
6:  5 2017-02-05 NA days  NA

setText方法更改textview的文本,你似乎有一些。 String.valueOf()适用于bool,int和多个基本java类。它应该做的工作。

答案 1 :(得分:0)

取代recursive来电,从booleanExpenditu方法返回sale值。

更新您的Expenditu()方法,如下所示:

private boolean Expenditu() {

    FirebaseUser user = firebaseAuth.getCurrentUser();
    if (user != null) {
        DatabaseReference dref = FirebaseDatabase.getInstance().getReference();
        String loc = user.getUid();

        dref = dref.child(loc + "Expenditure");
        dref.addValueEventListener(new com.google.firebase.database.ValueEventListener() {
            @Override
            public void onDataChange(DataSnapshot snapshot) {
                String Name;
                String Locatio;
                int totalAmount = 0;
                for (DataSnapshot postSnapshot : snapshot.getChildren()) {
                    DogExpenditure dogExpenditure = postSnapshot.getValue(DogExpenditure.class);

                    totalAmount += dogExpenditure.getAmount();


                    textView4.setText(Integer.toString(totalAmount));

                }
            }

            @Override
            public void onCancelled(DatabaseError databaseError) {
            }

            public void onCancelled(FirebaseError firebaseError) {
                System.out.println("The read failed: " + firebaseError.getMessage());
            }
        });
    }

    return true;
}

更新您的sale()方法,如下所示:

private boolean  sale() {

    FirebaseUser user = firebaseAuth.getCurrentUser();
    DatabaseReference dref = FirebaseDatabase.getInstance().getReference();
    String loc = user.getUid();
    dref = dref.child(loc + "Sales");
    dref.addValueEventListener(new com.google.firebase.database.ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot snapshot) {
            String Name;
            String Locatio;
            int totalAmounte = 0;
            for (DataSnapshot postSnapshot : snapshot.getChildren()) {
                DogExpenditure dogExpenditure = postSnapshot.getValue(DogExpenditure.class);
                totalAmounte += dogExpenditure.getAmount();

                textView5.setText(Integer.toString(totalAmounte));
            }
        }
        @Override
        public void onCancelled(DatabaseError databaseError) {
        }
        public void onCancelled(FirebaseError firebaseError) {
            System.out.println("The read failed: " + firebaseError.getMessage());
        }
    });

    return true;
}

现在,从onCreate()或其他地方调用方法。

Expenditu();
sale();