0

How can I change textview in another class with storageCointainer method? I want to get data from storageCointainer method to change textview in HomeFragment class.

public void getSavedMoney() {
    databaseReference.addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            for (DataSnapshot ds : dataSnapshot.getChildren()) {
                String money = ds.child("saveMoney").getValue().toString();
                moneyStringList.add(money);
            }
            moneyNumList = Lists.transform(moneyStringList, Double::parseDouble);
            savedMoney = moneyNumList.stream().mapToDouble(Double::doubleValue).sum();
            storageContainer(savedMoney);
        }

        @Override
        public void onCancelled(DatabaseError databaseError) {

        }
    });
}

public void storageContainer(double savedMoney) {
    this.savedMoney = savedMoney;
}
Artur
  • 13
  • 2
  • Use the callback pattern. `HomeFragment` should implement an interface that has a method for passing back your data. `storageContainer` should then call `HomeFragment`'s instance of that method, which should in turn update your `TextView`. – codebod Jun 15 '22 at 20:43

3 Answers3

1

You can pass the value by using Bundle. You can refer to this link https://stackoverflow.com/a/9245510/9346054

For you case, you can create the instance like below.

public static HomeFragment newInstance(final Double savedMoney) {
    final HomeFragment homeFragment = new HomeFragment();
    final Bundle args = new Bundle();
    args.putDouble("savedMoney", savedMoney);
    homeFragment.setArguments(args);
    return homeFragment;
}

Next, implement it at the storageContainer method like this.

public void storageContainer(double savedMoney) {
    this.savedMoney = savedMoney;
    newInstance(savedMoney);
}

At the HomeFragment, you can get the value at onCreate. Example as below

final Double savedMoney = getArguments().getDouble("savedMoney", 0);
//Display at the textView
tvSavedMoney.setText("Total: " + savedMoney);
Ticherhaz FreePalestine
  • 2,738
  • 4
  • 20
  • 46
1

If you want to send the value savedMoney to another activity, you should consider sending it only when it's ready. A typical way of sending the data would be through an Intent. So in code should look like this:

public void getSavedMoney() {
    databaseReference.addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            for (DataSnapshot ds : dataSnapshot.getChildren()) {
                String money = ds.child("saveMoney").getValue().toString();
                moneyStringList.add(money);
            }
            moneyNumList = Lists.transform(moneyStringList, Double::parseDouble);
            savedMoney = moneyNumList.stream().mapToDouble(Double::doubleValue).sum();
            //storageContainer(savedMoney); // removed

            Intent intent = new Intent(CurrentActivity.this, NextActivity.class);
            intent.putExtras("savedMoney", savedMoney);
            startActivity(intent);            
        }

        @Override
        public void onCancelled(DatabaseError databaseError) {
            Log.d("TAG", error.getMessage()); //Never ignore potential errors!
        }
    });
}

And to get it back inside the next activity, simply use:

double savedMoney = getIntent().getExtras().getDouble("savedMoney");
Log.d("TAG", "savedMoney = " + savedMoney);

If you understand Kotlin, I recommend you read the following resource:

Where I have explained three ways in which we can get data from Firebase Realtime Database using the get() method.

Alex Mamo
  • 130,605
  • 17
  • 163
  • 193
0

If both your activities are not directly related, you can try to use BroadcastManager to send intents across.

sean
  • 63
  • 7