title: Android listview timer run in every second
body: <p>I have a issue with using a timer on a <code>listview</code>. 
  In the list item I showed using sqlite values. There is a <code>textview</code> which showing time difference of last updated time of the data and current time. i have to show it in every one second. so the user can know how long he updated the record.</p>
  
  <p>I tried this in several ways.</p>
  
  <h2>First way</h2>
  
  <p>I tried to add timer in <code>adapter</code> class. so for every item new timer is created. so application crashed because of many timers run simultaneously.</p>
  
  <h2>Second way</h2>
  
  <p>I tried using adapter.notifyDataSetChanged() way. Like as this.</p>
  
  <pre><code>Handler timerHandler = new Handler();
  Runnable timerRunnable = new Runnable() {
      @Override
      public void run() { 
          if (adapterChatThread != null) {
              adapter.notifyDataSetChanged();
          }
          timerHandler.postDelayed(this, 1000); // run every second
      }
  };
  timerRunnable.run();
  </code></pre>
  
  <p>I move to another activity when click on list item and user can come back to this <code>Activity</code>.</p>
  
  <p>so in Onresume I used</p>
  
  <pre><code>timerHandler.postDelayed(timerRunnable, 500);
  </code></pre>
  
  <p>and OnPause</p>
  
  <pre><code>timerHandler.removeCallbacks(timerRunnable);
  </code></pre>
  
  <p>Issue is data is not showing well. I mean in every second data difference is not one second. some time differnce is 2sec, 5 sec, .. etc.</p>
  
  <p>means timer is not working as I expected.</p>
  
  <h1>Third way</h1>
  
  <p>I used a asynctask and call it in every second using a timer.</p>
  
  <pre><code>class ThreadTimer extends AsyncTask&lt;Void, Void, Void&gt; {
      @Override
      protected Void doInBackground(Void... params) {
          return null;
      }
  
      @Override
      protected void onPostExecute(Void result) {
          if (adapter != null)
              adapter.notifyDataSetChanged();
          super.onPostExecute(result);
      }
      }
  </code></pre>
  
  <p>I called this as in here</p>
  
  <pre><code>Handler timerHandler = new Handler();
  Runnable timerRunnable = new Runnable() {
      @Override
      public void run() { 
          new ThreadTimer().execute();
          timerHandler.postDelayed(this, 1000); // run every second
      }
  };
  timerRunnable.run();
  </code></pre>
  
  <p>previous issue triggered. (data not showing well)</p>
  
  <h2>Fourth way</h2>
  
  <p>Using <code>AsyncTask</code> as this</p>
  
  <pre><code>class ThreadTimer extends AsyncTask&lt;Void, Void, Void&gt; {
      void Sleep(int ms) {
          try {
              Thread.sleep(ms);
          } catch (Exception e) {
          }
      }
  
      @Override
      protected Void doInBackground(Void... params) {
          while (threadRun) {
              Sleep(1000);
              return null;
          }
          return null;
      }
  
      @Override
      protected void onPostExecute(Void result) {
          adapter.notifyDataSetChanged();
          super.onPostExecute(result);
      }
  }
  </code></pre>
  
  <p>I called this class in OnResume.
  In on pause I set threadRun= false;</p>
  
  <p>issue is same.</p>
  
  <p>please help me.</p>
  
  <p>My requirement is update list item in every second.</p>
  
  <p>Thank you.</p>
  
  <h2>edit</h2>
  
  <p>here is my adapter class textview update code.</p>
  
  <pre><code>Date lastUpdatedTime;
  final ChatThreadDAO ctd = new ChatThreadDAO();
  long timeForNextResponse = ctd.getLastRespondedTime(vct.get(position).getThread_id());
  
  try {
      if (vct.get(position).getThread_read_status() == 1 &amp;&amp; timeForNextResponse &gt; 0) {
          final long respTime = timeForNextResponse;
          SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
          lastUpdatedTime = formatter.parse(vct.get(position).getLast_updated_time());
          final long timeDiff = (new Date()).getTime() - lastUpdatedTime.getTime();
          if (timeDiff &lt;= respTime) {
              timeForNextResponse = respTime - timeDiff;
              ctd.updateTimeRespondToLastMsg(vct.get(position).getThread_id(), timeForNextResponse);
              holder.tvChatTimer.setVisibility(View.VISIBLE);
              holder.tvChatTimer.setText(timeForNextResponse / 1000 + "");
          } else {
              ctd.updateTimeRespondToLastMsg(vct.get(position).getThread_id(), 0);
          }
      } else {
          holder.tvChatTimer.setVisibility(View.INVISIBLE);
      }
  } catch (ParseException e) {
      e.printStackTrace();
  }
  </code></pre>
  
  <p>here vct is </p>
  
  <p>Vector vct;</p>
  
  <p>I assign the values to vector in adapter class constructer.</p>