我有一個問題,充氣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
歡呼聲,你的問題似乎解決了,你可能只需要在你的佈局上工作。 –