Reputation: 6369
I have chat app which have two types of elements in listview
message
message
message
Header
message
message
In my app after showing Header I need to remove or hide it after particular time or event. I know that I can get this item by position. The problem is that my listview dynamically can grow from both sides. What can I do in this situation. You can see my custom adapter in this question: In Android how to prevent updating existing items on notifyDataSetChanged()
Upvotes: 0
Views: 325
Reputation: 8106
I would extend a Class by another and give your ArrayAdapter the parent Class.
In your drawer you may work with
if (position.get(pos) instanceOf MessageHeader) { }
else if (position.get(pos) instanceOf SimpleMessage) {}
your Class may look like that:
class Message {
private String text;
}
class SimpleMessage extends Message {}
class HeaderMessage extends Message {}
and your ArrayAdapter accept the Message
class MyCustomAdapter extends ArrayAdapter<Message> {}
If you want to add or remove items you can use the following way doing that:
private ArrayList<Message> messages = null;
public myAdapter(List myList) { this.messages = (ArrayList)myList; }
public void addItem(Message message) { messages.add(message); forceUpdate(); }
public void removeItem(int position) {messages.remove(position); forceUpdate(); }
private voice forceUpdate() { notifyDataSetInvalidated(); notifyDataSetChanged(); }
private List<Integer> getItemPositionsWithHeader() {
ArrayList<Integer> list = new ArrayList<>();
for (int i = 0, j = messages.size(); i <= j; i++) {
if (messages.get(i) instanceOf HeaderMessage) {
list.add(i);
}
}
return list;
}
private void removeAllHeaderItemsFromList() {
for (Integer i : getItemPositionsWithHeader) {
messages.remove(i);
}
forceUpdate();
}
Upvotes: 1