-1
package app.nepaliapp.bscfree;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.google.android.gms.tasks.OnSuccessListener;
import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.auth.FirebaseUser;
import com.google.firebase.firestore.DocumentSnapshot;
import com.google.firebase.firestore.FirebaseFirestore;

public class Buy extends AppCompatActivity {
    Button message, Monthly, Yearly;
    TextView textView1;
    FirebaseAuth mAuth;
    String userID, money1;
    public static String MyVariable;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_server);
        message = findViewById(R.id.Topup);
        Monthly = findViewById(R.id.monthly);
        Yearly = findViewById(R.id.yearly);
        textView1 = findViewById(R.id.amount);
        mAuth = FirebaseAuth.getInstance();
        userID = mAuth.getUid();
        FirebaseFirestore.getInstance().collection("users")
                .document(userID)
                .get()
                .addOnSuccessListener(new OnSuccessListener<DocumentSnapshot>() {
                    @Override
                    public void onSuccess(DocumentSnapshot documentSnapshot) {
                       MyVariable = documentSnapshot.get("money").toString();
                        textView1.setText(MyVariable);
                    }
                });



        message.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                gotoUrl("https://m.me/101442208871937");
            }

            private void gotoUrl(String s) {
                Uri uri = Uri.parse(s);
                startActivity(new Intent(Intent.ACTION_VIEW, uri));
            }
        });

        Monthly.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                int num;
                num = Integer.getInteger(MyVariable);
          if (num == 10 ){
        startActivity(new Intent(Buy.this,Actionable.class));
         }

            }
        });


        Yearly.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

            }
        });


    }

}

The main thing up here is that I want to create a new variable that will get the value from money1 in the upper code and want to use it in my entire activity. Which will help me reduce my read operation in firebase. As I need one for show points and another to subtract. So please help me, I didn't got the text in my money, So please help for This Thank you.

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807

1 Answers1

1

Create a global variable in your MainActivity, then you can use it (update and read) all over the app.

at MainActivity:

public class MainActivity extends AppCompatActivity {
     public String MyVariable;

if you need to reach it from another class (like another fragment), use

MainActivity mainActivity = ((MainActivity) getActivity());

if (mainActivity != null)
   mainActivity.MyVariable = "aaa"

or like in your examlple:

public class Buy extends AppCompatActivity {

Button message, Monthly, Yearly;
TextView textView1;
FirebaseAuth mAuth;
String userID, money1;
public static String MyVariable;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_server);
    message = findViewById(R.id.Topup);
    Monthly = findViewById(R.id.monthly);
    Monthly.Enabled(false);
    Yearly = findViewById(R.id.yearly);
    textView1 = findViewById(R.id.amount);
    mAuth = FirebaseAuth.getInstance();
    userID = mAuth.getUid();
    FirebaseFirestore.getInstance().collection("users")
            .document(userID)
            .get()
            .addOnSuccessListener(new OnSuccessListener<DocumentSnapshot>() {
                @Override
                public void onSuccess(DocumentSnapshot documentSnapshot) {
                    MyVariable = documentSnapshot.get("money").toString();
                textView1.setText(MyVariable);
                Monthly.Enabled(true);  

                Monthly.setOnClickListener(new View.OnClickListener() {
     
               @Override
               public void onClick(View view) {
               int num;
                num = Integer.getInteger(MyVariable);
                  if (num == 10 ){
                         startActivity(new Intent(Buy.this,Actionable.class));
                        }

                          }
                    });
                }
            });

    

    message.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            gotoUrl("https://m.me/101442208871937");
        }

        private void gotoUrl(String s) {
            Uri uri = Uri.parse(s);
            startActivity(new Intent(Intent.ACTION_VIEW, uri));
        }
    });

    
    


    Yearly.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {

        }
    });


}

}

Avital
  • 549
  • 5
  • 15
  • it gets into the firebase listener at all? – Avital Mar 13 '22 at 09:24
  • I fixed your code and added it to my answer, I also moved the set text inside the listener... – Avital Mar 13 '22 at 09:33
  • you can take ut out if you want, it is just for you to see the change in UI. it doesn't increase anything, you just change the text in UI – Avital Mar 13 '22 at 09:43
  • @SubashBhandari you are wrong: setting the TextView is just setting text to a UI component. the variable that hold the data is `MyVariable` and it can be acceced anywhere. setting the UI has nothing to do with it... – Avital Mar 13 '22 at 09:51
  • how and where do you try to access it? – Avital Mar 13 '22 at 10:03
  • Monthly.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { int num; num = Integer.getInteger(MyVariable); if (num == 10 ){ startActivity(new Intent(Buy.this,Actionable.class)); } } }); – Subash Bhandari Mar 13 '22 at 10:04
  • did you check that you really got MyVariable inside FireStore listener? – Avital Mar 13 '22 at 10:09
  • Yes, 100% their is – Subash Bhandari Mar 13 '22 at 10:17
  • post your complete code as it is now pls – Avital Mar 13 '22 at 10:20
  • @SubashBhandari can you debug your code at the 2 listeners (Firestore and onClick) and see what is the value of MyVariable on both? – Avital Mar 13 '22 at 11:10
  • null onclick and 10 in firestore – Subash Bhandari Mar 13 '22 at 11:38
  • maybe, when you click, firestore didn't finish yet reading and getting the vlaue. try to add toast or log inside the firestore listenr and click only after you get the notice that firestore finish – Avital Mar 13 '22 at 11:41
  • Nope, Even though it toast has seen but response is null value error due to null value in it – Subash Bhandari Mar 13 '22 at 11:49
  • I edited my code, added disable and enablr to Monthly button. now it can be clicked only after MyVariable is initialize – Avital Mar 13 '22 at 11:49
  • Yup i already did that in that function button will be enabled as data will set inside onSuccess but outside that there is null. That is the problem. So please help me take value outside that onSuccess will be a milestone. – Subash Bhandari Mar 13 '22 at 11:56
  • But if we put that enable button outside the onSuccess but will not be enabled – Subash Bhandari Mar 13 '22 at 11:56
  • ok... one more try: move yout onClick listener inside onSuccess, after you retreive MyVariable... I edited my code to match that – Avital Mar 13 '22 at 12:36