android- facebook朋友个人资料图片在listview中膨胀

时间:2011-12-12 03:01:00

标签: android facebook listview facebook-graph-api

我有问题将facebook个人资料图片扩展到listview。我正在做的是一旦用户点击fb图标图标,从JSONArray我检索每个用户的图片网址并将其存储在arraylist中。所以当我为listview设置适配器时,我传递了url并对其进行解码。不幸的是,当我尝试向下滚动时,我的列表视图变得滞后,因为适配器仍在下载图像。如果我注释掉解码用户个人资料图片网址的代码,我的listview工作正常,但只显示我的朋友的名字。下面是我的代码。

        fbimage = (ImageView) findViewById(R.id.fb_contact);        
        fbimage.setOnClickListener(new OnClickListener(){
        public void onClick(View v){
            token = fb.getAccessToken();
            final Dialog friends_dialog = new Dialog(PostToWall.this);
            friends_dialog.setTitle("Facebook Friends");
            friends_dialog.setContentView(R.layout.friend_list);

            friendList = (ListView) friends_dialog.findViewById(R.id.fb_friend_list);                                           
            friendList.setOnItemClickListener(PostToWall.this);

            progress = ProgressDialog.show(PostToWall.this, "Facebook Friends",
                                        "Fetching Data", true, true);
            final Handler handler = new Handler(){
                public void handleMessage(Message msg){
                    progress.dismiss();
                    friendList.setAdapter(new FbFriendListAdapter(getApplicationContext(),friendsName, friendsId, friendPic));
                    friends_dialog.show();
                    }
                };

                Thread thread = new Thread(){
                    public void run(){
                        getFriendList();
                        handler.sendEmptyMessage(0);
                    }
                };
                thread.start();                                         
            }
        }); 

getFriendList方法:

public void getFriendList(){
        response = fb.request("me/friends");
        JSONObject fbFriendObj = Util.parseJson(response);
        JSONArray fbFriendArray = fbFriendObj.getJSONArray("data");                 
        friendsName = new ArrayList<String>();
        friendsId = new ArrayList<String>();
        friendPic = new ArrayList<String>();
        int i;
        for(i=0;i<=fbFriendArray.length();i++){
            JSONObject friendIDName = fbFriendArray.getJSONObject(i);
            Iterator it = friendIDName.keys();
            while(it.hasNext()){
                String nameId = (String)it.next();
                String name = friendIDName.getString(nameId);
                if(nameId.equals("id")){
                        friendsId.add(name);                            
                friendPic.add(graph_path + name + "/picture");                          
                }else if(nameId.equals("name")){
                    friendsName.add(name);
                }
            }
        }                                
    }

朋友列表适配器

public class FbFriendListAdapter extends ArrayAdapter<String>{

    private ArrayList<String> friendName;
    //private ArrayList<String> friendID;
    private ArrayList<String> friendPic;
    private Context context;
    Bitmap icon;

    public FbFriendListAdapter(Context c, ArrayList<String> friendName, ArrayList<String> friendID, ArrayList<String> friendPic){
        super(c, R.layout.fb_friend_adapter, 0, friendName);
        this.friendName = friendName;
        //this.friendID = friendID;
        this.friendPic = friendPic;
        this.context = c;
    }

    public int getCount() {
        return friendName.size();       
    }

    public long getItemId(int position) {
        return 0;
    }

    static class ViewHolder{
        public TextView descHolder;
        public ImageView typeHolder;
    }   

    public View getView(int position, View convertview, ViewGroup parent) {
            ViewHolder vh;
            if(convertview==null){              
                LayoutInflater inf = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                convertview = inf.inflate(R.layout.fb_friend_adapter, null,false);
                vh = new ViewHolder();
                vh.descHolder = (TextView) convertview.findViewById(R.id.fbFriendName);             
                vh.typeHolder = (ImageView) convertview.findViewById(R.id.fbImage);
                convertview.setTag(vh);             
            }
            else{               
                vh = (ViewHolder)convertview.getTag();

            }

            URL img_value = null;
            try {
                img_value = new URL(friendPic.get(position).toString());
                try {
                    icon = BitmapFactory.decodeStream(img_value.openConnection().getInputStream());
                } catch (IOException e) {
                    e.printStackTrace();
                }
            } catch (MalformedURLException e) {
                e.printStackTrace();
            }

            vh.descHolder.setText(friendName.get(position).toString());
            vh.typeHolder.setImageBitmap(icon);
            return convertview;
    }       
}

我删除了一些代码,例如try catch block,以便于阅读。希望有人可以指导我。

1 个答案:

答案 0 :(得分:2)

你的列表视图滞后,因为图像的下载发生在主(UI)线程中,

你应该使用将在后台运行的Asynctask并无缝下载图像,而不会导致任何落后于列表视图

例如。 (想想网站,它首先加载文本然后加载图像,但不会挂起你的屏幕)

这是一个指向优秀教程的链接,您可以从中构建列表视图逻辑 very handy tutorial

它使用了适配器类。通过哪个你可以为你的列表视图构建适配器

希望这有帮助