android listview没有填充

时间:2012-12-24 13:05:27

标签: android listview

我正在做的是从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>

2 个答案:

答案 0 :(得分: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();

当您在列表中添加/编辑/删除项目时,还必须调用notifyDatasetChanged();并且不要在UI中添加/删除太多,否则可能会强制关闭大数据

答案 1 :(得分: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列表的大小..我也经历过这个..