0

我正在从 facebook 获取朋友列表并在列表视图中填充它工作正常但现在我正在尝试添加进度条直到列表视图填充但出现异常我已经尝试搜索但没有找到我的问题的解决方案,这是我的代码:

public class FriendsProgress extends AsyncTask<Object, Void, Boolean>
    {
        @Override
        protected Boolean doInBackground(Object... Params)
        {
            try
            {
                getFriendList();                                                                    
                friendAdapter = new FriendAdapter(Friends.this, R.layout.activity_friends, friendsList);

                Friends.this.runOnUiThread(new Runnable()
                {
                @Override
                public void run()
                {

                    list.setAdapter(friendAdapter);
                    //list.setOnItemClickListener(EditStaff.this);

                }
                });


            }
            catch (Exception e)
            {
                System.out.println("StaffProgess Exception Caught:"+e.getMessage());
            }
            return Boolean.TRUE;
        }

        @Override
        protected void onPreExecute()
        {

            friendsProgress=new ProgressDialog(Friends.this);
            friendsProgress.setProgressStyle(ProgressDialog.STYLE_SPINNER); 
            friendsProgress.setIndeterminate(true);
            friendsProgress.setMessage("Loading...");       
            friendsProgress.setOwnerActivity(Friends.this);
            friendsProgress.show();

        }

        @Override
        protected void onPostExecute(Boolean Result) 
        {
            friendsProgress.dismiss();
            friendAdapter.notifyDataSetChanged();

        }
    }

这里是 getFriendsList 函数实现:

public void getFriendList(){

        Request request = Request.newMyFriendsRequest(Session.getActiveSession(), new Request.GraphUserListCallback() {

            @Override
            public void onCompleted(List<GraphUser> users, Response response) {
                // TODO Auto-generated method stub

                for(int i=0; i<users.size();i++)
                {
                GraphUser user =    users.get(i);
                Friend objFriend = new Friend();
                objFriend.setFriendID(user.getId());
                objFriend.setFriendName(user.getName());
                Drawable dd =Friends.LoadImageFromWebOperations("http://graph.facebook.com/" + objFriend.getFriendID() + "/picture");
                objFriend.setFriendPic(dd);
                //objFriend.setFriendPic("http://graph.facebook.com/" + objFriend.getFriendID() + "/picture");
                friendsList.add(objFriend);
                //friendAdapter.notifyDataSetChanged();
                Log.d("Friend's Id", objFriend.getFriendID());
                Log.d("Friend's Name", objFriend.getFriendName());
                //Log.d("Friend's Pic", objFriend.getFriendPic());
                Log.d("Friend's List Count", Integer.toString(friendsList.size()));
                }


        }
    });
    Request.executeBatchAsync(request);
4

3 回答 3

2

您不应该实例化 anAdapter并将其链接到方法ListView内部doInBackground(),因为它在工作线程上运行,并且Views不允许在工作线程上执行连接到 Android 的任何操作。相反,您可能想要移动此代码

friendAdapter = new FriendAdapter(Friends.this, R.layout.activity_friends, friendsList);

            Friends.this.runOnUiThread(new Runnable()
            {
            @Override
            public void run()
            {

                list.setAdapter(friendAdapter);
                //list.setOnItemClickListener(EditStaff.this);

            }
            });

onPostExecute()方法。这无疑是一个设计更好的解决方案。希望这可以帮助。

于 2012-12-26T07:54:23.803 回答
0

运行 Ui Thread insideonPostExecute()方法如下:

 runOnUiThread(new Runnable() {

            @Override
            public void run() {                                         
                 friendAdapter = new FriendAdapter(Friends.this, R.layout.activity_friends, friendsList);
                 list.setAdapter(friendAdapter);
                 friendsProgress.dismiss();
                 friendAdapter.notifyDataSetChanged();

            }
        });
于 2012-12-26T08:14:17.083 回答
0

尝试这个,

    ProgressDialog dialog;
    protected void onPreExecute()
    {
        dialog = new ProgressDialog(Friends.this);
        dialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
        dialog.setMax(100);
        dialog.show(); 
    }
于 2012-12-26T07:55:54.473 回答