2016-01-21 28 views
0

我試圖顯示從微軟樂隊採取心率例如每20秒6次。在這種情況下,我創建了6個文本視圖,然後我將從樂隊中讀取並在6個文本視圖中顯示它。但是,如何在6個文本視圖中顯示全部6個讀數?到目前爲止,我只能在第一個文本視圖中顯示一個閱讀。任何想法如何使用可能爲循環或否則顯示其他5讀數每20秒?如何使用「for循環」或「if和else if」方法每隔X秒和X時間更新多個TextView?

我的代碼,把心臟率讀數並將其轉換爲字符串:

public void onHeartrateClick(View v){ 
    if(v.getId() == R.id.Bhistory){ 

     TextView e = (TextView)findViewById(R.id.txtHeart); 
     String string = e.getText().toString(); 

     Intent i = new Intent(Pulse.this, History.class); 
     i.putExtra("History", string); 
     startActivity(i); 
    } 

我在history.xml代碼顯示讀數:

public class History extends ActionBarActivity { 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_history); 

    String history = getIntent().getStringExtra("History"); 

    TextView textView = (TextView)findViewById(R.id.TVhistory); 
    textView.setText(history); 
} 

我History.xml圖片這顯示從帶中讀取的讀數:

enter image description here

希望能儘快獲得幫助,因爲我正在爲項目做這件事。謝謝:)

+0

是你的'閱讀'的字符串列表? – Boukharist

回答

2

你可以在你的activity onCreate()中使用下面的代碼。

public class History extends ActionBarActivity { 
TextView mTextView; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_history); 

    String history = getIntent().getStringExtra("History"); 

    mTextView = (TextView)findViewById(R.id.TVhistory); 
    mTextView .setText(history); 

    Handler handler=new Handler(); 

    final Runnable runnable=new Runnable() { 
     @Override 
     public void run() { 

      //put your textview update code here 
      mTextView .setText(history); 
      handler.postDelayed(this,1000);//reschedule this runnable 
     } 
    }; 

    handler.postDelayed(runnable,1000); 

    } 
} 
+0

感謝您的回覆!只是想問一些關於textview更新代碼的問題。 對於代碼,我必須編寫幾個textview代碼?例如: TextView textView =(TextView)findViewById(R.id.TVhistory); textView.setText(history); TextView textView =(TextView)findViewById(R.id.textView2); textView.setText(history); 依此類推? 或者應該是: TextView textView2 =(TextView)findViewById(R.id.textView2); textView.setText(history); 對不起編程新手,所以不得不問這麼多問題。 –

+0

希望儘快得到您的回覆。 –

+0

理想情況下,我們在活動課中創建一個字段來引用每個文本視圖...通常我們不使用相同的名稱代碼 –