質問編集履歴
1
コード、質問内容の追記
test
CHANGED
File without changes
|
test
CHANGED
@@ -2,16 +2,330 @@
|
|
2
2
|
|
3
3
|
日付ごとにメモを登録しようとしているのですが、
|
4
4
|
|
5
|
-
データの検索、更新
|
5
|
+
データの検索、更新がうまくいっておりません。
|
6
|
+
|
7
|
+
|
8
|
+
|
6
|
-
|
9
|
+
String型のdisplayをキーにして、
|
10
|
+
|
7
|
-
|
11
|
+
MemoActivityの生成と同時にデータ検索、表示、
|
12
|
+
|
8
|
-
|
13
|
+
onUpdateメソッドでデータの追加、更新をしようとしています。
|
14
|
+
|
15
|
+
|
16
|
+
|
17
|
+
データ更新時、onUpdateメソッドのtryまで、
|
18
|
+
|
19
|
+
変数が正しく入っており、アプリが落ちたりもしません。
|
20
|
+
|
21
|
+
Toastの表示も正常に動作しております。
|
22
|
+
|
23
|
+
|
24
|
+
|
25
|
+
しかし、再度アクティビティーを生成するとデータが登録されていないことを判明します。
|
26
|
+
|
9
|
-
|
27
|
+
データベースの登録、検索を正常にする方法を教えていただければ幸いです。
|
28
|
+
|
29
|
+
Android Studio:3.6.1
|
30
|
+
|
31
|
+
build:grandle:3.6.3
|
10
32
|
|
11
33
|
|
12
34
|
|
13
35
|
```java
|
14
36
|
|
37
|
+
|
38
|
+
|
39
|
+
import androidx.appcompat.app.AppCompatActivity;
|
40
|
+
|
41
|
+
|
42
|
+
|
43
|
+
import android.content.Context;
|
44
|
+
|
45
|
+
import android.content.Intent;
|
46
|
+
|
47
|
+
import android.os.Bundle;
|
48
|
+
|
49
|
+
import android.view.Gravity;
|
50
|
+
|
51
|
+
import android.view.LayoutInflater;
|
52
|
+
|
53
|
+
import android.view.View;
|
54
|
+
|
55
|
+
import android.view.ViewGroup;
|
56
|
+
|
57
|
+
import android.view.inputmethod.InputMethodManager;
|
58
|
+
|
59
|
+
import android.widget.Button;
|
60
|
+
|
61
|
+
import android.widget.EditText;
|
62
|
+
|
63
|
+
import android.widget.ImageView;
|
64
|
+
|
65
|
+
import android.widget.LinearLayout;
|
66
|
+
|
67
|
+
import android.widget.TextView;
|
68
|
+
|
69
|
+
import android.widget.Toast;
|
70
|
+
|
71
|
+
|
72
|
+
|
73
|
+
import java.util.Calendar;
|
74
|
+
|
75
|
+
|
76
|
+
|
77
|
+
import datebase.CustomOpenHelper;
|
78
|
+
|
79
|
+
|
80
|
+
|
81
|
+
public class MemoActivity extends AppCompatActivity implements View.OnClickListener{
|
82
|
+
|
83
|
+
//EditTextを生成
|
84
|
+
|
85
|
+
EditText editText;
|
86
|
+
|
87
|
+
//データベース
|
88
|
+
|
89
|
+
CustomOpenHelper customOpenHelper;
|
90
|
+
|
91
|
+
//表示する日付
|
92
|
+
|
93
|
+
String display;
|
94
|
+
|
95
|
+
//表示する年月日
|
96
|
+
|
97
|
+
int displayYear;
|
98
|
+
|
99
|
+
int displayMonth;
|
100
|
+
|
101
|
+
int displayDay;
|
102
|
+
|
103
|
+
|
104
|
+
|
105
|
+
TextView textView;
|
106
|
+
|
107
|
+
//Calendarクラス
|
108
|
+
|
109
|
+
Calendar calendar = null;
|
110
|
+
|
111
|
+
@Override
|
112
|
+
|
113
|
+
protected void onCreate(Bundle savedInstanceState) {
|
114
|
+
|
115
|
+
super.onCreate(savedInstanceState);
|
116
|
+
|
117
|
+
setContentView(R.layout.activity_memo);
|
118
|
+
|
119
|
+
|
120
|
+
|
121
|
+
//MainActivityからインテントを取得
|
122
|
+
|
123
|
+
Intent intent = this.getIntent();
|
124
|
+
|
125
|
+
//値を取得
|
126
|
+
|
127
|
+
String year = intent.getStringExtra("year"); //年
|
128
|
+
|
129
|
+
String month = intent.getStringExtra("month"); //月
|
130
|
+
|
131
|
+
String day = intent.getStringExtra("day");
|
132
|
+
|
133
|
+
//表示する文字列を作成
|
134
|
+
|
135
|
+
display = year+"年"+month+"月"+day+"日";
|
136
|
+
|
137
|
+
|
138
|
+
|
139
|
+
//文字列を登録
|
140
|
+
|
141
|
+
displayYear = Integer.parseInt(year);
|
142
|
+
|
143
|
+
displayMonth = Integer.parseInt(month);
|
144
|
+
|
145
|
+
displayDay = Integer.parseInt(day);
|
146
|
+
|
147
|
+
|
148
|
+
|
149
|
+
//CalendarMakerの生成
|
150
|
+
|
151
|
+
this.calendar = Calendar.getInstance();
|
152
|
+
|
153
|
+
calendar.set(displayYear,displayMonth,displayDay);
|
154
|
+
|
155
|
+
|
156
|
+
|
157
|
+
//画面に表示
|
158
|
+
|
159
|
+
textView = ((TextView)findViewById(R.id.year_month_day));
|
160
|
+
|
161
|
+
textView.setText(display);
|
162
|
+
|
163
|
+
|
164
|
+
|
165
|
+
//EditTextを生成
|
166
|
+
|
167
|
+
editText = findViewById(R.id.memo);
|
168
|
+
|
169
|
+
//Focusの設定
|
170
|
+
|
171
|
+
editText.setOnFocusChangeListener(new View.OnFocusChangeListener(){
|
172
|
+
|
173
|
+
@Override
|
174
|
+
|
175
|
+
public void onFocusChange(View v, boolean hasFocus) {
|
176
|
+
|
177
|
+
//EditTextのフォーカスが外れた場合
|
178
|
+
|
179
|
+
if(!hasFocus){
|
180
|
+
|
181
|
+
//ソフトキーボードを非表示にする
|
182
|
+
|
183
|
+
InputMethodManager imm = (InputMethodManager)
|
184
|
+
|
185
|
+
getSystemService(Context.INPUT_METHOD_SERVICE);
|
186
|
+
|
187
|
+
|
188
|
+
|
189
|
+
imm.hideSoftInputFromWindow(v.getWindowToken(),InputMethodManager.HIDE_NOT_ALWAYS);
|
190
|
+
|
191
|
+
}
|
192
|
+
|
193
|
+
}
|
194
|
+
|
195
|
+
});
|
196
|
+
|
197
|
+
LinearLayout linearLayout = ((LinearLayout)findViewById(R.id.layout));
|
198
|
+
|
199
|
+
linearLayout.setOnClickListener(this);
|
200
|
+
|
201
|
+
|
202
|
+
|
203
|
+
//データベースを生成する
|
204
|
+
|
205
|
+
this.customOpenHelper = new CustomOpenHelper(this);
|
206
|
+
|
207
|
+
|
208
|
+
|
209
|
+
//EditTextに文字列をセットする
|
210
|
+
|
211
|
+
String memo = customOpenHelper.getPlan(display);
|
212
|
+
|
213
|
+
editText.setText(memo);
|
214
|
+
|
215
|
+
}
|
216
|
+
|
217
|
+
//EditTextに入力された文字列を取得する
|
218
|
+
|
219
|
+
public void onUpdate(View v){
|
220
|
+
|
221
|
+
//EditTextのテキストを取得
|
222
|
+
|
223
|
+
String text = editText.getText().toString();
|
224
|
+
|
225
|
+
//メモに入力がなかった場合
|
226
|
+
|
227
|
+
if(text.equals("")){
|
228
|
+
|
229
|
+
ImageView imageView = new ImageView(this);
|
230
|
+
|
231
|
+
imageView.setImageResource(R.drawable.no);
|
232
|
+
|
233
|
+
Toast toast = new Toast(this);
|
234
|
+
|
235
|
+
toast.setDuration(Toast.LENGTH_SHORT);
|
236
|
+
|
237
|
+
toast.setView(imageView);
|
238
|
+
|
239
|
+
toast.setGravity(Gravity.CENTER,0,0);
|
240
|
+
|
241
|
+
toast.show();
|
242
|
+
|
243
|
+
}else{//メモに入力があった場合
|
244
|
+
|
245
|
+
//データベースに登録する
|
246
|
+
|
247
|
+
this.customOpenHelper.addMemo(display,text);
|
248
|
+
|
249
|
+
Toast toast = new Toast(this);
|
250
|
+
|
251
|
+
toast.makeText(this,"登録しました",Toast.LENGTH_SHORT).show();
|
252
|
+
|
253
|
+
}
|
254
|
+
|
255
|
+
}
|
256
|
+
|
257
|
+
//日付を前にずらす
|
258
|
+
|
259
|
+
public void onPrevious(View v){
|
260
|
+
|
261
|
+
this.calendar.add(Calendar.DAY_OF_MONTH,-1);
|
262
|
+
|
263
|
+
this.getDate();
|
264
|
+
|
265
|
+
String date = displayYear+"年"+displayMonth+"月"+displayDay+"日";
|
266
|
+
|
267
|
+
textView.setText(date);
|
268
|
+
|
269
|
+
}
|
270
|
+
|
271
|
+
//日付を後ろにずらす
|
272
|
+
|
273
|
+
public void onNext(View v){
|
274
|
+
|
275
|
+
this.calendar.add(Calendar.DAY_OF_MONTH,1);
|
276
|
+
|
277
|
+
this.getDate();
|
278
|
+
|
279
|
+
String date = displayYear+"年"+displayMonth+"月"+displayDay+"日";
|
280
|
+
|
281
|
+
textView.setText(date);
|
282
|
+
|
283
|
+
}
|
284
|
+
|
285
|
+
//戻るボタン""
|
286
|
+
|
287
|
+
public void onDelete(View v){
|
288
|
+
|
289
|
+
//アクティビティーの破棄
|
290
|
+
|
291
|
+
finish();
|
292
|
+
|
293
|
+
}
|
294
|
+
|
295
|
+
@Override
|
296
|
+
|
297
|
+
public void onClick(View v) {
|
298
|
+
|
299
|
+
//ボタンにフォーカスを移動させる
|
300
|
+
|
301
|
+
Button button = ((Button)findViewById(R.id.update));
|
302
|
+
|
303
|
+
button.setFocusable(true);
|
304
|
+
|
305
|
+
button.setFocusableInTouchMode(true);
|
306
|
+
|
307
|
+
button.requestFocus();
|
308
|
+
|
309
|
+
}
|
310
|
+
|
311
|
+
//年月日の取得
|
312
|
+
|
313
|
+
private void getDate(){
|
314
|
+
|
315
|
+
this.displayYear = calendar.get(Calendar.YEAR);
|
316
|
+
|
317
|
+
this.displayMonth = calendar.get(Calendar.MONTH);
|
318
|
+
|
319
|
+
this.displayDay = calendar.get(Calendar.DATE);
|
320
|
+
|
321
|
+
}
|
322
|
+
|
323
|
+
}
|
324
|
+
|
325
|
+
```
|
326
|
+
|
327
|
+
```java
|
328
|
+
|
15
329
|
import android.content.ContentValues;
|
16
330
|
|
17
331
|
import android.content.Context;
|