首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >android- facebook好友头像在列表视图中膨胀

android- facebook好友头像在列表视图中膨胀
EN

Stack Overflow用户
提问于 2011-12-12 11:01:00
回答 1查看 4.3K关注 0票数 3

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

代码语言:javascript
复制
        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方法:

代码语言:javascript
复制
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);
                }
            }
        }                                
    }

好友列表适配器

代码语言:javascript
复制
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,以使其更易于阅读。希望有人能给我指引。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2011-12-12 13:43:10

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

您应该使用在后台运行并无缝下载图像的Asynctask,而不会在列表视图中造成任何延迟

例如:(以网站为例,它先加载文本,然后加载图像,但不会挂起屏幕)

这里有一个很棒的教程的链接,您可以从中构建自己的listview逻辑very handy tutorial

它利用了一个适配器类。通过它,您可以为listview构建适配器

希望这能有所帮助

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/8469482

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档