![](/img/trans.png)
[英]How can I assign Boolean Value true or false to the string “Open” and “Close”
[英]i assign true to a boolean value named “ready” but when i debug i saw it takes false in onResourceReady function
當我單擊按鈕時,布爾值將為false,直到從Glide准備好資源為止,所以我分配了一個真值,但仍為false我在代碼下面進行了調試的屏幕截圖,對於第二個屏幕截圖,最后一行顯示已准備就緒已經為false並且無法進入if(ready)
public void onClick(View view) {
ready = false;
if(index < totalQuestions){
AppCompatButton clickedButton = (AppCompatButton) view;
if (clickedButton.getText().equals(Common.listQuestions.get(index).getCorrectAnswer()))
{
correct.start();
clickedButton.getBackground().setColorFilter(0xFF00FF00, PorterDuff.Mode.SRC);
//clickedButton.setTextColor(Color.CYAN);
correctAnswer++;
score+=10;
stopProgress();
stopSound();
//*********************
RequestListener rl = new RequestListener() {
@Override
public boolean onLoadFailed(@Nullable GlideException e, Object model, Target target, boolean isFirstResource) {
Log.e("GLIDE","GLIDE LOAD FAILED");
Toast.makeText(PlayGame.this, "LOADING IMAGE FAILED", Toast.LENGTH_SHORT).show();
ready = true;
mProgressBar.setIndeterminate(false);
return true;
}
@Override
public boolean onResourceReady(Object resource, Object model, Target target, DataSource dataSource, boolean isFirstResource) {
mProgressBar.setIndeterminate(false);
Toast.makeText(PlayGame.this, "ready", Toast.LENGTH_SHORT).show();
ready = true;
return true;
}
};
我只是稍微修改一下代碼以確保更改布爾變量..但布爾變量仍然得到了錯誤的值。。`RequestListener rl = new RequestListener(){
@Override
public boolean onLoadFailed(@Nullable GlideException e, Object model, Target target, boolean isFirstResource) {
Log.e("GLIDE","GLIDE LOAD FAILED");
Toast.makeText(PlayGame.this, "LOADING IMAGE FAILED", Toast.LENGTH_SHORT).show();
ready = true;
mProgressBar.setIndeterminate(false);
return true;
}
@Override
public boolean onResourceReady(Object resource, Object model, Target target, DataSource dataSource, boolean isFirstResource) {
ready = true;
Log.e("GLIDE",ready+"");
mProgressBar.setIndeterminate(false);
Toast.makeText(PlayGame.this, ready+"", Toast.LENGTH_SHORT).show();
if (ready) {
// Toast.makeText(this, ready+"", Toast.LENGTH_SHORT).show();
Handler handler = new Handler();
handler.postDelayed(new Runnable() {
public void run() {
showQuestion(++index);
}
}, 3000);
}
return true;
}
};`
1. 調試屏幕截圖
2. 調試第二屏
我無法發表評論,所以我不得不回答你的問題
主要問題是RequestListener
是一個Callback,將其設置為Glide后將不會立即調用,因此您的代碼將逐行執行,因此您的代碼將運行如下:
ready
設置為false 2.if語句將運行,並在其中創建了RequestListener
的實例
3,在幾行之后,您已經將監聽器設置為Glide
4.至少您已經檢查了if(ready) ...
在第一步中, ready
設置為false,而在步驟4中,它仍為false,因為RequestListener
方法將在步驟4之后運行(取決於它在做什么),因此在代碼末尾為false。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.