title: How can calculate the value of variable at the first time on onClickListener event?
body: <p>I have an operation in which the value of a variable changes during the following process. The following code shows this operation.</p>
  
  <pre><code>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&lt;Posts&gt; posts) {
              if (posts == null || posts.isEmpty()) {
                  // There is no value on sever...
              } else {
                  for (int i = 0; i &lt; 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];
  }
  </code></pre>
  
  <p>The <code>checkOffCode</code> function receives a code, and returns a variable named <code>percent</code> if its value is equal to the value stored in the server.</p>
  
  <p>In the event <code>setOnClickListener</code> of the <code>btnPurchase</code> button, this value is called, and using of its value, the <code>price</code> variable is computed.
  The code for this section is as follows:</p>
  
  <pre><code>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 &gt;&gt;", String.valueOf(price));
      } else {
          Log.e("Failure &gt;&gt;", String.valueOf(price));
      }
  }
  });
  </code></pre>
  
  <p>The problem is that when I click on the <code>btnPurchase</code> button for the first time, the previous value of <code>percent</code> [percent = 0] is calculated in the operation, but when I click on the button for the second time, the variable <code>price</code> is calculated with the new <code>percent</code> value.</p>
  
  <p>The output <code>Log cat</code>  is shown in both first and second clicks respectively as follow:</p>
  
  <blockquote>
    <blockquote>
      <p>08-28 00:45:04.589 28467-28467/? E/Success >>: 125000</p>
      
      <p>08-28 00:45:11.425 28467-28467/? E/Success >>: 16000</p>
    </blockquote>
  </blockquote>
  
  <p><strong>The question is:</strong> How can I calculate the value of <code>price</code> with the new <code>percent</code> value at the first time?</p>