可扩展列表适配器根本不刷新

时间:2012-11-19 10:38:01

标签: android expandablelistview expandablelistadapter

我正在使用一个可扩展列表,它会显示聊天中的用户(在运行时),只要用户在场时发生更改,我就会发送广播来刷新适配器,但它不是一点儿都很清爽。

这是代码

设置适配器

/** 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;

    }

任何人请告诉我我做错了什么?

1 个答案:

答案 0 :(得分:3)

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

所以你可以尝试使用,

setListAdapter(adapter);

使用,

adapter.notifyDataSetChanged();