-1

Greeting all, I am having the hard time to set int value for a variable in ValueEventListener and get that value outside ValueEventListener for the method to checking is value == 1, the method will return 1, else return 0

I have tried many way like save the value via SharedPreferences, set value to Textview and call from textview, but still, the method always return 0 as it cannot read the value that have been set in ValueEventListener. Any help are much appreciate. Thank you

Here my code

int status = 0;
protected void onCreate(Bundle savedInstanceState) {
    ...

    btnSync_in.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            if (getCustomerList() == 1) {
                msg = msg + "Get customer success \n";
            } else {
                msg = msg + "Get customer unsuccessful \n";
            }

            AlertDialog.Builder statusDialog = new AlertDialog.Builder(SyncActivity.this);
            statusDialog.setPositiveButton("OK", null);
            statusDialog.setTitle("Status");
            statusDialog.setMessage(msg);
            statusDialog.show();
            msg = "";
        }
    });
}

The method that will return the int

private int getCustomerList() {
    urlRef = myRef.child("...");
    urlRef.addListenerForSingleValueEvent(new ValueEventListener() {
        @Override
        public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
            ...

            StringRequest stringRequest = new StringRequest(Request.Method.GET, customerURL, new Response.Listener<String>() {
                @Override
                public void onResponse(String response) {
                    status = 1; //here my problem, value assign here is success

                    try {
                        ...
                        }
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                }
            }, new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {
                    ...
                    status = 0; // here if unsuccessful

                }
            });
            RequestQueue requestQueue = Volley.newRequestQueue(SyncActivity.this);
            requestQueue.add(stringRequest);
        }

        @Override
        public void onCancelled(@NonNull DatabaseError databaseError) {
            ...
        }
    });

    if (status == 1) {
        return 1;
    } else {
        return 0;
    }
}
Tanveer Munir
  • 1,956
  • 1
  • 12
  • 27
  • 2
    Another victim of Asynchronous programming. `ValueEventListener` is an Asynchronous call you need to check status inside callback methods . – ADM Feb 20 '19 at 06:19

1 Answers1

0

Even better way why dont't you display your AlertDialog inside getCustomerList() method

Call your getCustomerList() like this

btnSync_in.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            getCustomerList();

        }
    });

Then add your AlertDialog inside getCustomerList() like this

SAMPLE CODE

 private void getCustomerList() {


    AlertDialog.Builder statusDialog = new AlertDialog.Builder(SyncActivity.this);
    statusDialog.setPositiveButton("OK", null);
    statusDialog.setTitle("Status");

    urlRef = myRef.child("...");
    urlRef.addListenerForSingleValueEvent(new ValueEventListener() {
        @Override
        public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
            ...

            StringRequest stringRequest = new StringRequest(Request.Method.GET, customerURL, new Response.Listener<String>() {
                @Override
                public void onResponse(String response) {
                    msg = msg + "Get customer success \n";

                     statusDialog.setMessage(msg);
                     statusDialog.show();
                     msg = "";


                    try {
                        ...
                        }
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                }
            }, new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {
                    ...
                    msg = msg + "Get customer unsuccessful \n";
                    statusDialog.setMessage(msg);
                    statusDialog.show();
                    msg = "";

                }
            });
            RequestQueue requestQueue = Volley.newRequestQueue(SyncActivity.this);
            requestQueue.add(stringRequest);
        }

        @Override
        public void onCancelled(@NonNull DatabaseError databaseError) {
            ...
        }
    });


}
AskNilesh
  • 67,701
  • 16
  • 123
  • 163
  • Thanks @Nilesh Rathod.. This method work perfectly but it show 10 times of AlertDialog. What I want is to show only one AlertDialog that show list for all method that was success or unsuccessful. I have about 10 method that will do the same(customer, production item, etc..) – ArnabBuluss Feb 21 '19 at 03:20