2011-08-09 8 views
0

5秒ごとにTextViewsをリフレッシュする必要がありますが、私はTimerTaskを作成しました(contrsuctorでアクティビティをパラメータとして設定しました)が、ウィジェットを作成したスレッドからのみアクセスできます。更新TExtViewsの問題 - TimerTaskを拡張する代わりに

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.system_data_show); 
    currentActivity = this; 
    Timer timer = new Timer(); 
    timer.schedule(new SystemTimerTask(currentActivity),500, 5000); 
    ///////////////// text views from layout ///////////////////////////////////////////// 
    TextView txtCapture = (TextView) this.findViewById(R.id.txtCapture); 
    txtCapture.setText("System Data"); 
    TextView txtWorkAllowedValue=(TextView)this.findViewById(R.id.workAllowedValue); 
    TextView txtBusBroken0Value=(TextView)this.findViewById(R.id.busBroken0Value); 
    TextView txtBusBroken1Value=(TextView)this.findViewById(R.id.busBroken1Value); 
    TextView txtShortCircuit0Value=(TextView)this.findViewById(R.id.shortCircuit0Value); 
    TextView txtShortCircuit1Value=(TextView)this.findViewById(R.id.shortCircuit1Value); 
    TextView txtErrorConfigurationValue=(TextView)this.findViewById(R.id.errorConfigurationValue); 
    } 

5秒ごとにこのテキストを更新する方法はありますか? TimerTaskを拡張しないようにする方法はありますか?私のSActivityがthatInterfaceを実装するように、多少のインターフェースを実装しているかもしれません。

答えて

1

あなたができることは、バックグラウンドで作業する(テキストビューをリフレッシュするデータを取得する)内部AsyncTaskクラスを作成し、その作業が完了したときですバックグラウンドでは、結果をonPostExecute(ここでUIのtextviewを更新できます)メソッドに投稿します。

あなたはAsyncTask内部クラスは、あなたがこのようなあなたのタイマー5秒ごとに何かをすることを持っている後:

new MyAsyncTask.execute();

1

HandlerとそのpostDelayed()機能を使用できます。このようにして、タイミングのためだけに別のスレッドを作成する必要はありません。たとえば、以下を参照してください。これはあなたがまたView.post(Runnable)を使用することができます必要とするものに適合しない場合はAndroid developer pageAPI Doc page

で説明したようRepeat a task with a time delay?

1

あなたはタイマースレッドでUIコンポーネントを更新するためにActivity.runOnUiThread(Runnable)を使用することができますメソッド(上記の開発者ページでも説明します)

関連する問題