回答編集履歴
3
private 化し忘れ
answer
CHANGED
@@ -18,7 +18,7 @@
|
|
18
18
|
import java.util.Random;
|
19
19
|
|
20
20
|
public class MainActivity extends AppCompatActivity {
|
21
|
-
MainViewModel vm;
|
21
|
+
private MainViewModel vm;
|
22
22
|
|
23
23
|
@Override
|
24
24
|
protected void onCreate(Bundle savedInstanceState) {
|
2
リストコピーに修正
answer
CHANGED
@@ -163,16 +163,16 @@
|
|
163
163
|
public class MainViewModel extends ViewModel {
|
164
164
|
static final int DATA_COUNT_MAX = 120;
|
165
165
|
|
166
|
-
private List<RawData> rawDataList = new ArrayList<>();
|
167
|
-
private MutableLiveData<List<RawData>> rawDataLiveData = new MutableLiveData<>(Collections.
|
166
|
+
private MutableLiveData<List<RawData>> rawDataLiveData = new MutableLiveData<>(Collections.emptyList());
|
168
167
|
LiveData<List<RawData>> getValue() {
|
169
168
|
return rawDataLiveData;
|
170
169
|
}
|
171
170
|
|
172
171
|
void setValue(long time, float value) {
|
172
|
+
List<RawData> newList = new ArrayList(rawDataLiveData.getValue());
|
173
|
-
|
173
|
+
newList.add(new RawData(time, value));
|
174
|
-
while(
|
174
|
+
while(newList.size() > DATA_COUNT_MAX) newList.remove(0);
|
175
|
-
rawDataLiveData.setValue(
|
175
|
+
rawDataLiveData.setValue(Collections.unmodifiableList(newList));
|
176
176
|
}
|
177
177
|
}
|
178
178
|
```
|
1
nullポインタの可能性とマルチスレッドで危ないかもしれない点を少し修正(本当はcopyか...)
answer
CHANGED
@@ -1,11 +1,12 @@
|
|
1
1
|
以下の感じで4分間ダミーデータがグラフに表示されます。
|
2
2
|
MPAndroidChart の仕様を確認していませんので、 setEntries のデータの更新部分が(MPAndroidChart を分かっている人からすると)ダメかもしれません。
|
3
|
+
ViewPager を ViewPager2 にしています。
|
3
4
|
|
4
5
|
MainActivity.java
|
5
6
|
```java
|
6
7
|
package com.teratail.q371702;
|
7
8
|
|
8
|
-
import android.os.
|
9
|
+
import android.os.*;
|
9
10
|
import android.util.Log;
|
10
11
|
|
11
12
|
import com.google.android.material.tabs.*;
|
@@ -47,14 +48,16 @@
|
|
47
48
|
|
48
49
|
void feedMultiple() {
|
49
50
|
if (thread != null) thread.interrupt();
|
50
|
-
|
51
|
+
Handler handler = new Handler(getMainLooper());
|
51
52
|
thread = new Thread(new Runnable() {
|
52
53
|
Random random = new Random();
|
53
54
|
@Override
|
54
55
|
public void run() {
|
55
56
|
Log.d("","start.");
|
56
|
-
for (int i=0; i<
|
57
|
+
for (int i=0; i<MainViewModel.DATA_COUNT_MAX*2; i++) {
|
58
|
+
long time = System.currentTimeMillis();
|
57
|
-
|
59
|
+
float value = (float)(Math.random()*40);
|
60
|
+
handler.post(() -> vm.setValue(time, value));
|
58
61
|
try {
|
59
62
|
Thread.sleep(1000);
|
60
63
|
} catch (InterruptedException e) {
|
@@ -161,15 +164,15 @@
|
|
161
164
|
static final int DATA_COUNT_MAX = 120;
|
162
165
|
|
163
166
|
private List<RawData> rawDataList = new ArrayList<>();
|
164
|
-
private MutableLiveData<List<RawData>> rawDataLiveData = new MutableLiveData<>();
|
167
|
+
private MutableLiveData<List<RawData>> rawDataLiveData = new MutableLiveData<>(Collections.unmodifiableList(rawDataList));
|
165
168
|
LiveData<List<RawData>> getValue() {
|
166
169
|
return rawDataLiveData;
|
167
170
|
}
|
168
171
|
|
169
|
-
void
|
172
|
+
void setValue(long time, float value) {
|
170
173
|
rawDataList.add(new RawData(time, value));
|
171
174
|
while(rawDataList.size() > DATA_COUNT_MAX) rawDataList.remove(0);
|
172
|
-
rawDataLiveData.
|
175
|
+
rawDataLiveData.setValue(rawDataLiveData.getValue());
|
173
176
|
}
|
174
177
|
}
|
175
178
|
```
|