title: How to refresh SMS Listview automatically ?
body: <p>I am creating SMS app. I am sending and receiving SMS with my application and then can show them in ListView. But listview doesn't get updated as soon as I send or receive SMS. I have to press back button , after that if I again go to ListView Activity then new SMS are shown.</p>
  
  <p>How can I make listview refresh automatically as soon as sms arrives or is send ?</p>
  
  <p>Code is :</p>
  
  <pre><code>public class ChatActivity extends ListActivity {
  
  private MyListAdapter adapter;
  ArrayList&lt;String&gt; item_id = new ArrayList&lt;String&gt;();
  ArrayList&lt;String&gt; item_phone_num = new ArrayList&lt;String&gt;();
  ArrayList&lt;String&gt; item_msg_body = new ArrayList&lt;String&gt;();
  ArrayList&lt;String&gt; item_time = new ArrayList&lt;String&gt;();
  ArrayList&lt;String&gt; item_flag = new ArrayList&lt;String&gt;();
  ArrayList&lt;String&gt; items = new ArrayList&lt;String&gt;();
  private Button btn_send;
  DbManager manager;
  Cursor Cursor;
  ViewHolder holder12;
  String contact_for_chat;
  String contact_no;
  String message_body = "";
  Calendar c;
  SimpleDateFormat sdf;
  String time;
  EditText et_chat;
  String flag;
  String msg = "";
  
  @Override
  public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);
      Bundle bundle = getIntent().getExtras();
      contact_for_chat = bundle.getString("contact_name");
      contact_for_chat = contact_for_chat.replace(" ", "");
      contact_no = Util.getContactNumber(contact_for_chat, ChatActivity.this);
      Toast.makeText(getApplicationContext(), contact_no, Toast.LENGTH_LONG).show();
      final ViewHolder holder = new ViewHolder();
      manager = new DbManager(this);
      Cursor = manager.Return_All(contact_no);
      showEvents(Cursor);
  
      c = Calendar.getInstance();
      sdf = new SimpleDateFormat("dd:MMMM:yyyy HH:mm:ss a");
      time = sdf.format(c.getTime());
      setActionBar();
      findViewsById();
      adapter = new MyListAdapter(this);
      adapter.notifyDataSetChanged();
      setListAdapter(adapter);
  
      btn_send = (Button) findViewById(R.id.button1);
      btn_send.setOnClickListener(new OnClickListener() {
          public void onClick(View v) {
  
              SendSMS();
          }
      });
  }
  
  protected void SendSMS() {
      SmsManager sms_manager = SmsManager.getDefault();
      message_body = et_chat.getText().toString();
      ArrayList&lt;String&gt; parts = sms_manager.divideMessage(message_body);
      sms_manager.sendMultipartTextMessage(contact_no, null, parts, null, null);
      flag = "1";
      manager.Insert_sms_data(time, contact_no, message_body,flag);
      msg+= "SMS to :" + contact_for_chat + " \n";
      msg += "having number:" + contact_no + " \n";
      msg += "as" +message_body + " \n";
      msg += "at"+ time + " \n";
      Toast.makeText(getApplicationContext(), ""+msg , Toast.LENGTH_LONG).show();
  
  }
  
  private void setActionBar() {
      ActionBar mActionBar = getActionBar();
      mActionBar.setDisplayHomeAsUpEnabled(false);
      mActionBar.setDisplayShowTitleEnabled(false);
      LayoutInflater mInflater = LayoutInflater.from(this);
      View mCustomView = mInflater.inflate(R.layout.actionbar_chat, null);
      TextView tv_chat = (TextView)mCustomView.findViewById(R.id.title_text);
      tv_chat.setText(contact_for_chat);
      ColorDrawable colorDaawable = new ColorDrawable(Color.parseColor("#CFCFC4"));
      mActionBar.setBackgroundDrawable(colorDaawable);
      mActionBar.setLogo(R.drawable.ic_launcher);
      mActionBar.setDisplayHomeAsUpEnabled(true);
      mActionBar.setCustomView(mCustomView);
      mActionBar.setDisplayShowCustomEnabled(true);
  }
  
  private void findViewsById() {
      et_chat = (EditText)findViewById(R.id.et_chat);
      btn_send = (Button)findViewById(R.id.btn_send);
  }
  
  private void showEvents(Cursor cursor) {
  
      item_id = new ArrayList&lt;String&gt;(cursor.getCount());
      item_phone_num = new ArrayList&lt;String&gt;(cursor.getCount());
      item_msg_body = new ArrayList&lt;String&gt;(cursor.getCount());
      item_time = new ArrayList&lt;String&gt;(cursor.getCount());
      item_flag = new ArrayList&lt;String&gt;(cursor.getCount());
      int i=0;
      while (cursor.moveToNext()) {
          item_id.add(i+"");
          item_time.add(cursor.getString(1));
          item_msg_body.add(cursor.getString(3));
          item_phone_num.add(cursor.getString(2));
          item_flag.add(cursor.getString(4));
          i++;
      }
  
    }
  public class MyListAdapter extends BaseAdapter {
      Context con;
      private LayoutInflater layoutinf;
      ArrayList&lt;Boolean&gt; itemChecked = new ArrayList&lt;Boolean&gt;();
      ArrayList&lt;String&gt; items_ = new ArrayList&lt;String&gt;();
  
      public MyListAdapter(ChatActivity context) {
          con = context;
      }
  
      public int getCount() {
          return item_id.size();
      }
  
      public Object getItem(int position) {
          return item_id.size();
      }
  
      public long getItemId(int position) {
          return item_id.get(position).hashCode();
      }
  
      public View getView(final int position, View arg1, ViewGroup arg2) {
  
          View v = arg1;
          ViewHolder holder = null;
          if (v == null) {
              layoutinf = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
              v = layoutinf.inflate(R.layout.row_chat, null);
              holder = new ViewHolder();
  
              holder.tv_contact = (TextView) v.findViewById(R.id.phone_num);
              holder.tv_sms_body = (TextView) v.findViewById(R.id.msg_body);
              holder.tv_time = (TextView) v.findViewById(R.id.time);
  
              v.setTag(holder);
          } else {
              holder = (ViewHolder) v.getTag();
          }
  
          if(item_flag.get(position).equals("1"))
          {
              holder.tv_sms_body.setBackgroundResource(R.drawable.bubble_green);
  
          }
                      else 
                      {
                          holder.tv_sms_body.setBackgroundResource(R.drawable.bubble_yellow);
  
                      }
  
  
          holder.tv_contact.setText("" + item_phone_num.get(position));
  
  
          holder.tv_sms_body.setText(item_msg_body.get(position));
  
          holder.tv_time.setText(item_time.get(position));
  
          return v;
      }
  }
  
  public class ViewHolder {
  
      private TextView tv_contact;
      private TextView tv_sms_body;
      private TextView tv_time;
  
  }
  }
  </code></pre>