AsyncTask无法正确加载数据。

时间:2012-02-03 14:02:33

标签: android android-asynctask

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变量没有存储在其中的值,即使迭代正在进行,我也无法找到原因。

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

1 个答案:

答案 0 :(得分:1)

我真的无法关注你尝试做什么,但问题可能在于,当你致电task.execute(user);时,user一片空白。因此,AsyncTask中的for (User user:users)没有要迭代的项目。