将Facebook个人资料图片设置为ListView中的ImageView

时间:2012-08-28 04:48:00

标签: android facebook facebook-graph-api facebook-android-sdk

我有这个应用程序在MySQL表中记录用户的Facebook ID,现在我正在尝试获取此ID并在ListView上显示用户的个人资料图片,但我不知道我做错了什么

我的自定义ArrayAdapter:

public class VisualAdapter extends ArrayAdapter<String> {

private final Context contexto;

private final String[] comics, username, userid;

URL img_url = null;

Bitmap bm;

public VisualAdapter(Context contexto, String[] comics, String[] username, 
        String[] userid) {
    super(contexto, R.layout.exibeimg, comics);
    this.contexto = contexto;
    this.comics = comics;
    this.username = username;
    this.userid = userid;
}

public View getView(int position, View convertView, ViewGroup parent) {
    // TODO Auto-generated method stub
    LayoutInflater inflater = (LayoutInflater) contexto
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

    View rowView = inflater.inflate(R.layout.exibeimg, parent, false);
    TextView textView = (TextView) rowView.findViewById(R.id.tvTitleImage);
    ImageView imageView = (ImageView) rowView.findViewById(R.id.ivImageVisual);
    ImageView ivUser = (ImageView) rowView.findViewById(R.id.ivUser);
    TextView tvUser = (TextView) rowView.findViewById(R.id.tvUserNAME);
    textView.setText(comics[position]);
    tvUser.setText(username[position]);

    try {
        img_url = new URL("http://graph.facebook.com/"+userid+"/picture?type=thumbnail");
        bm = BitmapFactory.decodeStream(img_url.openConnection().getInputStream());
        ivUser.setImageBitmap(bm);
    }
    catch (Exception e) {
        e.printStackTrace();
    }
    return rowView;
}

}

Logcat:http://pastebin.com/kmLXKvZJ

此外,这是在标签内。它位于选项卡2中,当应用程序在选项卡1中启动时,当我单击选项卡2(查看此列表)时,加载需要几秒钟,我可以做些什么来加快加载速度?

1 个答案:

答案 0 :(得分:0)

加载需要几秒钟,因为您正在getView()内同步执行网络操作,这会阻止主UI线程在等待网络响应时每次呈现视图。这非常糟糕,您需要做的是对图像进行异步延迟加载,以便不在UI线程上执行下载图片。

请阅读此great article,了解如何正确实施ListView,以便加载时间不会花费几秒钟。