teratail header banner
teratail header banner
質問するログイン新規登録

質問編集履歴

3

md

2019/11/08 12:56

投稿

makoto-n
makoto-n

スコア436

title CHANGED
File without changes
body CHANGED
@@ -156,7 +156,7 @@
156
156
  ```
157
157
 
158
158
  ---
159
- *解決後Activity.kt*
159
+ __解決後Activity.kt__
160
160
  ```
161
161
  // 登録
162
162
  class Registration__Activity : AppCompatActivity() {

2

追記

2019/11/08 12:56

投稿

makoto-n
makoto-n

スコア436

title CHANGED
File without changes
body CHANGED
@@ -153,4 +153,64 @@
153
153
  app:layout_constraintEnd_toEndOf="parent"
154
154
  app:layout_constraintStart_toStartOf="parent" />
155
155
  </androidx.constraintlayout.widget.ConstraintLayout>
156
+ ```
157
+
158
+ ---
159
+ *解決後Activity.kt*
160
+ ```
161
+ // 登録
162
+ class Registration__Activity : AppCompatActivity() {
163
+
164
+ override fun onCreate(savedInstanceState: Bundle?) {
165
+ super.onCreate(savedInstanceState)
166
+ setContentView(R.layout.activity_registration__)
167
+ val SQLite__Helper = SQLite__Helper(this)
168
+
169
+
170
+ fun Context.showToast(text: CharSequence) =
171
+ Toast.makeText(this, text, Toast.LENGTH_SHORT).show()
172
+ // Toastを簡素化
173
+
174
+
175
+ itmAddBtn.setOnClickListener {
176
+
177
+ var itmNM : String? = edtNM.text.toString()
178
+ var itmPrc: String? = edtPrc.text.toString()
179
+
180
+
181
+ var itmStr: String
182
+ if (itmNM?.isNotBlank()!!){ //空白も含まない
183
+ itmStr = itmNM
184
+ }else{
185
+ showToast("名前を入力してください")
186
+ return@setOnClickListener
187
+ } //空白か未入力であればメッセージを表示
188
+
189
+
190
+ var itmInt: Int
191
+ when(itmPrc?.toIntOrNull()){ //Int型にキャストするか、nullにする
192
+ is Int-> {
193
+ itmInt = itmPrc.toInt()
194
+ } //その結果の型をInt
195
+ else-> {
196
+ showToast("数値を入力してください")
197
+ edtPrc.setText("")
198
+ return@setOnClickListener
199
+ } //そうでないかnullであればメッセージを表示
200
+ }
201
+
202
+ var itmInstance = ItmMdl(
203
+ itmStr, itmInt
204
+ )
205
+
206
+ var result = SQLite__Helper.insertItm(itmInstance)
207
+
208
+ if (result) {
209
+ showToast("商品登録!")
210
+ edtNM.setText("")
211
+ edtPrc.setText("")
212
+ }
213
+ }
214
+ }
215
+ }
156
216
  ```

1

追記

2019/11/08 12:53

投稿

makoto-n
makoto-n

スコア436

title CHANGED
File without changes
body CHANGED
@@ -80,4 +80,77 @@
80
80
  }
81
81
  }
82
82
  }
83
+ ```
84
+ レイアウトXML
85
+ ```xml
86
+ <?xml version="1.0" encoding="utf-8"?>
87
+ <androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
88
+ xmlns:app="http://schemas.android.com/apk/res-auto"
89
+ xmlns:tools="http://schemas.android.com/tools"
90
+ android:layout_width="match_parent"
91
+ android:layout_height="match_parent"
92
+ tools:context=".Registration__Activity">
93
+
94
+ <TextView
95
+ android:id="@+id/textView"
96
+ android:layout_width="wrap_content"
97
+ android:layout_height="wrap_content"
98
+ android:layout_marginTop="24dp"
99
+ android:text="商品登録"
100
+ android:textSize="30sp"
101
+ app:layout_constraintEnd_toEndOf="parent"
102
+ app:layout_constraintStart_toStartOf="parent"
103
+ app:layout_constraintTop_toTopOf="parent" />
104
+
105
+ <EditText
106
+ android:id="@+id/edtNM"
107
+ android:layout_width="wrap_content"
108
+ android:layout_height="wrap_content"
109
+ android:layout_marginTop="100dp"
110
+ android:ems="10"
111
+ android:inputType="textPersonName"
112
+ app:layout_constraintEnd_toEndOf="parent"
113
+ app:layout_constraintStart_toStartOf="parent"
114
+ app:layout_constraintTop_toBottomOf="@+id/textView" />
115
+
116
+ <TextView
117
+ android:id="@+id/textView2"
118
+ android:layout_width="wrap_content"
119
+ android:layout_height="wrap_content"
120
+ android:layout_marginBottom="16dp"
121
+ android:text="商品名"
122
+ app:layout_constraintBottom_toTopOf="@+id/edtNM"
123
+ app:layout_constraintStart_toStartOf="@+id/edtNM" />
124
+
125
+ <EditText
126
+ android:id="@+id/edtPrc"
127
+ android:layout_width="wrap_content"
128
+ android:layout_height="wrap_content"
129
+ android:layout_marginTop="70dp"
130
+ android:ems="10"
131
+ android:inputType="numberDecimal"
132
+ app:layout_constraintEnd_toEndOf="parent"
133
+ app:layout_constraintStart_toStartOf="parent"
134
+ app:layout_constraintTop_toBottomOf="@+id/edtNM" />
135
+
136
+ <TextView
137
+ android:id="@+id/textView3"
138
+ android:layout_width="wrap_content"
139
+ android:layout_height="wrap_content"
140
+ android:layout_marginBottom="16dp"
141
+ android:text="値段"
142
+ app:layout_constraintBottom_toTopOf="@+id/edtPrc"
143
+ app:layout_constraintStart_toStartOf="@+id/edtPrc" />
144
+
145
+ <Button
146
+ android:id="@+id/itmAddBtn"
147
+ android:layout_width="wrap_content"
148
+ android:layout_height="wrap_content"
149
+ android:layout_marginBottom="50dp"
150
+ android:background="#FF9800"
151
+ android:text="商品登録"
152
+ app:layout_constraintBottom_toBottomOf="parent"
153
+ app:layout_constraintEnd_toEndOf="parent"
154
+ app:layout_constraintStart_toStartOf="parent" />
155
+ </androidx.constraintlayout.widget.ConstraintLayout>
83
156
  ```