質問編集履歴

5

ソースコードにコメントの追加

2019/05/23 01:46

投稿

sekaikan_ozaki
sekaikan_ozaki

スコア65

test CHANGED
File without changes
test CHANGED
@@ -20,9 +20,11 @@
20
20
 
21
21
  1.CSVファイルを読み込んでおく
22
22
 
23
+ 2.画面右上で条件を選択
24
+
23
- 2.ExitTextで文字を入力してbuttonをクリック
25
+ 3.ExitTextで文字を入力して検索ボタンをクリック
24
-
26
+
25
- 3.入力された文字がCSVファイルの中にあれば、「その文字」&「同じ行の違う列の対応する情報」をTextViewで出力
27
+ 4.入力された文字がCSVファイルの中にあれば、「その文字」&「同じ行の違う列の対応する情報」をTextViewで出力
26
28
 
27
29
 
28
30
 
@@ -30,13 +32,13 @@
30
32
 
31
33
 
32
34
 
33
- 2までは一応実装済みです。
35
+ 3までは一応実装済みです。
34
36
 
35
37
 
36
38
 
37
39
  写真はとりあえず、中身をすべて表示しているだけです。
38
40
 
39
- ![![イメージ説明](e88aa096405163146d21776cbeb5726a.png)](678e3e42fc260f0b1bfd82ec18e884be.png)
41
+ ![![イメージ説明](e88aa096405163146d21776cbeb5726a.png)
40
42
 
41
43
 
42
44
 
@@ -46,7 +48,7 @@
46
48
 
47
49
 
48
50
 
49
- 現時点でのソースコードはんな感じです。
51
+ 現時点でのソースコードはんな感じです。
50
52
 
51
53
 
52
54
 
@@ -102,6 +104,8 @@
102
104
 
103
105
 
104
106
 
107
+ //リストの宣言
108
+
105
109
  private ArrayList<String> timeList;
106
110
 
107
111
  private ArrayList<String> tempList;
@@ -110,6 +114,8 @@
110
114
 
111
115
 
112
116
 
117
+ //アダプターの宣言
118
+
113
119
  private ArrayAdapter arrayAdapter;
114
120
 
115
121
  private ArrayAdapter arrayAdapter2;
@@ -118,21 +124,21 @@
118
124
 
119
125
 
120
126
 
121
- private TextView textView;
127
+ private TextView textView;//適合した際に文章を表示
122
-
128
+
123
- private EditText editText;
129
+ private EditText editText;//時刻を入力
124
-
130
+
125
- String text;
131
+ String text;//入力内容
126
-
132
+
127
- String item;
133
+ String item;//気温or電車の本数
128
-
129
-
130
-
131
-
132
-
134
+
135
+
136
+
137
+
138
+
133
- private String spinnerItems[] = {"選択", "気温", "電車"};
139
+ private String spinnerItems[] = {"選択", "気温", "電車"};//スピナー
134
-
140
+
135
- private TextView SpinnertextView;
141
+ //private TextView SpinnertextView;//現在のスピナーで選んでるやつの表示用
136
142
 
137
143
 
138
144
 
@@ -162,7 +168,7 @@
162
168
 
163
169
 
164
170
 
165
- //アプリに表示するためのインスタンスを作成
171
+ //アプリにCSVの中身を表示するためのインスタンスを作成
166
172
 
167
173
  ListView listView = (ListView) findViewById(R.id.list_view1);
168
174
 
@@ -224,7 +230,7 @@
224
230
 
225
231
  //検索条件(気温or電車の本数)を選択
226
232
 
227
- SpinnertextView = findViewById(R.id.sp_text_view);
233
+ //SpinnertextView = findViewById(R.id.spinner_text_view);
228
234
 
229
235
  Spinner spinner = findViewById(R.id.spinner);
230
236
 
@@ -268,7 +274,7 @@
268
274
 
269
275
  item = (String)spinner.getSelectedItem();
270
276
 
271
- SpinnertextView.setText(item);
277
+ //SpinnertextView.setText(item);
272
278
 
273
279
 
274
280
 

4

コード、画像、内容の修正

2019/05/23 01:46

投稿

sekaikan_ozaki
sekaikan_ozaki

スコア65

test CHANGED
File without changes
test CHANGED
@@ -36,13 +36,13 @@
36
36
 
37
37
  写真はとりあえず、中身をすべて表示しているだけです。
38
38
 
39
- ![イメージ説明](83d00e56dec1f09804c065feaf12d100.png)
39
+ ![![イメージ説明](e88aa096405163146d21776cbeb5726a.png)](678e3e42fc260f0b1bfd82ec18e884be.png)
40
40
 
41
41
 
42
42
 
43
43
  具体的には、
44
44
 
45
- ほしい情報として「気温」を選択しているときに、「1時」と入力すると、「1時」&「21度」がTextViewで出力されるみたいな感じです。
45
+ ほしい情報として画面右上の選択で「気温」を選択しているときに、「1時」と入力すると、「1時」&「21度」がTextViewで出力されるみたいな感じです。
46
46
 
47
47
 
48
48
 
@@ -62,6 +62,8 @@
62
62
 
63
63
  import android.view.View;
64
64
 
65
+ import android.widget.AdapterView;
66
+
65
67
  import android.widget.ArrayAdapter;
66
68
 
67
69
  import android.widget.Button;
@@ -70,6 +72,8 @@
70
72
 
71
73
  import android.widget.ListView;
72
74
 
75
+ import android.widget.Spinner;
76
+
73
77
  import android.widget.TextView;
74
78
 
75
79
 
@@ -120,7 +124,15 @@
120
124
 
121
125
  String text;
122
126
 
123
-
127
+ String item;
128
+
129
+
130
+
131
+
132
+
133
+ private String spinnerItems[] = {"選択", "気温", "電車"};
134
+
135
+ private TextView SpinnertextView;
124
136
 
125
137
 
126
138
 
@@ -210,7 +222,75 @@
210
222
 
211
223
 
212
224
 
225
+ //検索条件(気温or電車の本数)を選択
226
+
227
+ SpinnertextView = findViewById(R.id.sp_text_view);
228
+
229
+ Spinner spinner = findViewById(R.id.spinner);
230
+
231
+
232
+
233
+ // ArrayAdapter
234
+
235
+ ArrayAdapter<String> adapter
236
+
237
+ = new ArrayAdapter<>(this,
238
+
239
+ android.R.layout.simple_spinner_item, spinnerItems);
240
+
241
+
242
+
243
+ //ユーザーが選択しようとするときにドロップダウンによりリストを表示するのに使うレイアウトを指定
244
+
245
+ adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
246
+
247
+
248
+
249
+ // spinner に adapter をセット
250
+
251
+ spinner.setAdapter(adapter);
252
+
253
+
254
+
255
+ // リスナーを登録
256
+
257
+ spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
258
+
259
+ // アイテムが選択された時
260
+
261
+ @Override
262
+
263
+ public void onItemSelected(AdapterView<?> parent,
264
+
265
+ View view, int position, long id) {
266
+
267
+ Spinner spinner = (Spinner)parent;
268
+
269
+ item = (String)spinner.getSelectedItem();
270
+
271
+ SpinnertextView.setText(item);
272
+
273
+
274
+
275
+
276
+
277
+ }
278
+
279
+ // アイテムが選択されなかった
280
+
281
+ public void onNothingSelected(AdapterView<?> parent) {
282
+
283
+ //
284
+
285
+ }
286
+
287
+ });
288
+
289
+
290
+
291
+
292
+
213
- //ボタンが押されたら
293
+ //検索ボタンが押されたら
214
294
 
215
295
  button.setOnClickListener(new View.OnClickListener() {
216
296
 
@@ -222,11 +302,9 @@
222
302
 
223
303
  text = editText.getText().toString();
224
304
 
225
- // 取得したテキストを TextView に張り付ける
226
-
227
- //textView.setText(text);
305
+ //入力された時刻と適合する時刻を探すメソッド
306
+
228
-
307
+ match(text);
229
-
230
308
 
231
309
 
232
310
 
@@ -234,16 +312,6 @@
234
312
 
235
313
  });
236
314
 
237
-
238
-
239
- //入力された時刻と適合する時刻を探すメソッド
240
-
241
- match(text);
242
-
243
-
244
-
245
-
246
-
247
315
  }
248
316
 
249
317
 
@@ -320,37 +388,29 @@
320
388
 
321
389
  if(text == s){
322
390
 
323
-
324
-
325
- // 取得したテキストを TextView に張り付ける
391
+ if(item == spinnerItems[1]){//スピナーで選択した条件が「気温」だったら
326
-
392
+
327
- textView.setText("text");
393
+ textView.setText(text+"の"+spinnerItems[1]+"は○○です");
394
+
328
-
395
+ }else if(item == spinnerItems[2]){//条件が「電車の本数」だったら
396
+
329
-
397
+ textView.setText(text+"の"+spinnerItems[2]+"は○○です");
330
-
398
+
331
- break;
399
+ }
332
400
 
333
401
  }
334
402
 
335
-
336
-
337
403
  }
338
404
 
339
-
340
-
341
405
  }
342
406
 
343
-
344
-
345
-
346
-
347
407
  }
348
408
 
349
409
  ```
350
410
 
351
411
 
352
412
 
353
- 現在のところ、edittextから受け取った文字をmatchメソッドにわたして、matchメソッドの中で、CSVファイルの時刻情報だけをfor文で回したときに、入力情報と一致すれば画面一番上に、「一致成功」りあえず表示させたいのですが、アルゴリズムがおかしいせいか「1時」と入力しても画面上に何も表示されません。。。
413
+ 現在のところ、edittextから受け取った文字をmatchメソッドにわたして、matchメソッドの中で、CSVファイルの時刻情報だけをfor文で回したときに、入力情報と一致すれば画面に、気温or電車の本数の情報をとりあえず表示させたいのですが、アルゴリズムがおかしいせいか「1時」と入力しても画面上に何も表示されません。。。
354
414
 
355
415
 
356
416
 

3

コードの追記、内容の一部変更

2019/05/23 01:18

投稿

sekaikan_ozaki
sekaikan_ozaki

スコア65

test CHANGED
File without changes
test CHANGED
@@ -84,6 +84,8 @@
84
84
 
85
85
  import java.util.ArrayList;
86
86
 
87
+ import java.util.Iterator;
88
+
87
89
  import java.util.List;
88
90
 
89
91
  import java.util.StringTokenizer;
@@ -96,11 +98,19 @@
96
98
 
97
99
 
98
100
 
99
- private ArrayList timeList;
101
+ private ArrayList<String> timeList;
100
-
102
+
101
- private ArrayList tempList;
103
+ private ArrayList<String> tempList;
102
-
104
+
103
- private ArrayList amountList;
105
+ private ArrayList<String> amountList;
106
+
107
+
108
+
109
+ private ArrayAdapter arrayAdapter;
110
+
111
+ private ArrayAdapter arrayAdapter2;
112
+
113
+ private ArrayAdapter arrayAdapter3;
104
114
 
105
115
 
106
116
 
@@ -108,6 +118,8 @@
108
118
 
109
119
  private EditText editText;
110
120
 
121
+ String text;
122
+
111
123
 
112
124
 
113
125
 
@@ -122,20 +134,24 @@
122
134
 
123
135
 
124
136
 
125
-
137
+ //フレームワーク作成
126
-
138
+
127
- timeList = new ArrayList<>();
139
+ timeList = new ArrayList<String>();
128
-
140
+
129
- tempList = new ArrayList<>();
141
+ tempList = new ArrayList<String>();
130
-
142
+
131
- amountList = new ArrayList<>();
143
+ amountList = new ArrayList<String>();
144
+
145
+
146
+
132
-
147
+ //CSVを読み込んでおく
133
-
134
148
 
135
149
  readCSV();
136
150
 
137
151
 
138
152
 
153
+ //アプリに表示するためのインスタンスを作成
154
+
139
155
  ListView listView = (ListView) findViewById(R.id.list_view1);
140
156
 
141
157
  ListView listView2 = (ListView) findViewById(R.id.list_view2);
@@ -144,19 +160,25 @@
144
160
 
145
161
 
146
162
 
163
+ //時刻の情報
164
+
147
- ArrayAdapter arrayAdapter =
165
+ arrayAdapter =
148
166
 
149
167
  new ArrayAdapter(
150
168
 
151
169
  this, R.layout.list_item_layout , timeList);
152
170
 
171
+ //気温の情報
172
+
153
- ArrayAdapter arrayAdapter2 =
173
+ arrayAdapter2 =
154
174
 
155
175
  new ArrayAdapter(
156
176
 
157
177
  this, R.layout.list_item_layout , tempList);
158
178
 
179
+ //電車の本数
180
+
159
- ArrayAdapter arrayAdapter3 =
181
+ arrayAdapter3 =
160
182
 
161
183
  new ArrayAdapter(
162
184
 
@@ -164,6 +186,8 @@
164
186
 
165
187
 
166
188
 
189
+ //画面内に表示
190
+
167
191
  listView.setAdapter(arrayAdapter);
168
192
 
169
193
  listView2.setAdapter(arrayAdapter2);
@@ -172,17 +196,21 @@
172
196
 
173
197
 
174
198
 
175
-
199
+ //文字を入力
176
200
 
177
201
  editText = findViewById(R.id.edit_text);
178
202
 
203
+ //入力した文字を表示する為の準備
204
+
179
205
  textView = findViewById(R.id.text_view);
180
206
 
207
+ //ボタンの作成
208
+
181
209
  Button button = findViewById(R.id.button);
182
210
 
183
211
 
184
212
 
185
-
213
+ //ボタンが押されたら
186
214
 
187
215
  button.setOnClickListener(new View.OnClickListener() {
188
216
 
@@ -190,18 +218,32 @@
190
218
 
191
219
  public void onClick(View v) {
192
220
 
193
- // エディットテキストのテキストを取得
221
+ // エディットテキストの文字を取得
194
-
222
+
195
- String text = editText.getText().toString();
223
+ text = editText.getText().toString();
196
224
 
197
225
  // 取得したテキストを TextView に張り付ける
198
226
 
199
- textView.setText(text);
227
+ //textView.setText(text);
228
+
229
+
230
+
231
+
200
232
 
201
233
  }
202
234
 
203
235
  });
204
236
 
237
+
238
+
239
+ //入力された時刻と適合する時刻を探すメソッド
240
+
241
+ match(text);
242
+
243
+
244
+
245
+
246
+
205
247
  }
206
248
 
207
249
 
@@ -264,34 +306,54 @@
264
306
 
265
307
  }
266
308
 
309
+
310
+
311
+ //入力された文字(時刻)と適合する時刻を探すメソッド
312
+
313
+ public void match(String text){
314
+
315
+ //時刻の情報を1番目から最後まで回す
316
+
317
+ for(String s : timeList){
318
+
319
+ //入力情報と一致したら
320
+
321
+ if(text == s){
322
+
323
+
324
+
325
+ // 取得したテキストを TextView に張り付ける
326
+
327
+ textView.setText("text");
328
+
329
+
330
+
331
+ break;
332
+
333
+ }
334
+
335
+
336
+
337
+ }
338
+
339
+
340
+
341
+ }
342
+
343
+
344
+
345
+
346
+
267
347
  }
268
348
 
269
349
  ```
270
350
 
271
351
 
272
352
 
273
- この続きとして、実装したいことは、
274
-
275
- // エディットテキストのテキストを取得
276
-
277
- String text = editText.getText().toString();
278
-
279
-
280
-
281
- 1.このtext変数をどうやって、時間(1時とか2時とか)と一つ一つ照らし合わせるか。
282
-
283
- 2.「1時」からどうやって気温の「21度」を引っ張ってくるか。
284
-
285
-
286
-
287
- この2つです。
288
-
289
-
290
-
291
- 1.して、for文で配列?arrayAdapterの「時入力された文字(時間)が一致するで、回し続ける
353
+ 現在のところ、edittextから受け取った文字をmatchメソッドわたして、matchメソッドの中で、CSVファイルの時刻情報だけをfor文で回したときに、入力情報と一致すれば画面一番上に、「一致成功」ととりあえず表示させたいですが、アルゴリズムがおかしいせいか1時」入力しても画面上に何も表示されません。。
292
-
293
- 2.に関しては具体的な解決法が思いつかないです・・・
354
+
294
-
295
-
296
-
355
+
356
+
357
+
358
+
297
- なにか解決法はありまでしょうか?
359
+ 間違っている箇所がありましたら教えていただけませんでしょうか?

2

内容の部分修正

2019/05/22 07:50

投稿

sekaikan_ozaki
sekaikan_ozaki

スコア65

test CHANGED
File without changes
test CHANGED
@@ -30,6 +30,10 @@
30
30
 
31
31
 
32
32
 
33
+ 2までは一応実装済みです。
34
+
35
+
36
+
33
37
  写真はとりあえず、中身をすべて表示しているだけです。
34
38
 
35
39
  ![イメージ説明](83d00e56dec1f09804c065feaf12d100.png)

1

ソースコード、内容の修正

2019/05/22 05:12

投稿

sekaikan_ozaki
sekaikan_ozaki

スコア65

test CHANGED
File without changes
test CHANGED
@@ -1,8 +1,16 @@
1
+ PCスペック
2
+
1
3
  Windows7 64bit
2
4
 
5
+ メモリ 8GB
6
+
7
+ プロセッサ Intel Core i7-3610
8
+
9
+
10
+
3
- Android studio 3.4
11
+ Android Studio 3.4
4
-
5
- 実機なし、エミュレータでデモ
12
+
13
+
6
14
 
7
15
 
8
16
 
@@ -12,9 +20,9 @@
12
20
 
13
21
  1.CSVファイルを読み込んでおく
14
22
 
15
- 2.ExitTextで文字を入力
23
+ 2.ExitTextで文字を入力してbuttonをクリック
16
-
24
+
17
- 3.入力された文字がCSVファイルの中にあれば、同じ行の違う列の対応する情報をTextViewで出力
25
+ 3.入力された文字がCSVファイルの中にあれば、「その文字」&「同じ行の違う列の対応する情報をTextViewで出力
18
26
 
19
27
 
20
28
 
@@ -22,22 +30,264 @@
22
30
 
23
31
 
24
32
 
25
- |///|気温|電車数|
26
-
27
- |1時|15度|12本|
28
-
29
- |2時|16度|8本|
30
-
31
- |3時|21度|9本|
32
-
33
- |4|18|10本|
34
-
35
-
36
-
37
- やりたいこと
38
-
39
- 気温を選択しているときに、「1時」と入力すると、「15度」がTextViewで出力されるみたいな感じです。
40
-
41
-
42
-
43
- そもそもCSVの読み込み方すら分からないので、そこから教えていただけませんでしょうか?
33
+ 写真はとりあえず、中身をすべて表示しているだけです。
34
+
35
+ ![イメージ説明](83d00e56dec1f09804c065feaf12d100.png)
36
+
37
+
38
+
39
+ 具体的には、
40
+
41
+ ほしい情報として「気温」を選択しているときに、「1」と入力すると、「1時」&「21」がTextViewで出力されるみたいな感じです。
42
+
43
+
44
+
45
+ 現時点でのソースコードかんな感じです。
46
+
47
+
48
+
49
+ ```java
50
+
51
+ package com.example.csvread3;
52
+
53
+
54
+
55
+ import android.support.v7.app.AppCompatActivity;
56
+
57
+ import android.os.Bundle;
58
+
59
+ import android.view.View;
60
+
61
+ import android.widget.ArrayAdapter;
62
+
63
+ import android.widget.Button;
64
+
65
+ import android.widget.EditText;
66
+
67
+ import android.widget.ListView;
68
+
69
+ import android.widget.TextView;
70
+
71
+
72
+
73
+ import java.io.BufferedReader;
74
+
75
+ import java.io.IOException;
76
+
77
+ import java.io.InputStream;
78
+
79
+ import java.io.InputStreamReader;
80
+
81
+ import java.util.ArrayList;
82
+
83
+ import java.util.List;
84
+
85
+ import java.util.StringTokenizer;
86
+
87
+
88
+
89
+
90
+
91
+ public class MainActivity extends AppCompatActivity {
92
+
93
+
94
+
95
+ private ArrayList timeList;
96
+
97
+ private ArrayList tempList;
98
+
99
+ private ArrayList amountList;
100
+
101
+
102
+
103
+ private TextView textView;
104
+
105
+ private EditText editText;
106
+
107
+
108
+
109
+
110
+
111
+ @Override
112
+
113
+ protected void onCreate(Bundle savedInstanceState) {
114
+
115
+ super.onCreate(savedInstanceState);
116
+
117
+ setContentView(R.layout.activity_main);
118
+
119
+
120
+
121
+
122
+
123
+ timeList = new ArrayList<>();
124
+
125
+ tempList = new ArrayList<>();
126
+
127
+ amountList = new ArrayList<>();
128
+
129
+
130
+
131
+ readCSV();
132
+
133
+
134
+
135
+ ListView listView = (ListView) findViewById(R.id.list_view1);
136
+
137
+ ListView listView2 = (ListView) findViewById(R.id.list_view2);
138
+
139
+ ListView listView3 = (ListView) findViewById(R.id.list_view3);
140
+
141
+
142
+
143
+ ArrayAdapter arrayAdapter =
144
+
145
+ new ArrayAdapter(
146
+
147
+ this, R.layout.list_item_layout , timeList);
148
+
149
+ ArrayAdapter arrayAdapter2 =
150
+
151
+ new ArrayAdapter(
152
+
153
+ this, R.layout.list_item_layout , tempList);
154
+
155
+ ArrayAdapter arrayAdapter3 =
156
+
157
+ new ArrayAdapter(
158
+
159
+ this, R.layout.list_item_layout , amountList);
160
+
161
+
162
+
163
+ listView.setAdapter(arrayAdapter);
164
+
165
+ listView2.setAdapter(arrayAdapter2);
166
+
167
+ listView3.setAdapter(arrayAdapter3);
168
+
169
+
170
+
171
+
172
+
173
+ editText = findViewById(R.id.edit_text);
174
+
175
+ textView = findViewById(R.id.text_view);
176
+
177
+ Button button = findViewById(R.id.button);
178
+
179
+
180
+
181
+
182
+
183
+ button.setOnClickListener(new View.OnClickListener() {
184
+
185
+ @Override
186
+
187
+ public void onClick(View v) {
188
+
189
+ // エディットテキストのテキストを取得
190
+
191
+ String text = editText.getText().toString();
192
+
193
+ // 取得したテキストを TextView に張り付ける
194
+
195
+ textView.setText(text);
196
+
197
+ }
198
+
199
+ });
200
+
201
+ }
202
+
203
+
204
+
205
+ /**
206
+
207
+ * CSVの読み込み
208
+
209
+ */
210
+
211
+ public void readCSV() {
212
+
213
+ try {
214
+
215
+ InputStream inputStream =
216
+
217
+ getResources().getAssets().open("data1.csv");
218
+
219
+
220
+
221
+ InputStreamReader inputStreamReader =
222
+
223
+ new InputStreamReader(inputStream);
224
+
225
+
226
+
227
+ BufferedReader bufferReader =
228
+
229
+ new BufferedReader(inputStreamReader);
230
+
231
+
232
+
233
+ String line = "";
234
+
235
+
236
+
237
+ while ((line = bufferReader.readLine()) != null) {
238
+
239
+ StringTokenizer stringTokenizer =
240
+
241
+ new StringTokenizer(line, ",");
242
+
243
+
244
+
245
+ timeList.add(stringTokenizer.nextToken());
246
+
247
+ tempList.add(stringTokenizer.nextToken());
248
+
249
+ amountList.add(stringTokenizer.nextToken());
250
+
251
+ }
252
+
253
+ bufferReader.close();
254
+
255
+ } catch (IOException e) {
256
+
257
+ e.printStackTrace();
258
+
259
+ }
260
+
261
+ }
262
+
263
+ }
264
+
265
+ ```
266
+
267
+
268
+
269
+ この続きとして、実装したいことは、
270
+
271
+ // エディットテキストのテキストを取得
272
+
273
+ String text = editText.getText().toString();
274
+
275
+
276
+
277
+ 1.このtext変数をどうやって、時間(1時とか2時とか)と一つ一つ照らし合わせるか。
278
+
279
+ 2.「1時」からどうやって気温の「21度」を引っ張ってくるか。
280
+
281
+
282
+
283
+ この2つです。
284
+
285
+
286
+
287
+ 1.に関しては、for文で配列?のarrayAdapterの「時間」を入力された文字(時間)が一致するまで、回し続ける。
288
+
289
+ 2.に関しては具体的な解決法が思いつかないです・・・
290
+
291
+
292
+
293
+ なにか解決法はありますでしょうか??