3

私がやっていることは、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>
4

2 に答える 2

3

このようにリストを使用しないでください。

friendsList = getFriendList();

リクエストが送信され、結果が得られたため、時間がかかる場合があるため、このようにリストオブジェクトを宣言します

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

この配列リストをアダプターで空として渡し、アダプターをリストに設定するだけです。

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

AsyncTaskクラスを使用して、バックグラウンドでWebからデータを取得し、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();

リスト内のアイテムを追加/編集/削除するときにもう1つ、notifyDatasetChanged()を呼び出す必要があります。UI内で追加/削除しすぎないでください。大きなデータで強制的に閉じる可能性があります。

于 2012-12-24T13:21:31.677 に答える
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() メソッドをオーバーライドして、friedns リストのサイズを返します。私もこれを経験しました。

于 2013-11-15T16:03:59.780 に答える