在 ListActivity 中加载项目会阻止应用程序
好吧,我正在制作一个应用程序,其中使用 ListActivity 来显示包含从 Internet 下载的图像的视图以及 TextView 和评级栏。 我添加了一个 AsyncTask 来管理它,并将我的 setListAdapter 放在 onPostExecute() 部分;但是,我还希望在设置完成时显示进度条(或 Progressdialog )。
该栏确实在我的模拟器中出现了一段时间,但当我在设备上尝试我的应用程序时却没有出现,即使在模拟器中,当 setListAdapter 工作时该栏也会冻结,但是......只是我还没有弄清楚如何在我的 ArrayAdapter 上添加publishProgress,因为它是另一个类。 啊,是的,我设置了 ListActivity 的布局,这就是我放置进度条的位置,但我猜该设备使 doInBackground 部分变得相当快,因此布局甚至不显示。
这是我到目前为止所得到的:
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
extras = getIntent().getExtras();
platinumCinema = false;
setContentView(R.layout.simple_list_item_1);
Log.i(TAG,"Control B");
MoviesAsyncTask movieTask = new MoviesAsyncTask();
movieTask.execute();
getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.window_title_billboard);
}
@Override
protected void onPreExecute() {
super.onPreExecute();
mProgress = (ProgressBar)findViewById(R.id.progressbar_Horizontal);
mProgress.setVisibility(View.VISIBLE);
Log.i(TAG, "PreExecute");
}
@Override
protected void onPostExecute(String result) {
super.onPostExecute(result);
if(result.equals("ok"))
Log.i(TAG,"Ended succesfully");
//ProgressDialog mDialog = ProgressDialog.show(BillboardMovieList.this, "","Loading. Please wait...", true);
publishProgress(75);
if(searchMethod.equals("byCinema")){
setListAdapter(new ArrayAdapterSchemaCinemas(BillboardMovieList.this, listMovies,listPosters, listRating, listTimes));
}else
...
Well, I'm making an app in which I'm using a ListActivity to display views that have images that I download from the Internet as well as TextViews and rating bars.
I've added an AsyncTask to manage this and have my setListAdapter in the onPostExecute() part; however, I also wanted a progressbar (or progressdialog )to be shown while the setting was done.
The bar does appear for a while in my emulator, but not when I try my app on a device, and even in the emulator, the bar freezes when the setListAdapter is working but...it's just that I haven't figured out how to add the publishProgress on my ArrayAdapter since it's another class.
Ah yes, I set the layout for the ListActivity and that's where I put the progressBar, but the device I guess makes the doInBackground part quite fast so that layout doesn't even show.
This is what I've got so far:
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
extras = getIntent().getExtras();
platinumCinema = false;
setContentView(R.layout.simple_list_item_1);
Log.i(TAG,"Control B");
MoviesAsyncTask movieTask = new MoviesAsyncTask();
movieTask.execute();
getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.window_title_billboard);
}
@Override
protected void onPreExecute() {
super.onPreExecute();
mProgress = (ProgressBar)findViewById(R.id.progressbar_Horizontal);
mProgress.setVisibility(View.VISIBLE);
Log.i(TAG, "PreExecute");
}
@Override
protected void onPostExecute(String result) {
super.onPostExecute(result);
if(result.equals("ok"))
Log.i(TAG,"Ended succesfully");
//ProgressDialog mDialog = ProgressDialog.show(BillboardMovieList.this, "","Loading. Please wait...", true);
publishProgress(75);
if(searchMethod.equals("byCinema")){
setListAdapter(new ArrayAdapterSchemaCinemas(BillboardMovieList.this, listMovies,listPosters, listRating, listTimes));
}else
...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
不要从
onPostExecute()
调用publishProgress()
。对于初学者来说,无论如何,直到onPostExecute()
完成之后进度才会改变,因为onProgressUpdate()
需要在位于中间的主应用程序线程上调用运行onPostExecute()
逻辑。如果您仍然觉得运行速度太慢,我建议您使用 Traceview 或简单的日志记录语句来准确确定问题所在。
Do not call
publishProgress()
fromonPostExecute()
. For starters, the progress will not change until afteronPostExecute()
completes, anyway, sinceonProgressUpdate()
needs to be called on the main application thread, which is in the middle of running youronPostExecute()
logic.If you still feel things are running too slowly, I would recommend that you perhaps use Traceview or simple logging statements to identify exactly where the problem is.