質問編集履歴

3

md

2019/11/08 12:56

投稿

makoto-n
makoto-n

スコア436

test CHANGED
File without changes
test CHANGED
@@ -314,7 +314,7 @@
314
314
 
315
315
  ---
316
316
 
317
- *解決後Activity.kt*
317
+ __解決後Activity.kt__
318
318
 
319
319
  ```
320
320
 

2

追記

2019/11/08 12:56

投稿

makoto-n
makoto-n

スコア436

test CHANGED
File without changes
test CHANGED
@@ -309,3 +309,123 @@
309
309
  </androidx.constraintlayout.widget.ConstraintLayout>
310
310
 
311
311
  ```
312
+
313
+
314
+
315
+ ---
316
+
317
+ *解決後Activity.kt*
318
+
319
+ ```
320
+
321
+ // 登録
322
+
323
+ class Registration__Activity : AppCompatActivity() {
324
+
325
+
326
+
327
+ override fun onCreate(savedInstanceState: Bundle?) {
328
+
329
+ super.onCreate(savedInstanceState)
330
+
331
+ setContentView(R.layout.activity_registration__)
332
+
333
+ val SQLite__Helper = SQLite__Helper(this)
334
+
335
+
336
+
337
+
338
+
339
+ fun Context.showToast(text: CharSequence) =
340
+
341
+ Toast.makeText(this, text, Toast.LENGTH_SHORT).show()
342
+
343
+ // Toastを簡素化
344
+
345
+
346
+
347
+
348
+
349
+ itmAddBtn.setOnClickListener {
350
+
351
+
352
+
353
+ var itmNM : String? = edtNM.text.toString()
354
+
355
+ var itmPrc: String? = edtPrc.text.toString()
356
+
357
+
358
+
359
+
360
+
361
+ var itmStr: String
362
+
363
+ if (itmNM?.isNotBlank()!!){ //空白も含まない
364
+
365
+ itmStr = itmNM
366
+
367
+ }else{
368
+
369
+ showToast("名前を入力してください")
370
+
371
+ return@setOnClickListener
372
+
373
+ } //空白か未入力であればメッセージを表示
374
+
375
+
376
+
377
+
378
+
379
+ var itmInt: Int
380
+
381
+ when(itmPrc?.toIntOrNull()){ //Int型にキャストするか、nullにする
382
+
383
+ is Int-> {
384
+
385
+ itmInt = itmPrc.toInt()
386
+
387
+ } //その結果の型をInt
388
+
389
+ else-> {
390
+
391
+ showToast("数値を入力してください")
392
+
393
+ edtPrc.setText("")
394
+
395
+ return@setOnClickListener
396
+
397
+ } //そうでないかnullであればメッセージを表示
398
+
399
+ }
400
+
401
+
402
+
403
+ var itmInstance = ItmMdl(
404
+
405
+ itmStr, itmInt
406
+
407
+ )
408
+
409
+
410
+
411
+ var result = SQLite__Helper.insertItm(itmInstance)
412
+
413
+
414
+
415
+ if (result) {
416
+
417
+ showToast("商品登録!")
418
+
419
+ edtNM.setText("")
420
+
421
+ edtPrc.setText("")
422
+
423
+ }
424
+
425
+ }
426
+
427
+ }
428
+
429
+ }
430
+
431
+ ```

1

追記

2019/11/08 12:53

投稿

makoto-n
makoto-n

スコア436

test CHANGED
File without changes
test CHANGED
@@ -163,3 +163,149 @@
163
163
  }
164
164
 
165
165
  ```
166
+
167
+ レイアウトXML
168
+
169
+ ```xml
170
+
171
+ <?xml version="1.0" encoding="utf-8"?>
172
+
173
+ <androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
174
+
175
+ xmlns:app="http://schemas.android.com/apk/res-auto"
176
+
177
+ xmlns:tools="http://schemas.android.com/tools"
178
+
179
+ android:layout_width="match_parent"
180
+
181
+ android:layout_height="match_parent"
182
+
183
+ tools:context=".Registration__Activity">
184
+
185
+
186
+
187
+ <TextView
188
+
189
+ android:id="@+id/textView"
190
+
191
+ android:layout_width="wrap_content"
192
+
193
+ android:layout_height="wrap_content"
194
+
195
+ android:layout_marginTop="24dp"
196
+
197
+ android:text="商品登録"
198
+
199
+ android:textSize="30sp"
200
+
201
+ app:layout_constraintEnd_toEndOf="parent"
202
+
203
+ app:layout_constraintStart_toStartOf="parent"
204
+
205
+ app:layout_constraintTop_toTopOf="parent" />
206
+
207
+
208
+
209
+ <EditText
210
+
211
+ android:id="@+id/edtNM"
212
+
213
+ android:layout_width="wrap_content"
214
+
215
+ android:layout_height="wrap_content"
216
+
217
+ android:layout_marginTop="100dp"
218
+
219
+ android:ems="10"
220
+
221
+ android:inputType="textPersonName"
222
+
223
+ app:layout_constraintEnd_toEndOf="parent"
224
+
225
+ app:layout_constraintStart_toStartOf="parent"
226
+
227
+ app:layout_constraintTop_toBottomOf="@+id/textView" />
228
+
229
+
230
+
231
+ <TextView
232
+
233
+ android:id="@+id/textView2"
234
+
235
+ android:layout_width="wrap_content"
236
+
237
+ android:layout_height="wrap_content"
238
+
239
+ android:layout_marginBottom="16dp"
240
+
241
+ android:text="商品名"
242
+
243
+ app:layout_constraintBottom_toTopOf="@+id/edtNM"
244
+
245
+ app:layout_constraintStart_toStartOf="@+id/edtNM" />
246
+
247
+
248
+
249
+ <EditText
250
+
251
+ android:id="@+id/edtPrc"
252
+
253
+ android:layout_width="wrap_content"
254
+
255
+ android:layout_height="wrap_content"
256
+
257
+ android:layout_marginTop="70dp"
258
+
259
+ android:ems="10"
260
+
261
+ android:inputType="numberDecimal"
262
+
263
+ app:layout_constraintEnd_toEndOf="parent"
264
+
265
+ app:layout_constraintStart_toStartOf="parent"
266
+
267
+ app:layout_constraintTop_toBottomOf="@+id/edtNM" />
268
+
269
+
270
+
271
+ <TextView
272
+
273
+ android:id="@+id/textView3"
274
+
275
+ android:layout_width="wrap_content"
276
+
277
+ android:layout_height="wrap_content"
278
+
279
+ android:layout_marginBottom="16dp"
280
+
281
+ android:text="値段"
282
+
283
+ app:layout_constraintBottom_toTopOf="@+id/edtPrc"
284
+
285
+ app:layout_constraintStart_toStartOf="@+id/edtPrc" />
286
+
287
+
288
+
289
+ <Button
290
+
291
+ android:id="@+id/itmAddBtn"
292
+
293
+ android:layout_width="wrap_content"
294
+
295
+ android:layout_height="wrap_content"
296
+
297
+ android:layout_marginBottom="50dp"
298
+
299
+ android:background="#FF9800"
300
+
301
+ android:text="商品登録"
302
+
303
+ app:layout_constraintBottom_toBottomOf="parent"
304
+
305
+ app:layout_constraintEnd_toEndOf="parent"
306
+
307
+ app:layout_constraintStart_toStartOf="parent" />
308
+
309
+ </androidx.constraintlayout.widget.ConstraintLayout>
310
+
311
+ ```