質問編集履歴

1

コードの追加

2017/10/07 07:57

投稿

place
place

スコア12

test CHANGED
File without changes
test CHANGED
@@ -3,3 +3,221 @@
3
3
  これに1秒ごとに乱数で出力した整数を1つ追加し、一番古い整数は削除するという機能を追加したいのですが、
4
4
 
5
5
  どのようにすれば実現できますか?
6
+
7
+ 一秒ごとに何かを行う、と言うところまでは実装しましたが、データの移し変え?の方法が分かりません。
8
+
9
+
10
+
11
+ 環境はandroid studioです。
12
+
13
+
14
+
15
+ import android.annotation.TargetApi;
16
+
17
+ import android.os.Build;
18
+
19
+ import android.os.Bundle;
20
+
21
+ import android.os.Handler;
22
+
23
+ import android.os.Message;
24
+
25
+ import android.support.v7.app.AppCompatActivity;
26
+
27
+ import android.widget.TextView;
28
+
29
+
30
+
31
+ import java.util.Arrays;
32
+
33
+
34
+
35
+ public class MainActivity extends AppCompatActivity implements Runnable{
36
+
37
+
38
+
39
+ int Date[]=new int[100];
40
+
41
+ int i;
42
+
43
+ int sum=0 ;
44
+
45
+ Double ans;
46
+
47
+
48
+
49
+ public static String testtext;
50
+
51
+ private Thread mThread;
52
+
53
+ public static Handler mHandler;
54
+
55
+ private TextView mTextView;
56
+
57
+ public TextView Testtext;
58
+
59
+ public TextView Testtext2;
60
+
61
+ public static long count = 0;
62
+
63
+
64
+
65
+
66
+
67
+ int timing=1000;
68
+
69
+
70
+
71
+ @TargetApi(Build.VERSION_CODES.LOLLIPOP)
72
+
73
+ @Override
74
+
75
+ protected void onCreate(Bundle savedInstanceState) {
76
+
77
+ super.onCreate(savedInstanceState);
78
+
79
+ setContentView(R.layout.activity_main);
80
+
81
+
82
+
83
+ for(i=0;i<100;i++){
84
+
85
+ Date[i]= (int)( (Math.random()*100));
86
+
87
+ sum+=Date[i];
88
+
89
+ }
90
+
91
+ ans=(double)(sum/100);
92
+
93
+
94
+
95
+ mTextView = (TextView)findViewById(R.id.textView);
96
+
97
+ Testtext = (TextView)findViewById(R.id.textView2);
98
+
99
+ Testtext2 = (TextView)findViewById(R.id.textView3);
100
+
101
+
102
+
103
+ Testtext.setText(Arrays.toString(Date));
104
+
105
+ Testtext2.setText(String.valueOf(ans));
106
+
107
+ mHandler = new Handler() {
108
+
109
+ //メッセージ受信
110
+
111
+ public void handleMessage(android.os.Message message) {
112
+
113
+ //メッセージの表示
114
+
115
+ String text = (String) message.obj;
116
+
117
+ if(message.what==0) {
118
+
119
+ mTextView.setText(text);
120
+
121
+ }
122
+
123
+ if(message.what==2) {
124
+
125
+ Testtext.setText(text);
126
+
127
+ }
128
+
129
+
130
+
131
+ //メッセージの種類に応じてswitch文で制御すれば
132
+
133
+ //イベント制御に利用可能
134
+
135
+ };
136
+
137
+
138
+
139
+
140
+
141
+ };
142
+
143
+ }
144
+
145
+
146
+
147
+ @Override
148
+
149
+ public void onResume(){
150
+
151
+ super.onResume();
152
+
153
+ mThread = new Thread(this);
154
+
155
+ //スレッド処理を開始
156
+
157
+ if(mThread != null ){
158
+
159
+ mThread.start();
160
+
161
+ }
162
+
163
+ count=0;
164
+
165
+ }
166
+
167
+ @Override
168
+
169
+ public void onPause(){
170
+
171
+ super.onPause();
172
+
173
+ //スレッドを削除
174
+
175
+ mThread = null;
176
+
177
+ }
178
+
179
+ //スレッドによる更新処理
180
+
181
+ public void run() {
182
+
183
+ long time = System.currentTimeMillis();
184
+
185
+
186
+
187
+ while (mThread != null) {
188
+
189
+ long now = System.currentTimeMillis();
190
+
191
+ if(now - time > timing){
192
+
193
+ //Message msg = new Message();
194
+
195
+ //Message msg = Message.obtain();
196
+
197
+ Message msg = mHandler.obtainMessage();
198
+
199
+ msg.obj = new String("ループが"+ count + "回終了しました");
200
+
201
+ //ハンドラへのメッセージ送信
202
+
203
+ mHandler.sendMessage(msg);
204
+
205
+ //スレッドの利用変数を初期化
206
+
207
+ time = now;
208
+
209
+ count++;
210
+
211
+ }
212
+
213
+ }
214
+
215
+ }
216
+
217
+
218
+
219
+
220
+
221
+
222
+
223
+ }