如何添加两个的ArrayList这在Android有不同的数据,以得到一个单一的列表视图 [英] How to add two arraylist which has different data in android to get a single listview
问题描述
我有两个数组列表。
ArrayList的一个含有在我的聊天应用程序的USR列表
ArrayList中包含两个在我的聊天应用组列表
我想在这里做的是我想添加用户名单在我的聊天应用程序组的列表。
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.
这是我的第一个数组列表:
在这里,用户是我的模型类
中userMap是HashMap中有所有个用户的列表
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);
}
}
适配器我的第一个ArrayList是
Adapter for my first arraylist is
UserAdapter adapter1 = new UserAdapter(getActivity(),R.layout.all_user_list_item,users);
这是我拥有所有的组第二的ArrayList
这里是渠道为团体我的模型类。而listchannels是具有所有组HashMap中。
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);
我想补充两个列表,这样我可以设置包含所有用户和组到AutocompleteTextview单一的ListView
请告诉我一个办法做到这一点。
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.
推荐答案
您不能合并两个不同的适配器。作为一个选项,是创建一个单独的类,其中将包括共同的东西两个物体之间,如:
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;
}
不要忘了添加构造函数和get / set方法。
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);
}
}
和
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 ;)
这篇关于如何添加两个的ArrayList这在Android有不同的数据,以得到一个单一的列表视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!