在我的项目中,我定义了一个这样的界面:
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 !!!
任何建议将不胜感激。感谢
答案 0 :(得分:0)
您没有显示SearchResultsActivity()的代码,但它显然与MainActivity()不同。您看到的日志条目来自两个不同的活动,包含两个单独的变量。
答案 1 :(得分:0)
现在您已经更改了代码,现在看起来更清楚......
您是否尝试调试onPostExecute()?
如果您看到日志,则会清楚地表明它多次设置为false,但只设置为true一次。所以,调试你的onPostExecute()方法,主要是isComplete()函数....
我希望问题存在于那里。
希望这能帮助您解决问题。
...谢谢
Mr.777