在 ListActivity 中加载项目会阻止应用程序

发布于 2024-11-03 14:39:07 字数 1871 浏览 4 评论 0原文

好吧,我正在制作一个应用程序,其中使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

失而复得 2024-11-10 14:39:07

不要从 onPostExecute() 调用 publishProgress()。对于初学者来说,无论如何,直到 onPostExecute() 完成之后进度才会改变,因为 onProgressUpdate() 需要在位于中间的主应用程序线程上调用运行 onPostExecute() 逻辑。

如果您仍然觉得运行速度太慢,我建议您使用 Traceview 或简单的日志记录语句来准确确定问题所在。

Do not call publishProgress() from onPostExecute(). For starters, the progress will not change until after onPostExecute() completes, anyway, since onProgressUpdate() needs to be called on the main application thread, which is in the middle of running your onPostExecute() 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文