我是一名新的Android程序员。我试图找出两个firebase结果之间的差异:Expenditure
和Sale
。我在一个活动中的一个类中完成了两个查询。但是,我对我编写的代码没有任何好运。目前,两种方法都返回空值。
以下是代码:
package com.example.moses.farm;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.TextView;
import com.firebase.client.FirebaseError;
import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.auth.FirebaseUser;
import com.google.firebase.database.DataSnapshot;
import com.google.firebase.database.DatabaseError;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import com.google.firebase.database.ValueEventListener;
public class LukenyaSales extends AppCompatActivity {
private TextView profit;
private TextView Loss;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_lukenya_sales);
profit = (TextView) findViewById(R.id.profit);
Loss = (TextView) findViewById(R.id.Loss);
class Profit {
private int Expendituree;
private int Salee;
private FirebaseAuth firebaseAuth;
public int getExpenditure() {
firebaseAuth = FirebaseAuth.getInstance();
DatabaseReference dref = FirebaseDatabase.getInstance().getReference();
dref.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot snapshot) {
int totalAmount = 0;
FirebaseUser user = firebaseAuth.getCurrentUser();
DatabaseReference dref = FirebaseDatabase.getInstance().getReference();
if (user != null) {
final String uid = FirebaseAuth.getInstance().getCurrentUser().getUid();
for (DataSnapshot postSnapshot : snapshot.getChildren()) {
dref = dref.child(uid + "Expenditure");
DogExpenditure dogExpenditure = postSnapshot.getValue(DogExpenditure.class);
totalAmount += dogExpenditure.getAmount();
Expendituree = totalAmount;
}
}
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
public void onCancelled(FirebaseError firebaseError) {
System.out.println("The read failed: " + firebaseError.getMessage());
}
});
return Expendituree;
}
public int getSale() {
final DatabaseReference dreff = FirebaseDatabase.getInstance().getReference();
dreff.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot snapshot) {
int totalAmount = 0;
FirebaseUser user = firebaseAuth.getCurrentUser();
if (user != null) {
final String uid = FirebaseAuth.getInstance().getCurrentUser().getUid();
DatabaseReference dreff = FirebaseDatabase.getInstance().getReference();
for (DataSnapshot postSnapshot : snapshot.getChildren()) {
dreff = dreff.child(uid + "Expenditure");
DogExpenditure dogExpenditure = postSnapshot.getValue(DogExpenditure.class);
totalAmount += dogExpenditure.getAmount();
}
Salee = totalAmount;
}
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
public void onCancelled(FirebaseError firebaseError) {
System.out.println("The read failed: " + firebaseError.getMessage());
}
});
return Salee;
}
int pro;
int loss;
public int getLoss() {
if (Expendituree > Salee) {
loss = Expendituree - Salee;
Loss.setText("Your total Loss for this Unit is:" + Integer.toString(loss));
}
return loss;
}
public int getPro() {
if (Expendituree < Salee) {
pro = Salee - Expendituree;
profit.setText("Your total Profit for this Unit is:" + Integer.toString(pro));
}
return pro;
}
}
}
}