-2

I took value from Firebase database and showed it on Textview. I want to show that value in another Textview on same activity. How to Pass it? Here is my code

@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);
                pre_units.setText(value);

                Double convert = Double.parseDouble(pre_units.getText().toString());
                Intent intent = new Intent(getApplicationContext(), ShowUnits.class);
                intent.putExtra("PreValue", convert);

            } }

        @Override
        public void onCancelled(DatabaseError databaseError) {

        }
    });

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

    Intent i = getIntent();
    final double con = i.getDoubleExtra("Value", 0.0);
    final double pre = i.getDoubleExtra("PreValue", 0.0);

    cur_units.setText("" + con);
    textViewCheck.setText("" +pre);

https://i.stack.imgur.com/Fp5x8.png

Hemant N. Karmur
  • 840
  • 1
  • 7
  • 21
hesh
  • 1
  • 3
  • make `value` a class variable, so that you can access it outside the listener method as well. – Molly Mar 16 '19 at 06:44
  • Check **[this](https://stackoverflow.com/questions/47847694/how-to-return-datasnapshot-value-as-a-result-of-a-method/47853774)** out. – Alex Mamo Mar 16 '19 at 11:44

2 Answers2

0

Use the below code:I've just added two lines of code and removed one. check it out let me know, it resolves or not

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

        Intent intent = getIntent();
        //declare textView as final or globally
        final TextView textViewCheck = (TextView) findViewById(R.id.textViewCheck);
        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);
                    pre_units.setText(value);
                    //use this line of code  
                    textViewCheck.setText(String.format("%s", Double.parseDouble(value)));

                    Double convert = Double.parseDouble(pre_units.getText().toString());
                    Intent intent = new Intent(getApplicationContext(), ShowUnits.class);
                    intent.putExtra("PreValue", convert);

                } }

            @Override
            public void onCancelled(DatabaseError databaseError) {

            }
        });

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

        Intent i = getIntent();
        final double con = i.getDoubleExtra("Value", 0.0);
        final double pre = i.getDoubleExtra("PreValue", 0.0);

        cur_units.setText("" + con);
        textViewCheck.setText("" +pre);
  • I was checked, but this code is not resolve my problem. I want return DataSnapshot value as a result of a method outside of listener? – hesh Mar 18 '19 at 05:33
0

textViewCheck is a field of class. You can use it inside your listener.

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);
            pre_units.setText(value);

            Double convert = Double.parseDouble(value);
            textViewCheck.setText(convert.toString());
        }
}

Update

You can create field for value you need outside listener.

class MainActivity extends Activity {

    // Created a field
    private double currentUnits;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        lastQuery.addListenerForSingleValueEvent(new ValueEventListener() {
            public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
                for (DataSnapshot childSnapshot: dataSnapshot.getChildren()) {
                    String value = childSnapshot.child("current_units").getValue(String.class);
                    pre_units.setText(value);
                    // Update value
                    currentUnits = Double.parseDouble(value);
                }
            }

            @Override
            public void onCancelled(DatabaseError databaseError) {

            }
        });
   }

    // It is function outside listener that use currentUnits field
    void someFunctionThatUseValueWasRecivedFromListener() {
        double squaredValue = currentUnits * cucurrentUnits;
        textViewCheck.setText(String.valueOf(squaredValue ));
    } 
}
Andrew Churilo
  • 2,229
  • 1
  • 17
  • 26
  • I want it outside of listener. Because that value need to do calculation function – hesh Mar 16 '19 at 07:55
  • @hesh I've updated my answer to show how to use value outside of listener. – Andrew Churilo Mar 16 '19 at 11:08
  • But this code not working. I think error is in here void someFunctionThatUseValueWasRecivedFromListener {} – hesh Mar 18 '19 at 05:34
  • @hesh Yes, I forgot parentheses, sorry. But this code should not work, I've hid inconsiderable code. I show just main changes you need to do: create field, update field value inside listener and the usage of field outside listener. – Andrew Churilo Mar 18 '19 at 07:35
  • I havn't idea how to change. – hesh Mar 19 '19 at 04:53