Question

I have been able to create the if statement that checks for the string and it returns the toast message that i created but it keeps showing the toast message every time i open the chat. even if the most recent message doesn't contain the string I am looking for so i am assume it isn't checking to see if it is the last message received and it doesn't check to see if it is unread. the code is below. the reason i am trying to do this is because my parents share a facebook account and i want an easy way to display if the message is signed mom or dad. the code below only has the check for mom once it works i will be adding the check for dad signature. I am using the open source message client Xabber. Thank you for help.

  public void setVisibleChat(String account, String user) {
    final boolean remove = !AccountManager.getInstance()
            .getArchiveMode(account).saveLocally();
    AbstractChat chat = getChat(account, user);
    if (chat == null)
        chat = createChat(account, user);
    else {
        // Mark messages as read and them delete from db if necessary.
        final ArrayList<MessageItem> messageItems = new ArrayList<MessageItem>();
        for (MessageItem messageItem : chat.getMessages()) {
                if (!messageItem.isRead()) {
                messageItem.markAsRead();
                messageItems.add(messageItem);
            }

            if (chat.getLastText().contains("Mom") && (!messageItem.isRead()));{
                Toast.makeText(Application.getInstance(), "Message from Mom!", Toast.LENGTH_SHORT).show();
            }

        }
        Application.getInstance().runInBackground(new Runnable() {
            @Override
            public void run() {
                Collection<Long> ids = getMessageIds(messageItems, remove);
                if (remove)
                    MessageTable.getInstance().removeMessages(ids);
                else
                    MessageTable.getInstance().markAsRead(ids);
            }
        });
    }
    visibleChat = chat;
}
Était-ce utile?

La solution

You've got an extra semi-colon here

if (chat.getLastText().contains("Mom") && (!messageItem.isRead())); <------

So your next block of code containing the Toast show statement will always be executed.

Remove the semi-colon

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top