Skip to content Skip to sidebar Skip to footer

How To Add Two Arraylist Which Has Different Data In Android To Get A Single Listview

I have two arraylist. Arraylist one contains list of usrs in my chat app ArrayList Two contains list of Groups in my chat app What i am trying to do here is i want add user list w

Solution 1:

you can not combine two different adapter. As an option, is to create a separate class that will include something in common between two objects, like:

classChatObject{
privateint id;
prvate String title;
privateboolean isUser;
}

dont forget to add constructor and get/set methods.

And when you are filling your arrays, also create your new Objects which will be using in your listview.

ArrayList<User> users = new ArrayList<User>();
ArrayList<ChatObject > chatObjects= new ArrayList<ChatObject >();
        for (User user : SocketSingleton.userMap.values()) {
            if (user.getId() != loggedUserId) {
                users.add(user);
                ChatObject chatObject = new ChatObject();
                chatObject.setId(user.getId());
                chatObject.setTitle(user.getName());  //for example
                chatObject.setIsUser(true);
                chatObjects.add(chatObject);
            }
        }

and

ArrayList<Channel> groups = new ArrayList<JoinedChannel>();
        for (Channel channel : SocketSingleton.listchannels.values()) {
            groups.add(channel);
            ChatObject chatObject = new ChatObject();
            chatObject.setId(channel.getId());
            chatObject.setTitle(channel.getTitle());  //for example
            chatObject.setIsUser(false);
            chatObjects.add(chatObject);
        }

Hope it helps you ;)

Post a Comment for "How To Add Two Arraylist Which Has Different Data In Android To Get A Single Listview"