我有一个问题,充气Facebook的个人资料图片到列表视图。我正在做的是一旦用户点击fb图像图标,从JSONArray我检索每个用户的图片url并将其存储在数组列表中。所以当我为我的列表视图设置适配器时,我传递了URL并对其进行解码。不幸的是,当我尝试向下滚动时,我的列表视图变得滞后,因为适配器仍在下载图像。如果我注释掉解码用户配置文件图片url的代码,我的listview工作正常,但只显示我朋友的名字。下面是我的代码。Android的Facebook的朋友个人资料图片充气在列表视图
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块,使其更易于阅读。希望有人能指导我。
感谢您的想法,我按照酷似教程,没有落后了。但图像与用户名不在正确的位置。 – Zahary
欢呼声,你的问题似乎解决了,你可能只需要在你的布局上工作。 –