I would like to display different types of objects in the same ListView
, but I don't know how to differentiate these Objects via getItem(position)
The ListView
displays a list of Messages
which can be either a Chat
, either a Notification
, and the items Chat
and Notification
have different layout.
This is the adapter :
public class MailboxAdapter extends BaseAdapter {
private ArrayList<Messages> m_alMessages = null;
private Messages getItem(int position) {
return m_alMessages.get(position)
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
if (m_alMessages != null) {
if (getItem(position).isChat()) {
final Chat cChatItem = getItem(position);
if (convertView == null) {
//Cat logic
// ...
}
} else {
final Notification nNotifItem = getItem(position);
if (convertView == null) {
//Notification logic
// ...
}
}
}
}
the Message Class (minimal)
public class Message {
private long m_lId = 0L;
private boolean m_bIsChat = false;
public boolean isChat() { return m_bIsChat; }
}
the Notification
and Chat
classes :
public class Notification extends Message { ... }
public class Cat extends Message { ... }
I am retrieving a list of Chats and a List of Notifications from web-services when starting the activity, so I would have to add them to a new list of Messages in their respective order (date), and then instantiate m adapter with this list of Message
Is it a good practice ?