我没有像我认为的那样成功地做到这一点,所以我会问专家。
我有一个链接到图像的十个URL的ArrayList。我想显示第一个URL 2秒,然后获取第二个URL并执行相同的操作直到结束。
这是我到目前为止的情况,我想也许我不会在postExecute中以最佳方式进行对话?:
private class LiveView extends AsyncTask<String, Integer, ArrayList<String>> {
ProgressDialog dialog;
private volatile boolean running = true;
@Override
protected void onPreExecute() {
dialog = ProgressDialog.show(
myView.this,
"Working",
"Info message . . .",
true,
true,
new DialogInterface.OnCancelListener(){
public void onCancel(DialogInterface dialog) {
cancel(true);
}
}
);
}
@Override
protected void onCancelled() {
running = false;
}
@Override
protected ArrayList<String> doInBackground(String... passed) {
while (running) {
//removed the code here that sends the request to to make this shorter the server but it works fine
return responseFromServer.arrayListofURLs; //list or URLs
}
return null;
}
@Override
protected void onPostExecute(ArrayList<String> listURLs) {
dialog.cancel();
Dialog liveView = new Dialog(myView.this, R.style.Dialog);
liveView.setContentView(R.layout.liveview_dialogue);
TextView title = (TextView)liveView.findViewById(R.id.liveViewTitle);
Button button = (Button) liveView.findViewById(R.id.liveViewButton);
ImageView trackImage = (ImageView)liveView.findViewById(R.id.liveViewImage);
//I want to loop through the ten images here?
button.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
}
});
liveView.show();
}
}
答案 0 :(得分:1)
创建处理程序,并通过postDelayed向其发送延迟2秒的消息。每当收到消息时,通过调用trackImage.setImage显示下一个图像。当他们最终关闭对话框时,从处理程序中删除所有待处理的消息。
答案 1 :(得分:0)
要使用一些代码完成答案,以下是我使用处理程序的方法。我不需要传递任何变量,因为我为AsyncTask创建了全局位图的ListArray。如果对话框已关闭,我还使用布尔值来结束处理程序。
liveView = new Dialog(myView.this, R.style.Dialog);
liveView.setContentView(R.layout.liveview_dialogue);
TextView title = (TextView)liveView.findViewById(R.id.liveViewTitle);
Button button = (Button) liveView.findViewById(R.id.liveViewButton);
trackImage = (ImageView)liveView.findViewById(R.id.liveViewImage);
button.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
run = false;
liveView.dismiss();
}
});
liveView.show();
final Handler handler = new Handler();
final Runnable r = new Runnable()
{
Iterator<Bitmap> it = images.iterator();
public void run()
{
if(run){
trackImage.setImageBitmap(it.next());
if(it.hasNext())
handler.postDelayed(this, 5000);
}
}
};
handler.postDelayed(r, 5000);