为什么我的进度条不能正常增加?
我试图在收到的广播上增加进度条,但从我的角度来看, myProgressDialog.incrementProgressBy(increment); 代码没有任何效果。我尝试将命令放置在不同的地方,但我仍然看不到任何效果。
这是我的代码的样子。
public static final int max = 180;
public final static int increment = (1/180);
@Override
public void onCreate(Bundle savedInstanceState) {
...
i = new Intent();
i.setAction(ITEM_CREATED);
registerReceiver(myBroadcastReceiver, new IntentFilter(ITEM_CREATED));
findFeeds = new Runnable(){
@Override
public void run()
{
getFeedObjects();
}
};
beginThread();
}
public static Context getAppContext() {
return context;
}
private void beginThread()
{
switch (checkConnectionState(OffsideLiteActivity.this))
{
case 0:
thread = new Thread(null, findItems, "DoingInBackground");
thread.start();
myProgressDialog = new ProgressDialog(this);
myProgressDialog.setCancelable(true);
myProgressDialog.setMessage("Loading...");
myProgressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
myProgressDialog.setProgress(0);
myProgressDialog.setMax(max);
myProgressDialog.show();
break;
case 1:
connectionError();
break;
default:
}
}
private BroadcastReceiver myBroadcastReceiver =
new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent)
{
myProgressDialog.incrementProgressBy(increment);
Log.d("RECEIVED", "Broadcast received");
}
};
有趣的是,logcat 注册了我在 myProgressDialog.incrementProgressBy(increment);
代码
logcat
之后放置的 Broadcast receive
日志
08-03 13:56:15.109: DEBUG/RECEIVED(8666): Broadcast received
08-03 13:56:15.109: DEBUG/RECEIVED(8666): Broadcast received
08-03 13:56:15.109: DEBUG/RECEIVED(8666): Broadcast received
08-03 13:56:15.109: DEBUG/RECEIVED(8666): Broadcast received
08-03 13:56:15.109: DEBUG/RECEIVED(8666): Broadcast received
就像我说的,我尝试将myProgressDialog.incrementProgressBy(increment);
在我的代码中的其他地方命令,但无济于事。有什么建议吗?
I am trying to increment my progress bar on a broadcast recieved but the myProgressDialog.incrementProgressBy(increment);
code has no effect from my point of view. I have tried placing the command in different places but I still can see no effect at all.
Here is what my code looks like.
public static final int max = 180;
public final static int increment = (1/180);
@Override
public void onCreate(Bundle savedInstanceState) {
...
i = new Intent();
i.setAction(ITEM_CREATED);
registerReceiver(myBroadcastReceiver, new IntentFilter(ITEM_CREATED));
findFeeds = new Runnable(){
@Override
public void run()
{
getFeedObjects();
}
};
beginThread();
}
public static Context getAppContext() {
return context;
}
private void beginThread()
{
switch (checkConnectionState(OffsideLiteActivity.this))
{
case 0:
thread = new Thread(null, findItems, "DoingInBackground");
thread.start();
myProgressDialog = new ProgressDialog(this);
myProgressDialog.setCancelable(true);
myProgressDialog.setMessage("Loading...");
myProgressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
myProgressDialog.setProgress(0);
myProgressDialog.setMax(max);
myProgressDialog.show();
break;
case 1:
connectionError();
break;
default:
}
}
private BroadcastReceiver myBroadcastReceiver =
new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent)
{
myProgressDialog.incrementProgressBy(increment);
Log.d("RECEIVED", "Broadcast received");
}
};
The interesting part is that logcat registers the Broadcast received
log that i placed after the myProgressDialog.incrementProgressBy(increment);
code
logcat
08-03 13:56:15.109: DEBUG/RECEIVED(8666): Broadcast received
08-03 13:56:15.109: DEBUG/RECEIVED(8666): Broadcast received
08-03 13:56:15.109: DEBUG/RECEIVED(8666): Broadcast received
08-03 13:56:15.109: DEBUG/RECEIVED(8666): Broadcast received
08-03 13:56:15.109: DEBUG/RECEIVED(8666): Broadcast received
Like I said I have tried putting the myProgressDialog.incrementProgressBy(increment);
command elsewhere in my code, but to no avail. Any suggestion?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
问题可能出在这一行:
我相信 int 会被截断为 0,因此您只需将进度对话框增加 0。您可能希望增量为 1。
The problem might be this line:
I believe that int will be truncated to 0, so you're only ever incrementing the progress dialog by 0. You probably want increment to be 1.
与其使用广播接收器来更新你的 UI,你不能只使用类似观察者可观察模式的东西吗?然后侦听器将在 UI 线程中运行 UI 更新,如下所示:
或者如果你想要更现代的东西,请使用 AsyncTask并仍然在 UI 线程中运行更新。
问候,
史蒂芬
Instead of using a broadcastreceiver to update your UI, couldn't you just use something like an obser-observable pattern, the listener would then run the UI update in the UI thread like this :
or if you want something more modern, use an AsyncTask and still run the update in the UI Thread.
Regards,
Stéphane
尝试将调试点放在 myProgressDialog.incrementProgressBy() 处......增量的值是多少?我的猜测是它可能是 0。或者将您的日志更改为:
Try putting a debug point right at myProgressDialog.incrementProgressBy().... what is the value of increment? My guess is that it could be 0. Or change your Log to: