如何在侦听器外部将TextSnapshot值获取到textview

时间:2019-03-17 08:35:16

标签: java android

我对Java没有太多经验。我需要从Firebase实时数据库获取值,并在侦听器之外返回此值。我想在textview中显示该值。我已经从数据库中获取了价值。我想在“ pre_units”文本视图中显示该价值。如何将其传递给外部听众?这是我的代码。

    public class ShowUnits extends AppCompatActivity {


TextView textView_cus, cur_units, pre_units;

double Amount, Ser_Charge, Total_Amount, Before_VAT, Units, Test;

DatabaseReference databaseBills;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.show_units);

    Intent intent = getIntent();

    databaseBills = FirebaseDatabase.getInstance().getReference("bills");
    Query lastQuery = databaseBills.orderByKey().limitToLast(1);
    lastQuery.addListenerForSingleValueEvent(new ValueEventListener() {
        public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
            for (DataSnapshot childSnapshot : dataSnapshot.getChildren()) {
                String value = childSnapshot.child("current_units").getValue(String.class);
                Log.d("onDataChange", "current_units=" + value);
            }
        }

        @Override
        public void onCancelled(DatabaseError databaseError) {

        }
    });


    cur_units = (TextView) findViewById(R.id.cur_units);
    pre_units = (TextView) findViewById(R.id.pre_units);

    Intent i = getIntent();
    final double con = i.getDoubleExtra("Value", 0.0);
    cur_units.setText("" + con);

0 个答案:

没有答案