title: Sending and Recieving Messages using Parse
body: <p>I have a problem sending and receiving messages using Parse.  The app runs but the sent message doesn't get to the other users, and the message is not saved in Parse.  Also the username is not written on the top of the activity chat.</p>
  
  <p>This is my chat class:</p>
  
  <pre><code>package com.example.veto_uwk.chatapp;
  
  import java.util.ArrayList;
  import java.util.Date;
  import java.util.List;
  
  import android.content.Context;
  import android.content.Intent;
  import android.os.Bundle;
  import android.os.Handler;
  import android.provider.SyncStateContract;
  import android.text.InputType;
  import android.text.format.DateUtils;
  import android.view.MenuItem;
  import android.view.View;
  import android.view.ViewGroup;
  import android.view.inputmethod.InputMethodManager;
  import android.widget.AbsListView;
  import android.widget.BaseAdapter;
  import android.widget.EditText;
   import android.widget.ListView;
  import android.widget.TextView;
  
   import com.example.veto_uwk.chatapp.CustomActivity;
   import com.example.veto_uwk.chatapp.UserList;
  import com.parse.FindCallback;
    import com.parse.ParseAnalytics;
  import com.parse.ParseException;
  import com.parse.ParseObject;
  import com.parse.ParseQuery;
     import com.parse.ParseUser;
        import com.parse.SaveCallback;
   import com.example.veto_uwk.chatapp.R;
    import com.example.veto_uwk.chatapp.CustomActivity;
     import com.example.veto_uwk.chatapp.Conversation;
  
  
   public class Chat extends CustomActivity
   {
   public static ParseUser Username;
  
  /** The Conversation list. */
  private ArrayList&lt;Conversation&gt; convList;
  
  /** The chat adapter. */
  private ChatAdapter adp;
  
  /** The Editext to compose the message. */
  private EditText txt;
  
  /** The user name of buddy. */
  private String buddy;
  
  /** The date of last message in conversation. */
  private Date lastMsgDate;
  
  /** Flag to hold if the activity is running or not. */
  private boolean isRunning;
  
  /** The handler. */
  private static Handler handler;
  
  /* (non-Javadoc)
   * @see android.support.v4.app.FragmentActivity#onCreate(android.os.Bundle)
   */
  @Override
  protected void onCreate(Bundle savedInstanceState)
  {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.chat);
  
      ParseAnalytics.trackAppOpened(getIntent());
  
      convList = new ArrayList&lt;Conversation&gt;();
      ListView list = (ListView) findViewById(R.id.list);
      adp = new ChatAdapter();
      list.setAdapter(adp);
      list.setTranscriptMode(AbsListView.TRANSCRIPT_MODE_ALWAYS_SCROLL);
      list.setStackFromBottom(true);
  
      txt = (EditText) findViewById(R.id.txt);
      txt.setInputType(InputType.TYPE_CLASS_TEXT
              | InputType.TYPE_TEXT_FLAG_MULTI_LINE);
  
      setTouchNClick(R.id.buttonSend);
  
  
      buddy = getIntent().getStringExtra("name");
    /*  getActionBar().setTitle(buddy);*/
  
  
  
      handler = new Handler();
  }
  
  /* (non-Javadoc)
   * @see android.support.v4.app.FragmentActivity#onResume()
   */
  @Override
  protected void onResume()
  {
      super.onResume();
      isRunning = true;
      loadConversationList();
  }
  
  /* (non-Javadoc)
   * @see android.support.v4.app.FragmentActivity#onPause()
   */
  @Override
  protected void onPause()
  {
      super.onPause();
      isRunning = false;
  }
  
  /* (non-Javadoc)
   * @see com.socialshare.custom.CustomFragment#onClick(android.view.View)
   */
  @Override
  public void onClick(View v)
  {
      super.onClick(v);
      if (v.getId() == R.id.buttonSend)
      {
          sendMessage();
      }
  
  }
  
  /**
   * Call this method to Send message to opponent. It does nothing if the text
   * is empty otherwise it creates a Parse object for Chat message and send it
   * to Parse server.
   */
  private void sendMessage()
  {
      if (txt.length() == 0)
          return;
  
      InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
      imm.hideSoftInputFromWindow(txt.getWindowToken(), 0);
  
      String s = txt.getText().toString();
      final Conversation c = new Conversation(s, new Date(),
              UserList.user.getUsername());
      c.setStatus(Conversation.STATUS_SENDING);
      convList.add(c);
      adp.notifyDataSetChanged();
      txt.setText(null);
  
      ParseObject po = new ParseObject("Chat");
      po.put("sender", UserList.user.getUsername());
      po.put("receiver", buddy);
      po.put("createdAt", "");
      po.put("message", s);
      po.saveEventually(new SaveCallback() {
  
          @Override
          public void done(ParseException e)
          {
              if (e == null)
                  c.setStatus(Conversation.STATUS_SENT);
              else
                  c.setStatus(Conversation.STATUS_FAILED);
              adp.notifyDataSetChanged();
          }
      });
  }
  
  /**
   * Load the conversation list from Parse server and save the date of last
   * message that will be used to load only recent new messages
   */
  private void loadConversationList()
  {
      ParseQuery&lt;ParseObject&gt; q = ParseQuery.getQuery("Chat");
      if (convList.size() == 0)
      {
          // load all messages...
          ArrayList&lt;String&gt; al = new ArrayList&lt;String&gt;();
          al.add(buddy);
          al.add(UserList.user.getUsername());
          q.whereContainedIn("sender", al);
          q.whereContainedIn("receiver", al);
      }
      else
      {
          // load only newly received message..
          if (lastMsgDate != null)
              q.whereGreaterThan("createdAt", lastMsgDate);
          q.whereEqualTo("sender", buddy);
          q.whereEqualTo("receiver", UserList.user.getUsername());
      }
      q.orderByDescending("createdAt");
      q.setLimit(30);
      q.findInBackground(new FindCallback&lt;ParseObject&gt;() {
  
          @Override
          public void done(List&lt;ParseObject&gt; li, ParseException e)
          {
              if (li != null &amp;&amp; li.size() &gt; 0)
              {
                  for (int i = li.size() - 1; i &gt;= 0; i--)
                  {
                      ParseObject po = li.get(i);
                      Conversation c = new Conversation(po
                              .getString("message"), po.getCreatedAt(), po
                              .getString("sender"));
                      convList.add(c);
                      if (lastMsgDate == null
                              || lastMsgDate.before(c.getDate()))
                          lastMsgDate = c.getDate();
                      adp.notifyDataSetChanged();
                  }
              }
              handler.postDelayed(new Runnable() {
  
                  public void run()
                  {
                      if (isRunning)
                          loadConversationList();
                  }
              }, 1000);
          }
      });
  
  }
  
  /**
   * The Class ChatAdapter is the adapter class for Chat ListView. This
   * adapter shows the Sent or Receieved Chat message in each list item.
   */
  private class ChatAdapter extends BaseAdapter
  {
  
      /* (non-Javadoc)
       * @see android.widget.Adapter#getCount()
       */
      public int getCount()
      {
          return convList.size();
      }
  
      /* (non-Javadoc)
       * @see android.widget.Adapter#getItem(int)
       */
      public Conversation getItem(int arg0)
      {
          return convList.get(arg0);
      }
  
      /* (non-Javadoc)
       * @see android.widget.Adapter#getItemId(int)
       */
      public long getItemId(int arg0)
      {
          return arg0;
      }
  
      /* (non-Javadoc)
       * @see android.widget.Adapter#getView(int, android.view.View, android.view.ViewGroup)
       */
      public View getView(int pos, View v, ViewGroup arg2)
      {
          Conversation c = getItem(pos);
          if (c.isSent())
              v = getLayoutInflater().inflate(R.layout.chat_item_sent, null);
          else
              v = getLayoutInflater().inflate(R.layout.chat_item_rcv, null);
  
          TextView lbl = (TextView) v.findViewById(R.id.lbl2);
          lbl.setText(DateUtils.getRelativeDateTimeString(Chat.this, c
                          .getDate().getTime(), DateUtils.SECOND_IN_MILLIS,
                  DateUtils.DAY_IN_MILLIS, 0));
  
          lbl = (TextView) v.findViewById(R.id.lbl1);
          lbl.setText(c.getMsg());
  
          if (c.isSent())
          {
              if (c.getStatus() == Conversation.STATUS_SENT)
                  lbl.setText("Delivered");
              else if (c.getStatus() == Conversation.STATUS_SENDING)
                  lbl.setText("Sending...");
              else
                  lbl.setText("Failed");
          }
          else
              lbl.setText("");
  
          return v;
      }
  
  }
  
  /* (non-Javadoc)
   * @see android.app.Activity#onOptionsItemSelected(android.view.MenuItem)
   */
  @Override
  public boolean onOptionsItemSelected(MenuItem item)
  {
      if (item.getItemId() == android.R.id.home)
      {
          finish();
      }
      return super.onOptionsItemSelected(item);
  }
  }
  </code></pre>
  
  <p>This is my conversation class:</p>
  
  <pre><code> package com.example.veto_uwk.chatapp;
  
   import java.util.Date;
  
  
  public class Conversation {
  public static final int STATUS_SENDING =0;
  public static final int STATUS_SENT =1;
  public static final int STATUS_FAILED = 2;
  private String  msg;
  private int status = STATUS_SENT;
  private Date date;
  private String sender;
  
  public Conversation(String msg ,Date date ,String sender)
  {
      this.msg=msg;
      this.date=date;
      this.sender =sender;
  }
  public Conversation()
  {
  
  }
  public String getMsg()
  {
      return msg;
  }
  public void setMsg(String msg)
  {
      this.msg = msg;
  }
  public boolean isSent()
  {
      return UserList.user.getUsername().equals(sender);
  }
  public Date getDate()
  {
      return date;
  }
  public void setDate(Date date){
      this.date =date;
  }
  public String getSender()
  {
      return sender;
  }
  public void setSender(String sender)
  {
      this.sender=sender;
  }
  public int getStatus()
  {
      return status;
  }
  public void setStatus(int status)
  {
      this.status = status;
  }
  }
  </code></pre>