diff --git a/src/org/yaaic/adapter/DeckAdapter.java b/src/org/yaaic/adapter/DeckAdapter.java index b9fdc6a..08acfc1 100644 --- a/src/org/yaaic/adapter/DeckAdapter.java +++ b/src/org/yaaic/adapter/DeckAdapter.java @@ -186,7 +186,14 @@ public class DeckAdapter extends BaseAdapter public MessageListView renderConversation(Conversation conversation, ViewGroup parent) { MessageListView list = new MessageListView(parent.getContext()); - list.setAdapter(new MessageListAdapter(conversation, parent.getContext())); + + MessageListAdapter adapter = conversation.getMessageListAdapter(); + if (adapter == null) { + adapter = new MessageListAdapter(conversation, parent.getContext()); + conversation.setMessageListAdapter(adapter); + } + + list.setAdapter(adapter); list.setDivider(null); list.setLayoutParams(new Gallery.LayoutParams( diff --git a/src/org/yaaic/model/Conversation.java b/src/org/yaaic/model/Conversation.java index 569510e..714d1ef 100644 --- a/src/org/yaaic/model/Conversation.java +++ b/src/org/yaaic/model/Conversation.java @@ -24,6 +24,8 @@ import java.util.Collections; import java.util.LinkedList; import java.util.List; +import org.yaaic.adapter.MessageListAdapter; + /** * Base class for conversations * @@ -44,6 +46,7 @@ public abstract class Conversation private List buffer; private List history; private String name; + private MessageListAdapter adapter; /** * Get the type of conversation (channel, query, ..) @@ -131,4 +134,20 @@ public abstract class Conversation { buffer.clear(); } + + /** + * Store the adapter of this conversation + */ + public void setMessageListAdapter(MessageListAdapter adapter) + { + this.adapter = adapter; + } + + /** + * Get the MessageList Adapter of this conversation if known + */ + public MessageListAdapter getMessageListAdapter() + { + return adapter; + } } diff --git a/src/org/yaaic/view/ConversationActivity.java b/src/org/yaaic/view/ConversationActivity.java index 1b7b922..98bc68f 100644 --- a/src/org/yaaic/view/ConversationActivity.java +++ b/src/org/yaaic/view/ConversationActivity.java @@ -232,33 +232,15 @@ public class ConversationActivity extends Activity implements ServiceConnection, public void onConversationMessage(String target) { Conversation conversation = server.getConversation(target); + MessageListAdapter adapter = conversation.getMessageListAdapter(); while(conversation.hasBufferedMessages()) { - // XXX: I wrote all this beautiful code but the view sometimes has - // not all children like the adapter so getChildAt() will return - // no child view or not the view that is logical connected to - // the item in the adapter at the same position. So we just - // notify the whole deck adapter (as fallback). - Message message = conversation.pollBufferedMessage(); - int position = deckAdapter.getPositionByName(target); - - if (position != -1) { - MessageListView view = (MessageListView) deck.getChildAt(position); - if (view != null) { - MessageListAdapter adapter = view.getAdapter(); - adapter.addMessage(message); - } else { - Log.d(TAG, "MessageListView Adapter is null (position: " + position + ")"); - // Fallback: We could not get the MessageListAdapter. Notify the whole deck - deckAdapter.notifyDataSetChanged(); - } - } - - if (deckAdapter.isSwitched() && target.equals(deckAdapter.getSwitchedName())) { - MessageListView switchedView = deckAdapter.getSwitchedView(); - switchedView.getAdapter().addMessage(message); + if (adapter != null) { + adapter.addMessage(message); + } else { + Log.d(TAG, "MessageListAdapter is null (conversation " + conversation.getName() + " has no adapter assigned)"); } } }