android - Display different objects in the same ListAdapter -
i display different types of objects in same listview, don't know how differentiate these objects via getitem(position)
the listview displays list of messages can either chat, either notification, , items chat , notification have different layout.
this 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 , chat classes :
public class notification extends message { ... } public class cat extends message { ... } i retrieving list of chats , list of notifications web-services when starting activity, have add them new list of messages in respective order (date), , instantiate m adapter list of message
is practice ?
just add boolean flag class object , in adapter, check flag , use 1 layout if chat, if flagged notification. no problem that
there tutorial here covers more or less trying different view layouts within listview
within adapter create 2 viewholders, 1 chat, 1 notification. then, in getview(), object creating view for, check boolean flag, instantiate correct holder , inflate view based upon flag , set view elements if there 1 class , listview show view set element in place.
Comments
Post a Comment