Search code examples
javaandroidandroid-asynctaskonclicklistener

How can calculate the value of variable at the first time on onClickListener event?


I have an operation in which the value of a variable changes during the following process. The following code shows this operation.

private long checkOffCode(String pChode) {
    final long[] percent = {0};
    APIGettingPosts apiGettingPosts = new APIGettingPosts(MainActivity.this, "get_off_code.php");
    apiGettingPosts.getOffCode(new APIGettingPosts.OnOffCodeReceived() {
        @Override
        public void onReceived(List<Posts> posts) {
            if (posts == null || posts.isEmpty()) {
                // There is no value on sever...
            } else {
                for (int i = 0; i < posts.size(); ++i) {

                    // If the code in serve is equal with pCode],
                    // change the price value....

                    if (pChode.equals(posts.get(i).getCode())) {
                        percent[0] = Long.valueOf(posts.get(i).getPercent());
                    }
                }
            }
        }
    });

    return percent[0];
}

The checkOffCode function receives a code, and returns a variable named percent if its value is equal to the value stored in the server.

In the event setOnClickListener of the btnPurchase button, this value is called, and using of its value, the price variable is computed. The code for this section is as follows:

btnPurchase.setOnClickListener(new View.OnClickListener(){
    long percent = 0;
    @Override
    public void onClick (View view){

    percent = checkOffCode("MYCODE");
    if (percent != 0) {
        // update price value...
        price = price - price * percent / 100;
        Log.e("Success >>", String.valueOf(price));
    } else {
        Log.e("Failure >>", String.valueOf(price));
    }
}
});

The problem is that when I click on the btnPurchase button for the first time, the previous value of percent [percent = 0] is calculated in the operation, but when I click on the button for the second time, the variable price is calculated with the new percent value.

The output Log cat is shown in both first and second clicks respectively as follow:

08-28 00:45:04.589 28467-28467/? E/Success >>: 125000

08-28 00:45:11.425 28467-28467/? E/Success >>: 16000

The question is: How can I calculate the value of price with the new percent value at the first time?


Solution

  • There is no relation with whether you click the button first or second time. The problem is that you try to get the return value from checkOffCode directly without being sure about that onReceived has been called. You can change the code like this:

    private void checkOffCode(String pChode) {
        final long[] percent = {0};
        APIGettingPosts apiGettingPosts = new APIGettingPosts(MainActivity.this, "get_off_code.php");
        apiGettingPosts.getOffCode(new APIGettingPosts.OnOffCodeReceived() {
            @Override
            public void onReceived(List<Posts> posts) {
                if (posts == null || posts.isEmpty()) {
                    // There is no value on sever...
                } else {
                    for (int i = 0; i < posts.size(); ++i) {
    
                        // If the code in serve is equal with pCode],
                        // change the price value....
    
                        if (pChode.equals(posts.get(i).getCode())) {
                            percent[0] = Long.valueOf(posts.get(i).getPercent());
                        }
                    }
                }
                if (percent[0] != 0) {
                    // update price value...
                    price = price - price * percent[0] / 100;
                    Log.e("Success >>", String.valueOf(price));
                } else {
                    Log.e("Failure >>", String.valueOf(price));
                }
            }
        });
    }
    
    btnPurchase.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            checkOffCode("MYCODE");
        }
    });