2012-12-24 155 views
3

我在做什麼是從facebook獲取朋友列表,並試圖填充列表視圖與朋友的名字和他的個人資料圖片,但沒有例外和錯誤進入logcat列表獲取56的大小和數據在數組中名單但不填充在UI不知道爲什麼好心建議我的解決方案,這是我的活動getFriendList功能代碼:android listview不填充

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> 
+2

確保Adapter'getCount()'返回你想要顯示的行數。 http://developer.android.com/reference/android/widget/Adapter.html#getCount() – sabadow

+0

你可以發佈你的XML佈局嗎? –

+0

適配器數量爲0,但爲什麼它是0? –

回答

3

不要使用列表這種方式。

friendsList = getFriendList();

由於請求是發送和你有可能需要一些時間,以便聲明您的列表中的物體,像這樣的結果

List<Friends> friendList = new ArrayList<Friends>(); 

只是通過這個數組列表爲空,適配器和適配器設置列表

friendAdapter = new FriendAdapter(this, R.layout.activity_friends, friendsList); 
list.setAdapter(friendAdapter); 

現在使用的AsyncTask類以從網絡中的數據背景,以UI並沒有停止,它會在後臺獲取

修訂

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

    } 
}; 

稱這種現象的onCreate後適配器設置

FetchFriendList ffl = new FetchFriendList(); 
ffl.execute(); 

還有一兩件事,當你添加/編輯/列表中刪除的項目,你必須調用notifyDatasetChanged();不要過多添加/內UI刪除它可以強制關閉大數據

+0

我應該在哪裏調用asynctask ... @ Pratik? –

+0

檢查更新的答案 – Pratik

+0

感謝@Pratik .... –

0

只要改變你的適配器

if(row==null) 
{ 
    LayoutInflater inflater=((Activity)context).getLayoutInflater(); 
    row=inflater.inflate(layoutResourceId, null); 
    holder=new Friends(); 
    holder.friendName = (TextView)row.findViewById(R.id.textViewFriendsName); 
    row.setTag(holder); 
} 

,並在適配器覆蓋getCount將()方法返回的大小the friedns list ..我也經歷過這個..