回答編集履歴

1 ソースコードの記述場所が間違っていたため

nori1508

nori1508 score 6

2017/06/18 01:41  投稿

ハンドラを用いて解決することができました。
参考までにソースを掲載します。
 
```java
```  
import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.widget.ArrayAdapter;
import android.widget.TextView;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.widget.ArrayAdapter;
import android.widget.TextView;
import com.mlkcca.client.DataElement;
import com.mlkcca.client.DataStore;
import com.mlkcca.client.DataStoreEventListener;
import com.mlkcca.client.MilkCocoa;
import com.mlkcca.client.DataStore;
import com.mlkcca.client.DataStoreEventListener;
import com.mlkcca.client.MilkCocoa;
public class MainActivity extends Activity implements DataStoreEventListener {
  String temp;
  int temp0;
  String temp2;
  float temp1;
  TextView t3;
  private MilkCocoa m_milkCocoa;
  private DataStore m_dataStore;
  private Handler handler = new Handler();
  private ArrayAdapter<String> adapter;
String temp;
int temp0;
String temp2;
float temp1;
TextView t3;
private MilkCocoa m_milkCocoa;
private DataStore m_dataStore;
private Handler handler = new Handler();
private ArrayAdapter<String> adapter;
   @Override
  protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);
      t3 = (TextView) findViewById(R.id.text3);
      m_milkCocoa = new MilkCocoa("MY_MILKCOCOA_APP_ID");
      m_dataStore = m_milkCocoa.dataStore("DATASTORE NAME");
      m_dataStore.addDataStoreEventListener(this);
      m_dataStore.on("push");
  }
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
t3 = (TextView) findViewById(R.id.text3);
m_milkCocoa = new MilkCocoa("MY_MILKCOCOA_APP_ID");
m_dataStore = m_milkCocoa.dataStore("DATASTORE NAME");
m_dataStore.addDataStoreEventListener(this);
m_dataStore.on("push");
}
@Override  
public void onPushed(DataElement dataElement) {  
final DataElement pushed = dataElement;  
new Thread(new Runnable() {  
@Override  
public void run() {  
handler.post(new Runnable() {  
@Override  
public void run() {  
String temp = pushed.getValue("temp");  
int temp0 = Integer.parseInt(temp);  
temp1 = (float) temp0 / 10;  
Log.i("MC", "温度は" + temp1 + "℃です");  
temp2 = "温度は" + temp1 + "℃です";  
t3.setText(temp2);  
}  
});  
}  
   @Override
   public void onPushed(DataElement dataElement) {
       final DataElement pushed = dataElement;
       new Thread(new Runnable() {
           @Override
           public void run() {
               handler.post(new Runnable() {
                   @Override
                   public void run() {
                       String temp = pushed.getValue("temp");
                       int temp0 = Integer.parseInt(temp);
                       temp1 = (float) temp0 / 10;
                       Log.i("MC", "温度は" + temp1 + "℃です");
                       temp2 = "温度は" + temp1 + "℃です";
                       t3.setText(temp2);
                   }
               });
           }
}).start();
}
       }).start();
  }
@Override
public void onSetted(DataElement dataElement) {
}  
   @Override
  public void onSetted(DataElement dataElement) {
@Override
public void onSended(DataElement dataElement) {
   }
}
@Override  
public void onRemoved(DataElement dataElement) {  
   @Override
   public void onSended(DataElement dataElement) {
}
   }
   @Override
   public void onRemoved(DataElement dataElement) {
   }
   @Override
   public void onPause() {
       super.onPause();
       finish();
       android.os.Process.killProcess(android.os.Process.myPid());
   }
}
@Override
public void onPause() {
super.onPause();
finish();
android.os.Process.killProcess(android.os.Process.myPid());
}
}
```

思考するエンジニアのためのQ&Aサイト「teratail」について詳しく知る