问题
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 with list of groups in my chat app.
This is my first arraylist: Here User is my model class userMap is HashMap which has all th User list
ArrayList<User> users = new ArrayList<User>();
for (User user : SocketSingleton.userMap.values()) {
if (user.getId() != loggedUserId) {
users.add(user);
}
}
Adapter for my first arraylist is
UserAdapter adapter1 = new UserAdapter(getActivity(),R.layout.all_user_list_item,users);
This is my second arraylist which has all the groups Here Channel is my model class for groups. And listchannels is HashMap which has all the groups.
ArrayList<Channel> groups = new ArrayList<JoinedChannel>();
for (Channel channel : SocketSingleton.listchannels.values()) {
groups.add(channel);
}
Adapter for second array list
ChannelAdapter adapter2 = new ChannelAdapter(getActivity(), R.layout.grouplist, groups);
I want to add the two lists So that i can set that single listview which contains all user and groups to a AutocompleteTextview Please tell me a way to do that.
回答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:
class ChatObject{
private int id;
prvate String title;
private boolean 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 ;)
来源:https://stackoverflow.com/questions/33805996/how-to-add-two-arraylist-which-has-different-data-in-android-to-get-a-single-lis