Android的ListView的冻结,同时加载数据 [英] Android ListView freezes while loading data

查看:128
本文介绍了Android的ListView的冻结,同时加载数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的是自定义的列表视图,在我的应用程序图像 并从JSON URL加载的所有数据。

我创建了一个onscrollistener()

低于目前的数据,自动添加数据时,用户滚动到列表视图的底部。

当我的数据加载整个列表视图会停顿2-3秒。

我不知道什么是错?

这是我的$ C $下的AsyncTask

 私有类BackgroundLoadMore扩展的AsyncTask<虚空,虚空,虚空> {

          @覆盖
            在preExecute保护无效(){
                //显示进度对话框发送HTTP请求之前


            }

        保护无效doInBackground(空...未使用){
            runOnUiThread(新的Runnable(){
                公共无效的run(){

                    LoadData();


                            list.setOnScrollListener(新EndlessScrollListener());
                }
            });
            返程(空);
        }

        保护无效onPostExecute(作废不用){
            //在完成后台任务
            //关闭进度对话框等,。


        }
 

解决方案

您必须从URL中的主UI线程获取JSON数据。这将阻止在UI从由系统被更新,并且因此冻结。使用的AsyncTask 在后台做这样的网络任务。

LoadData()应该被称为在后台线程,这是AsyncTask的的doInBackground()。您的通话runOnUIThread把它背在UI线程上,那你不想要的。删除的AsyncTask的 runOnUIThread 电话。

 保护无效doInBackground(空...未使用){
        LoadData();

        返程(空);
    }

   保护无效onPostExecute(作废不用){
        //在完成后台任务
        //关闭进度对话框等,。

        list.setOnScrollListener(新EndlessScrollListener());

    }
 

i am using a custom listview with images in my app and loading all data from a json url.

i have created a onscrollistener()

which automatically add data below the current data when user scrolls to the bottom of the listview.

But when my data is loading whole listview freezes for 2-3 sec.

I dont know whats wrong??

here is my code for AsyncTask

private class BackgroundLoadMore extends AsyncTask<Void, Void, Void> {

          @Override
            protected void onPreExecute() {
                // Showing progress dialog before sending http request


            }

        protected Void doInBackground(Void... unused) {
            runOnUiThread(new Runnable() {
                public void run() {

                    LoadData();


                            list.setOnScrollListener(new EndlessScrollListener());
                }
            });
            return (null);
        }

        protected void onPostExecute(Void unused) {
            // On completing background task
            // closing progress dialog etc,.


        }

解决方案

You must be fetching the json data from url in main UI thread. This blocks the UI from being updated by system, and hence the freeze. Use AsyncTask to do such network tasks in background.

LoadData() should be called in a background thread, which is asynctask's doInBackground(). Your call runOnUIThread puts it back on the UI thread, and that you dont want. remove the runOnUIThread call from asynctask.

     protected Void doInBackground(Void... unused) {
        LoadData();

        return (null);
    }

   protected void onPostExecute(Void unused) {
        // On completing background task
        // closing progress dialog etc,.

        list.setOnScrollListener(new EndlessScrollListener());

    }

这篇关于Android的ListView的冻结,同时加载数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆