从内部方法访问变量到外部方法

时间:2018-04-18 10:05:22

标签: android

在android中我正在研究如何在方法中使用变量。当我以前返回变量时,它将值显示为0.我已将变量声明为全局变量。我们如何从内部方法访问外部方法的变量。在这里,我调用了 executeForm(String UID)

public class PaymentMethods extends AppCompatActivity  implements View.OnClickListener{
                    private PaymentsClient mPaymentsClient;
                    private  final static int LOAD_PAYMENT_DATA_REQUEST_CODE = 899;
                    float walletbal;
                 protected void onCreate(Bundle savedInstanceState) {
                        super.onCreate(savedInstanceState);
                        setContentView(R.layout.paymentmethods);


>  executeForm(mAuth.getUid());

// here method is calling
               System.out.println("walletbalance"+walletbal);
        }
    public float executeForm(String UID){
            Gson gson = new GsonBuilder()
                    .setLenient()
                    .create();

            Retrofit.Builder builder = new Retrofit.Builder()
                    .baseUrl("https://us-central1-deyapay-192704.cloudfunctions.net/balance/")
                    .addConverterFactory(GsonConverterFactory.create(gson));
            Retrofit retrofit =  builder.build();
            APIService apiservice=retrofit.create(APIService.class);
            Call<PostData> call=apiservice.bal(UID);
            call.enqueue(new Callback<PostData>() {
                @Override
                public void onResponse(Call<PostData> call, Response<PostData> response) {
                    if(response.isSuccessful()){
                        response.body();
                        String dp = response.body().getToken();
                        Log.d(TAG,dp);
                        //balance.setText(dp);
                    walletbal = Float.parseFloat(dp);

                        System.out.println("wall"+walletbal);



                    }
                    else
                    {

                    }
                    System.out.println("deya"+walletbal);


                }

                @Override
                public void onFailure(Call<PostData> call, Throwable t) {
                    t.printStackTrace();
                    Log.e(TAG,t.toString());

                }
            });
            System.out.print("dd"+walletbal);
            return walletbal;// here I am getting walletbal as 0
        }





    }

1 个答案:

答案 0 :(得分:1)

walletbal值始终为0,因为方法代码是逐行执行的,而您从onResponse()得到的实际值是在获得响应后得到的。所以你需要改变你的方法如下:

1)创建一个如下界面:

public interface GetWalletbalValue {
    void getValue(float walletbal);
    void onError(String errorMessage);
}

2)改变你的方法如下:

public void executeForm(String UID,GetWalletbalValue getWalletbalValue){
        Gson gson = new GsonBuilder()
                .setLenient()
                .create();

        Retrofit.Builder builder = new Retrofit.Builder()
                .baseUrl("https://us-central1-deyapay-192704.cloudfunctions.net/balance/")
                .addConverterFactory(GsonConverterFactory.create(gson));
        Retrofit retrofit =  builder.build();
        APIService apiservice=retrofit.create(APIService.class);
        Call<PostData> call=apiservice.bal(UID);
        call.enqueue(new Callback<PostData>() {
            @Override
            public void onResponse(Call<PostData> call, Response<PostData> response) {
                if(response.isSuccessful()){
                    response.body();
                    String dp = response.body().getToken();
                    Log.d(TAG,dp);
                    //balance.setText(dp);
                    walletbal = Float.parseFloat(dp);

                    System.out.println("wall"+walletbal);
                    getWalletbalValue.getValue(walletbal);
                }
                else
                {
                    // Failure message goes here
                }
                System.out.println("deya"+walletbal);   
            }

            @Override
            public void onFailure(Call<PostData> call, Throwable t) {
                t.printStackTrace();
                Log.e(TAG,t.toString());
                getWalletbalValue.onError(t.getLocalizedMessage());
            }
        });

    }

3)并从您的活动中调用以下方法:

executeForm(mAuth.getUid(), new GetWalletbalValue() {
            @Override
            public void getValue(float walletbal) {
                System.out.println("wall"+walletbal); //here you will get value
            }

           @Override
           public void onError(String errorMessage) {
            Toast.makeText(Main2Activity.this, errorMessage, Toast.LENGTH_SHORT).show();
        }
        });