質問編集履歴
1
コードの追加
test
CHANGED
File without changes
|
test
CHANGED
@@ -35,3 +35,367 @@
|
|
35
35
|
|
36
36
|
|
37
37
|
以前はH2databaseを使っていて、作成しているファイルに入れるインストールしたファイルがあったのですが、SQLiteもやることがあるのでしょうか?よろしくお願いします。
|
38
|
+
|
39
|
+
|
40
|
+
|
41
|
+
Activity_main.xml
|
42
|
+
|
43
|
+
```java
|
44
|
+
|
45
|
+
<?xml version="1.0" encoding="utf-8"?>
|
46
|
+
|
47
|
+
<RelativeLayout
|
48
|
+
|
49
|
+
xmlns:android="http://schemas.android.com/apk/res/android"
|
50
|
+
|
51
|
+
xmlns:tools="http://schemas.android.com/tools"
|
52
|
+
|
53
|
+
android:layout_width="match_parent"
|
54
|
+
|
55
|
+
android:layout_height="match_parent"
|
56
|
+
|
57
|
+
tools:context=".MainActivity">
|
58
|
+
|
59
|
+
|
60
|
+
|
61
|
+
|
62
|
+
|
63
|
+
|
64
|
+
|
65
|
+
<TextView
|
66
|
+
|
67
|
+
android:id="@+id/textView1"
|
68
|
+
|
69
|
+
android:layout_width="wrap_content"
|
70
|
+
|
71
|
+
android:layout_height="wrap_content"
|
72
|
+
|
73
|
+
android:layout_alignParentLeft="true"
|
74
|
+
|
75
|
+
android:layout_alignParentRight="true"
|
76
|
+
|
77
|
+
android:layout_alignParntTop="true"
|
78
|
+
|
79
|
+
android:text="名前と年齢を入力してください"
|
80
|
+
|
81
|
+
android:textAppearance="?android:attr/textAppearanceMedium"/>
|
82
|
+
|
83
|
+
|
84
|
+
|
85
|
+
|
86
|
+
|
87
|
+
<TextView
|
88
|
+
|
89
|
+
android:id="@+id/textView2"
|
90
|
+
|
91
|
+
android:layout_width="wrap_content"
|
92
|
+
|
93
|
+
android:layout_height="wrap_content"
|
94
|
+
|
95
|
+
android:layout_alignParentLeft="true"
|
96
|
+
|
97
|
+
android:layout_alignParentRight="true"
|
98
|
+
|
99
|
+
android:layout_below="@+id/textView1"
|
100
|
+
|
101
|
+
android:text="名前"/>
|
102
|
+
|
103
|
+
|
104
|
+
|
105
|
+
|
106
|
+
|
107
|
+
<EditText
|
108
|
+
|
109
|
+
android:id="@+id/editName"
|
110
|
+
|
111
|
+
android:layout_width="wrap_content"
|
112
|
+
|
113
|
+
android:layout_height="wrap_content"
|
114
|
+
|
115
|
+
android:layout_alignParentLeft="true"
|
116
|
+
|
117
|
+
android:layout_alignParentRight="true"
|
118
|
+
|
119
|
+
android:layout_below="@+id/textView2"
|
120
|
+
|
121
|
+
android:ems="10"
|
122
|
+
|
123
|
+
android:inputType="textPersonName">
|
124
|
+
|
125
|
+
|
126
|
+
|
127
|
+
|
128
|
+
|
129
|
+
<requestFocus/>
|
130
|
+
|
131
|
+
</EditText>
|
132
|
+
|
133
|
+
|
134
|
+
|
135
|
+
|
136
|
+
|
137
|
+
<TextView
|
138
|
+
|
139
|
+
android:id="@+id/textView3"
|
140
|
+
|
141
|
+
android:layout_width="wrap_content"
|
142
|
+
|
143
|
+
android:layout_height="wrap_content"
|
144
|
+
|
145
|
+
android:layout_alignParentLeft="true"
|
146
|
+
|
147
|
+
android:layout_alignParentRight="true"
|
148
|
+
|
149
|
+
android:layout_below="@+id/editName"
|
150
|
+
|
151
|
+
android:text="年齢"/>
|
152
|
+
|
153
|
+
|
154
|
+
|
155
|
+
|
156
|
+
|
157
|
+
|
158
|
+
|
159
|
+
<EditText
|
160
|
+
|
161
|
+
android:id="@+id/editAge"
|
162
|
+
|
163
|
+
android:layout_width="wrap_content"
|
164
|
+
|
165
|
+
android:layout_height="wrap_content"
|
166
|
+
|
167
|
+
android:layout_alignParentLeft="true"
|
168
|
+
|
169
|
+
android:layout_alignParentRight="true"
|
170
|
+
|
171
|
+
android:layout_below="@+id/textView3"
|
172
|
+
|
173
|
+
android:ems="10"
|
174
|
+
|
175
|
+
android:inputType="number" />
|
176
|
+
|
177
|
+
|
178
|
+
|
179
|
+
|
180
|
+
|
181
|
+
<Button
|
182
|
+
|
183
|
+
android:id="@+id/database"
|
184
|
+
|
185
|
+
android:layout_width="wrap_content"
|
186
|
+
|
187
|
+
android:layout_height="wrap_content"
|
188
|
+
|
189
|
+
android:layout_alignParentBottom="true"
|
190
|
+
|
191
|
+
android:layout_alignParentRight="true"
|
192
|
+
|
193
|
+
android:text="データベースの中身を閲覧"/>
|
194
|
+
|
195
|
+
|
196
|
+
|
197
|
+
|
198
|
+
|
199
|
+
</RelativeLayout>
|
200
|
+
|
201
|
+
|
202
|
+
|
203
|
+
```
|
204
|
+
|
205
|
+
|
206
|
+
|
207
|
+
MainActivity.java
|
208
|
+
|
209
|
+
```java
|
210
|
+
|
211
|
+
package com.example.kanehiro.database;
|
212
|
+
|
213
|
+
|
214
|
+
|
215
|
+
import android.app.Activity;
|
216
|
+
|
217
|
+
import android.content.ContentValues;
|
218
|
+
|
219
|
+
import android.content.Intent;
|
220
|
+
|
221
|
+
import android.database.sqlite.SQLiteDatabase;
|
222
|
+
|
223
|
+
import android.os.Bundle;
|
224
|
+
|
225
|
+
import android.view.View;
|
226
|
+
|
227
|
+
import android.view.View.OnClickListener;
|
228
|
+
|
229
|
+
import android.widget.Button;
|
230
|
+
|
231
|
+
import android.widget.EditText;
|
232
|
+
|
233
|
+
import android.widget.Toast;
|
234
|
+
|
235
|
+
|
236
|
+
|
237
|
+
public class MainActivity extends Activity {
|
238
|
+
|
239
|
+
@Override
|
240
|
+
|
241
|
+
protected void onCreate(Bundle savedInstanceState) {
|
242
|
+
|
243
|
+
super.onCreate(savedInstanceState);
|
244
|
+
|
245
|
+
setContentView(R.layout.activity_main);
|
246
|
+
|
247
|
+
|
248
|
+
|
249
|
+
MyOpenHelper helper = new MyOpenHelper(this);
|
250
|
+
|
251
|
+
final SQLiteDatabase db = helper.getWritableDatabase();
|
252
|
+
|
253
|
+
final EditText nameText = (EditText) findViewById(R.id.editName);
|
254
|
+
|
255
|
+
final EditText ageText = (EditText) findViewById(R.id.editAge);
|
256
|
+
|
257
|
+
|
258
|
+
|
259
|
+
Button entryButton = (Button) findViewById(R.id.insert);
|
260
|
+
|
261
|
+
entryButton.setOnClickListener(new OnClickListener() {
|
262
|
+
|
263
|
+
|
264
|
+
|
265
|
+
@Override
|
266
|
+
|
267
|
+
public void onClick(View v) {
|
268
|
+
|
269
|
+
String name = nameText.getText().toString();
|
270
|
+
|
271
|
+
String age = ageText.getText().toString();
|
272
|
+
|
273
|
+
|
274
|
+
|
275
|
+
ContentValues insertValues = new ContentValues();
|
276
|
+
|
277
|
+
insertValues.put("name", name);
|
278
|
+
|
279
|
+
insertValues.put("age", age);
|
280
|
+
|
281
|
+
long id = db.insert("person", name, insertValues);
|
282
|
+
|
283
|
+
}
|
284
|
+
|
285
|
+
|
286
|
+
|
287
|
+
});
|
288
|
+
|
289
|
+
Button updateButton = (Button) findViewById(R.id.update);
|
290
|
+
|
291
|
+
updateButton.setOnClickListener(new OnClickListener() {
|
292
|
+
|
293
|
+
|
294
|
+
|
295
|
+
@Override
|
296
|
+
|
297
|
+
public void onClick(View v) {
|
298
|
+
|
299
|
+
String name = nameText.getText().toString();
|
300
|
+
|
301
|
+
String age = ageText.getText().toString();
|
302
|
+
|
303
|
+
|
304
|
+
|
305
|
+
if (name.equals("")) {
|
306
|
+
|
307
|
+
Toast.makeText(MainActivity.this, "名前を入力してください。",
|
308
|
+
|
309
|
+
Toast.LENGTH_SHORT).show();
|
310
|
+
|
311
|
+
} else {
|
312
|
+
|
313
|
+
ContentValues updateValues = new ContentValues();
|
314
|
+
|
315
|
+
updateValues.put("age", age);
|
316
|
+
|
317
|
+
db.update("person", updateValues, "name=?", new String[] { name });
|
318
|
+
|
319
|
+
}
|
320
|
+
|
321
|
+
}
|
322
|
+
|
323
|
+
});
|
324
|
+
|
325
|
+
Button deleteButton = (Button) findViewById(R.id.delete);
|
326
|
+
|
327
|
+
deleteButton.setOnClickListener(new OnClickListener() {
|
328
|
+
|
329
|
+
|
330
|
+
|
331
|
+
@Override
|
332
|
+
|
333
|
+
public void onClick(View v) {
|
334
|
+
|
335
|
+
String name = nameText.getText().toString();
|
336
|
+
|
337
|
+
String age = ageText.getText().toString();
|
338
|
+
|
339
|
+
|
340
|
+
|
341
|
+
if (name.equals("")) {
|
342
|
+
|
343
|
+
Toast.makeText(MainActivity.this, "名前を入力してください。",
|
344
|
+
|
345
|
+
Toast.LENGTH_SHORT).show();
|
346
|
+
|
347
|
+
} else {
|
348
|
+
|
349
|
+
db.delete("person", "name=?", new String[]{name});
|
350
|
+
|
351
|
+
}
|
352
|
+
|
353
|
+
}
|
354
|
+
|
355
|
+
});
|
356
|
+
|
357
|
+
Button deleteAllButton = (Button) findViewById(R.id.deleteAll);
|
358
|
+
|
359
|
+
deleteAllButton.setOnClickListener(new OnClickListener() {
|
360
|
+
|
361
|
+
|
362
|
+
|
363
|
+
@Override
|
364
|
+
|
365
|
+
public void onClick(View v) {
|
366
|
+
|
367
|
+
String name = nameText.getText().toString();
|
368
|
+
|
369
|
+
String age = ageText.getText().toString();
|
370
|
+
|
371
|
+
db.delete("person", null, null);
|
372
|
+
|
373
|
+
}
|
374
|
+
|
375
|
+
});
|
376
|
+
|
377
|
+
Button detaBaseButton = (Button) findViewById(R.id.dataBase);
|
378
|
+
|
379
|
+
detaBaseButton.setOnClickListener(new OnClickListener() {
|
380
|
+
|
381
|
+
|
382
|
+
|
383
|
+
@Override
|
384
|
+
|
385
|
+
public void onClick(View v) {
|
386
|
+
|
387
|
+
Intent dbIntent = new Intent(MainActivity.this,
|
388
|
+
|
389
|
+
ShowDataBase.class);
|
390
|
+
|
391
|
+
startActivity(dbIntent);
|
392
|
+
|
393
|
+
}
|
394
|
+
|
395
|
+
});
|
396
|
+
|
397
|
+
}
|
398
|
+
|
399
|
+
}
|
400
|
+
|
401
|
+
```
|