我正在做的是从 facebook 获取朋友列表并尝试使用朋友姓名和他的个人资料图片填充列表视图,但没有异常和错误出现在 logcat 列表中的大小为 56 并且数据在数组列表中但不填充 UI 不要不知道为什么请给我建议解决方案,这是我的活动 getFriendList 函数代码:
public List<Friend> getFriendList(){
Request request = Request.newMyFriendsRequest(Session.getActiveSession(), new Request.GraphUserListCallback() {
@Override
public void onCompleted(List<GraphUser> users, Response response) {
// TODO Auto-generated method stub
for(int i=0; i<users.size();i++)
{
GraphUser user = users.get(i);
Friend objFriend = new Friend();
objFriend.setFriendID(user.getId());
objFriend.setFriendName(user.getName());
//objFriend.setFriendPic(user.g)
//objFriend.setFriendPic("http://graph.facebook.com/" + objFriend.getFriendID() + "/picture");
friendsList.add(objFriend);
Log.d("Friend's Id", objFriend.getFriendID());
Log.d("Friend's Name", objFriend.getFriendName());
//Log.d("Friend's Pic", objFriend.getFriendPic());
Log.d("Friend's List Count", Integer.toString(friendsList.size()));
}
}
});
Request.executeBatchAsync(request);
return friendsList;
}
这是我的 onCreate 函数:
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.friends_list);
list = (ListView)findViewById(R.id.listview_friends);
friendsList = getFriendList();
friendAdapter = new FriendAdapter(this, R.layout.activity_friends, friendsList);
list.setAdapter(friendAdapter);
}
这是我的自定义适配器代码:
public class FriendAdapter extends ArrayAdapter<Friend>
{
Context context;
Friends holder;
int layoutResourceId;
List<Friend> friendList;
Bitmap profilePhoto;
public FriendAdapter(Context context, int layoutResourceId,List<Friend> friendList)
{
super(context, layoutResourceId, friendList);
this.context=context;
this.layoutResourceId=layoutResourceId;
this.friendList=friendList;
}
@Override
public View getView(int position,View convertView,ViewGroup parent)
{
View row=convertView;
if(row==null)
{
LayoutInflater inflater=((Activity)context).getLayoutInflater();
row=inflater.inflate(layoutResourceId, parent,false);
holder=new Friends();
//holder.friendProfilePic=(ImageView)row.findViewById(R.id.ImageViewfriendProfilePic);
holder.friendName = (TextView)row.findViewById(R.id.textViewFriendsName);
row.setTag(holder);
}
else
{
holder=(Friends)row.getTag();
}
//
holder.friendName.setText(friendList.get(position).getFriendName());
//holder.friendProfilePic.setImageBitmap(profilePhoto);
return row;
}
这是我的 xml 布局friends_list.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<ListView
android:id="@+id/listview_friends"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</ListView>
</LinearLayout>
这是activity_friends.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<ImageView
android:id="@+id/ImageViewfriendProfilePic"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dip"
android:src="@drawable/com_facebook_button_grey_focused" />
<TextView
android:id="@+id/textViewFriendsName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="100dip"
android:layout_marginTop="20dip"
android:text="TextView" />
</RelativeLayout>
</LinearLayout>