What i am doing is getting friends list from facebook and trying to populate list view with friends name and his profile pic but no exception and error coming in logcat list is getting size of 56 and data is in the array list but not populating on UI don't know why kindly suggest me the solution,here is my activity getFriendList Function code:
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;
}
and here is my onCreate function:
@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);
}
here is my custom adapter code:
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;
}
here is my xml layout 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>
and here is 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>
Don't use list this way.
friendsList = getFriendList();
As the request was send and you got the result it may take time so declare your list object like this way
List<Friends> friendList = new ArrayList<Friends>();
just pass this array list as empty in adapter and set the adapter to the list
friendAdapter = new FriendAdapter(this, R.layout.activity_friends, friendsList);
list.setAdapter(friendAdapter);
now use the AsyncTask class to get the data from web in background so the UI was not stop and it'll be fetched in background
UPDATED
AsyncTask FetchFriendList = new AsyncTask<Void,Void,Void>(){
private List<Friends> list;
public void doInBackground(Void... param){
list = getFriendList(); // call here your request
}
public void onPostExecution(Void result){
friendList.addAll(list);
friendAdapter.notifyDatasetChanged();
}
};
call this in onCreate after adapter set
FetchFriendList ffl = new FetchFriendList();
ffl.execute();
One more thing when you add/edit/delete item in list you must call notifyDatasetChanged(); and don't too much add/delete within UI it may force close with large data
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With