2012-11-03 50 views
1

我想改變的TextView後每1個minute.The問題是,它正在改變只是一日一次,在其他cases.This其餘同樣是在那裏我使用的是被稱爲每1可運行分鐘:的TextView得到更新一次

public void ReduceTimeIteration() 
{ 
    try 
    { 
     final Handler m_handler = new Handler(); 

     runOnUiThread(new Runnable() { 
      @Override 
      public void run() { 
       DiscussArrayAdapter.MyMethod(SplashActivity.this,(long)60000); 
       m_handler.postDelayed(this, 60000); 
      } 
     }); 
    } 
    catch(Exception e) 
    { 
     Log.e("Exception: ",e+" in ReduceTimeIteration() of SplashActivity.java"); 
    } 
} 

這就是我要更新類textviewTimeLeft

public class DiscussArrayAdapter extends ArrayAdapter<OneComment> { 

    private TextView countryName; 
    private List<OneComment> countries = new ArrayList<OneComment>(); 
    private LinearLayout wrapper; 
    public View row; 
    static OneComment coment; 
    static TextView TimeLeft; 

    @Override 
    public void add(OneComment object) 
    { 
     try 
     { 
      countries.add(object); 
      super.add(object); 
     } 
     catch(Exception e) 
     { 
      Log.e("Exception: ",e+" Exception occured in add() of DiscussArrayAdapter.java"); 
     } 
    } 

    public DiscussArrayAdapter(Context context, int textViewResourceId) { 
     super(context, textViewResourceId); 
    } 

    public int getCount() 
    { 
     try 
     { 
      return this.countries.size(); 
     } 
     catch(Exception e) 
     { 
      Log.e("Exception: ",e+" Exception occured in getCount() of DiscussArrayAdapter.java"); 
     } 
     return this.countries.size(); 
    } 

    public OneComment getItem(int index) 
    { 
     try 
     { 
      return this.countries.get(index); 
     } 
     catch(Exception e) 
     { 
      Log.e("Exception: ",e+" Exception occured in getItem() of DiscussArrayAdapter.java"); 
     } 
     return null;   
    } 

    public View getView(int position, View convertView, ViewGroup parent) 
    { 
     try 
     { 
      row = convertView; 
      if (row == null) { 
       LayoutInflater inflater = (LayoutInflater) this.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
       row = inflater.inflate(R.layout.listitem_discuss, parent, false); 
      } 

      wrapper = (LinearLayout) row.findViewById(R.id.wrapper); 

      coment = getItem(position); 

      countryName = (TextView) row.findViewById(R.id.comment); 

      countryName.setText(coment.comment); 

      TimeLeft = (TextView) row.findViewById(R.id.timeleft); 

      int hours,minutes,seconds; 

      if(coment.timeLeft > 0) // When the time is greater than 0 
      { 
       if(TimeLeft == null) 
       { 
        ((ViewGroup) row).addView(TimeLeft); 
       } 

            long t = coment.timeLeft - SecureMessagesActivity.TimeChecker(); 

       seconds = (int) (t/1000) % 60 ; 
       minutes = (int) (t/(1000*60)) % 60; 
       hours = minutes/60; 

       TimeLeft.setText("hello"); 
      } 
      else 
      { 
       if(TimeLeft != null) 
       { 
        ((ViewGroup)TimeLeft.getParent()).removeView(TimeLeft); 
       } 
      } 

      return row; 
     } 
     catch(Exception e) 
     { 
      Log.e("Exception: ",e+" Exception occured in View getView(int position, View convertView, ViewGroup parent) of DiscussArrayAdapter.java"); 
     } 
     return row; 
    } 

    public static void MyMethod(Activity activity,long time) 
    { 
     try 
     { 

       if(TimeLeft.getText().equals("hello")) 
       { 
        TimeLeft.setText("hi"); 
       } 
       else if(TimeLeft.getText().equals("hi")) 
       { 
        TimeLeft.setText("hello"); 
       } 

      Toast.makeText(activity, "Value of TextView is "+TimeLeft.getText() , Toast.LENGTH_SHORT).show(); 
     } 
     catch(Exception e) 
     { 
      Log.e("Exception: ", e+" Occured in MyMethod() of DiscussArrayAdapter.java"); 
     } 
    } 
} 

我不知道是什麼問題,因爲其中的TextView的timeleft正在改變它的價值只有一次而在其餘的情況下,它r保持一致。請幫助我。提前致謝。

+0

我覺得線程調用一次時間。 – ckpatel

+0

沒有先生,它每60秒調用一次。請看行m_handler.postDelayed(this,60000); – user1726619

+0

嘗試使用'TimeLeft.getText()。toString()。equals(「hello」)' – Pallavi

回答

2

我想試試這個代碼: -

int delay = 5000; // delay for 5 sec. 
    int period = 1000; // repeat every sec. 

    Timer timer = new Timer(); 
    timer.scheduleAtFixedRate(new TimerTask() 
     { 
      public void run() 
      { 
       //your code 
      } 
     }, delay, period); 

提供另一種解決方案

​​3210
+1

同樣的問題仍然存在。問題不在於處理程序的迭代,而是在每60秒迭代一次,甚至textview的值正在發生變化,因爲我在調試時注意到了這一點,但在仿真器的屏幕上,它並沒有改變除了處理程序的第一次迭代。 – user1726619