AsyncTask 未正确加载数据。

发布于 2025-01-02 04:09:06 字数 2917 浏览 1 评论 0原文

public class HomeActivity extends Activity{


//   public ArrayList<User> users1 = new ArrayList<User>();
    @Override
   public void onCreate(Bundle savedInstanceState) {
       super.onCreate(savedInstanceState);

      // ListView lv = (ListView) findViewById(R.id.user_crumbs_list);

    final ArrayList<User> users1 = new ArrayList<User>();  
    User user = null;
       class AsyncLoader extends AsyncTask<User,Void, ArrayList<User>> {
           ProgressDialog dialog;

        @Override

        protected void onPreExecute(){

               dialog = new ProgressDialog(HomeActivity.this); // App - your main activity class
               dialog.setMessage("Please, wait...");
               dialog.show();
        }

        @Override
        protected ArrayList<User> doInBackground(User... users) {
            String response = "";

            for (User user:users)

                try {

                  try {
                        user = new User("4eeb34c6d80e8f1214000000");
                        user.getFollowingCrumbsUpList();
                    } catch (IOException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    } catch (JSONException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }





                    for(User u : user.following){
                        users1.add(u);
                    }



                } catch (Exception e) {
                    e.printStackTrace();
                }

        return users1;
        }

        protected void onPostExecute(String result) {
            dialog.dismiss();
            return;

        }


        }






        AsyncLoader task = new AsyncLoader();
        task.execute(user);



        setContentView(R.layout.user_main_tab_home);
        final UserFollowingListAdapter csl = new          UserFollowingListAdapter(this,R.layout.user_list_item,users1,this);
        ListView lv = (ListView) findViewById(R.id.user_list);          









       public void showError(){
        new AlertDialog.Builder(this)
        .setTitle(" Oops , Server down :( ")
        .setPositiveButton("Ok", new DialogInterface.OnClickListener() {

            @Override
            public void onClick(DialogInterface arg0, int arg1) {
                // TODO Auto-generated method stub

            }
            //
        }).setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int whichButton) {
                // Do nothing.
            }
        }).show();

      }
}

这是一个无限循环,数据根本不加载,并且对话框会无限循环,直到我单击它。我的目标是加载一些初始数据量,然后根据用户滚动获取数据。截至目前,我什至无法显示数据。

调试后,我发现 user1 变量没有存储任何值,即使迭代正在进行,我也无法找出原因。

关于我哪里出错的任何线索?

public class HomeActivity extends Activity{


//   public ArrayList<User> users1 = new ArrayList<User>();
    @Override
   public void onCreate(Bundle savedInstanceState) {
       super.onCreate(savedInstanceState);

      // ListView lv = (ListView) findViewById(R.id.user_crumbs_list);

    final ArrayList<User> users1 = new ArrayList<User>();  
    User user = null;
       class AsyncLoader extends AsyncTask<User,Void, ArrayList<User>> {
           ProgressDialog dialog;

        @Override

        protected void onPreExecute(){

               dialog = new ProgressDialog(HomeActivity.this); // App - your main activity class
               dialog.setMessage("Please, wait...");
               dialog.show();
        }

        @Override
        protected ArrayList<User> doInBackground(User... users) {
            String response = "";

            for (User user:users)

                try {

                  try {
                        user = new User("4eeb34c6d80e8f1214000000");
                        user.getFollowingCrumbsUpList();
                    } catch (IOException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    } catch (JSONException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }





                    for(User u : user.following){
                        users1.add(u);
                    }



                } catch (Exception e) {
                    e.printStackTrace();
                }

        return users1;
        }

        protected void onPostExecute(String result) {
            dialog.dismiss();
            return;

        }


        }






        AsyncLoader task = new AsyncLoader();
        task.execute(user);



        setContentView(R.layout.user_main_tab_home);
        final UserFollowingListAdapter csl = new          UserFollowingListAdapter(this,R.layout.user_list_item,users1,this);
        ListView lv = (ListView) findViewById(R.id.user_list);          









       public void showError(){
        new AlertDialog.Builder(this)
        .setTitle(" Oops , Server down :( ")
        .setPositiveButton("Ok", new DialogInterface.OnClickListener() {

            @Override
            public void onClick(DialogInterface arg0, int arg1) {
                // TODO Auto-generated method stub

            }
            //
        }).setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int whichButton) {
                // Do nothing.
            }
        }).show();

      }
}

This is going on an infinite loop where the data doesn't load at all and the dialog goes on an infinite loop till I click on it. My goal is to load some initial amount of data and then fetch data as per the user scroll. As of now, I'm not even able to display the data.

After debugging I found out that user1 variable has no values stored in it, which I'm not able to find out why, even though the iteration is taking place.

Any clues as to where I am going wrong?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

美男兮 2025-01-09 04:09:06

我真的无法理解您尝试要做的事情,但问题可能在于,在您调用 task.execute(user); 时,user 为空。因此,AsyncTask 中的 for (User user:users) 没有可迭代的项目。

I really can't follow what you're trying to do, but the problem likely lies in the fact that, at the time you call task.execute(user);, user is null. So the for (User user:users) in your AsyncTask has no items to iterate over.

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