gpt4 book ai didi

android-facebook friend 个人资料图片在 ListView 中膨胀

转载 作者:太空狗 更新时间:2023-10-29 13:38:20 27 4
gpt4 key购买 nike

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

        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();
}
});

获取好友列表方法:

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 ,以使其更易于阅读。希望有人能指导我。

最佳答案

您的 ListView 滞后,因为图像下载发生在主 (UI) 线程中,

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

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

这是一个优秀教程的链接,您可以从中构建 ListView 逻辑 very handy tutorial

它使用适配器类。通过它你可以为你的 ListView 构建适配器

希望对你有帮助

关于android-facebook friend 个人资料图片在 ListView 中膨胀,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8469482/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com