1

我正在使用一个可扩展列表,只要用户的存在发生变化,它就会显示用户在聊天中的存在(在运行时),我正在发送广播以刷新适配器,但它根本不刷新.

这是代码

设置适配器

/** Set Adapter here */
        adapter = new UserMenuAdapter(this, groupNames, childs);
        setListAdapter(adapter);
        object = this;

我使用 sendbroadcast 方法从服务中调用此接收器,并且我已经检查了它的 onReceive 但列表没有刷新。

BroadcastReceiver UpdateList = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {

            groupChileItem();
            adapter.notifyDataSetChanged();

        }
    };

//groupChileitem ( 方法 )

private void groupChileItem(){
        /***###############GROUP ARRAY ############################*/
        final ArrayList<String> groupNames = new ArrayList<String>();

        if(XMPPConn.mfriendRequestList.size() > 0){
            groupNames.add("Request ("+XMPPConn.mfriendRequestList.size()+")"); 
        }else{
            groupNames.add("Request (0)");
        }
        chatUser = dbHelper.getChatUser();
        groupNames.add("Chats ("+chatUser.size()+")");  
        groupNames.add("Contacts (" + XMPPConn.mfriendList.size() + ")");
        groupNames.add("CGM Groups (" + XMPPConn.mGroupList.size() + ")");
        if(XMPPConn.mfriendPendingList.size() > 0){
            groupNames.add("Pending ("+XMPPConn.mfriendPendingList.size()+")");
        }else{
            groupNames.add("Pending (0)");
        }

        mXMPPConn.getGroup();
        categoryList = dbHelper.getAllCategory();
        /**Group From Sever*/
        if (XMPPConn.mGroupList.size() > 0) {
            for (int g = 0; g < XMPPConn.mGroupList.size(); g++) {
                mXMPPConn.getGroupContact(XMPPConn.mGroupList.get(g).groupName);
                groupNames.add(XMPPConn.mGroupList.get(g).groupName + "("
                        + XMPPConn.mGroupContactList.size()+ ")");
            }
        } 
        this.groupNames = groupNames;

        /*** ###########CHILD ARRAY * #################*/
        ArrayList<ArrayList<ChildItems>> childs = new ArrayList<ArrayList<ChildItems>>();
        ArrayList<ChildItems> child = new ArrayList<ChildItems>();
        /**child for request frieds*/
        if(XMPPConn.mfriendRequestList.size() > 0){
            for(int i = 0; i < XMPPConn.mfriendRequestList.size(); i++){
                child.add(new ChildItems(XMPPConn.mfriendRequestList.get(i).friendNickName,
                        "Waiting for Authorization",0,null));
            }
        }else{
            child.add(new ChildItems("No Request list","",0,null));
        }   
        childs.add(child);
        /**Child for chat */
        child = new ArrayList<ChildItems>();
        if(chatUser.size() > 0){
            for(int i = 0; i < chatUser.size(); i++){
                child.add(new ChildItems(userName(chatUser.get(i)), "",0,null));
            }
        }else{
            child.add(new ChildItems("--No History--", "",0,null));
        }       
        childs.add(child);
        /**Child for contact list*/
        child = new ArrayList<ChildItems>();
        child.add(new ChildItems("", "",0,null));
        if (XMPPConn.mfriendList.size() > 0) {
            for (int n = 0; n < XMPPConn.mfriendList.size(); n++) {
                child.add(new ChildItems(XMPPConn.mfriendList.get(n).friendNickName,
                        XMPPConn.mfriendList.get(n).friendStatus, 
                        XMPPConn.mfriendList.get(n).friendState,
                        XMPPConn.mfriendList.get(n).friendPic));
            }
        }
        childs.add(child);
        /************** CGM Group Child here *********************/
        child = new ArrayList<ChildItems>();
        child.add(new ChildItems("", "",0,null));

        if (XMPPConn.mGroupList.size() > 0) {
            for (int grop = 0; grop < XMPPConn.mGroupList.size(); grop++) {
                child.add(new ChildItems(
                        XMPPConn.mGroupList.get(grop).groupName + " ("
                        + XMPPConn.mGroupList.get(grop).groupUserCount
                        + ")", "",0,null));
            }
        }
        childs.add(child);

        /**Pending friend*/
        child = new ArrayList<ChildItems>();
        if(XMPPConn.mfriendPendingList.size()> 0){
            for(int i = 0; i < XMPPConn.mfriendPendingList.size(); i++){
                child.add(new ChildItems(XMPPConn.mfriendPendingList.get(i).friendNickName,
                        "Waiting for Authorization",0,null));
            }
        }else{
            child.add(new ChildItems("No Pending list","",0,null));
        }
        childs.add(child);
        /************************ Group Contact List *************************/
        if (XMPPConn.mGroupList.size() > 0) {
            for (int g = 0; g < XMPPConn.mGroupList.size(); g++) {
                /** Contact List */
                mXMPPConn.getGroupContact(XMPPConn.mGroupList.get(g).groupName);
                child = new ArrayList<ChildItems>();
                for (int con = 0; con < XMPPConn.mGroupContactList.size(); con++) {
                    child.add(new ChildItems(
                            XMPPConn.mGroupContactList.get(con).friendNickName,
                            XMPPConn.mGroupContactList.get(con).friendStatus,0,null));
                }
                childs.add(child);
            }
        }
        this.childs = childs;

    }

有人请告诉我我做错了什么?

4

1 回答 1

3

我会坚持让您尝试再次设置适配器而不是使用notifyDataSetChanged()

所以你可以尝试使用,

setListAdapter(adapter);

而不是使用,

adapter.notifyDataSetChanged();
于 2012-11-23T07:08:20.330 回答