在我的项目中,我定义了一个这样的接口:
public interface SearchResultProgressListener {
public void OnSearchCompleted(boolean value);
}
我在 A 类(称为 SearchResultsActivity)中实现了这个:
public class SearchResultsActivity extends BaseActivity implements
SearchResultProgressListener {
private ProgressBar mProgressBar;
private boolean timerStatus = false;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_searchresults);
// Linking progressbar
mProgressBar = (ProgressBar) findViewById(R.id.progressBar_fake);
new CountDownTimer(10000, 1000) {
public void onTick(long millisUntilFinished) {
int temp = (int) millisUntilFinished / 1000;
switch(temp) {
case 9: temp = 1; break;
case 8: temp = 2; break;
case 7: temp = 3; break;
case 6: temp = 4; break;
case 5: temp = 5; break;
case 4: temp = 6; break;
case 3: temp = 7; break;
case 2: temp = 8; break;
case 1: temp = 9; break;
case 0: temp = 10; break;
}
Log.e("Hesam", "Status1: " + timerStatus);
if(!timerStatus)
mProgressBar.setProgress(temp);
else
mProgressBar.setProgress(10);
}
public void onFinish() {
mProgressBar.setVisibility(View.GONE);
}
}.start();
}
@Override
public void OnSearchCompleted(boolean status) {
this.timerStatus = status;
Log.e("Hesam", "Status0: " + timerStatus);
}
}
我有 B 类(称为 HotelsAsyncTaskFragment),我想通过这个类将布尔值发送到 A 类。
public class HotelsAsyncTaskFragment extends SherlockFragment {
private SearchResultProgressListener srpListener;
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
srpListener = (SearchResultProgressListener) new SearchResultsActivity();
}
class HotelsSearchTask extends AsyncTask<Void, Void, Void> {
@Override
protected void onPreExecute() {...}
@Override
protected Void doInBackground(Void... params) {...}
@Override
protected void onPostExecute(Void result) {
if(!isComplete()) {
HotelsAsyncTaskFragment.this.srpListener.OnSearchCompleted(false);
} else {
HotelsAsyncTaskFragment.this.srpListener.OnSearchCompleted(true);
}
}
}
}
现在,我希望当我将 A 类中的“timerStatus”设置为“True”时,它的内容会发生变化,但不会。这是我的日志:
02-13 16:41:58.716: E/Hesam(469): Status1: false
02-13 16:42:13.566: E/Hesam(469): Status1: false
02-13 16:42:14.566: E/Hesam(469): Status1: false
02-13 16:42:14.611: E/Hesam(469): Status0: true
02-13 16:42:15.571: E/Hesam(469): Status1: false
02-13 16:42:16.601: E/Hesam(469): Status1: false
02-13 16:42:17.616: E/Hesam(469): Status1: false
02-13 16:42:18.616: E/Hesam(469): Status1: false
02-13 16:42:19.621: E/Hesam(469): Status1: false
02-13 16:42:20.626: E/Hesam(469): Status1: false
02-13 16:42:21.621: E/Hesam(469): Status1: false
如您所见,“timerStatus”在第四行设置为“true”,但之后我不知道为什么它设置为 false!
任何建议将不胜感激。谢谢