質問編集履歴
1
コードの追加
title
CHANGED
File without changes
|
body
CHANGED
@@ -16,4 +16,186 @@
|
|
16
16
|
|
17
17
|
や、'com.example.kanehiro.database.MyOpenHelper' is not assignable to 'android.app.Activity' more などがでいる状態です。
|
18
18
|
|
19
|
-
以前はH2databaseを使っていて、作成しているファイルに入れるインストールしたファイルがあったのですが、SQLiteもやることがあるのでしょうか?よろしくお願いします。
|
19
|
+
以前はH2databaseを使っていて、作成しているファイルに入れるインストールしたファイルがあったのですが、SQLiteもやることがあるのでしょうか?よろしくお願いします。
|
20
|
+
|
21
|
+
Activity_main.xml
|
22
|
+
```java
|
23
|
+
<?xml version="1.0" encoding="utf-8"?>
|
24
|
+
<RelativeLayout
|
25
|
+
xmlns:android="http://schemas.android.com/apk/res/android"
|
26
|
+
xmlns:tools="http://schemas.android.com/tools"
|
27
|
+
android:layout_width="match_parent"
|
28
|
+
android:layout_height="match_parent"
|
29
|
+
tools:context=".MainActivity">
|
30
|
+
|
31
|
+
|
32
|
+
|
33
|
+
<TextView
|
34
|
+
android:id="@+id/textView1"
|
35
|
+
android:layout_width="wrap_content"
|
36
|
+
android:layout_height="wrap_content"
|
37
|
+
android:layout_alignParentLeft="true"
|
38
|
+
android:layout_alignParentRight="true"
|
39
|
+
android:layout_alignParntTop="true"
|
40
|
+
android:text="名前と年齢を入力してください"
|
41
|
+
android:textAppearance="?android:attr/textAppearanceMedium"/>
|
42
|
+
|
43
|
+
|
44
|
+
<TextView
|
45
|
+
android:id="@+id/textView2"
|
46
|
+
android:layout_width="wrap_content"
|
47
|
+
android:layout_height="wrap_content"
|
48
|
+
android:layout_alignParentLeft="true"
|
49
|
+
android:layout_alignParentRight="true"
|
50
|
+
android:layout_below="@+id/textView1"
|
51
|
+
android:text="名前"/>
|
52
|
+
|
53
|
+
|
54
|
+
<EditText
|
55
|
+
android:id="@+id/editName"
|
56
|
+
android:layout_width="wrap_content"
|
57
|
+
android:layout_height="wrap_content"
|
58
|
+
android:layout_alignParentLeft="true"
|
59
|
+
android:layout_alignParentRight="true"
|
60
|
+
android:layout_below="@+id/textView2"
|
61
|
+
android:ems="10"
|
62
|
+
android:inputType="textPersonName">
|
63
|
+
|
64
|
+
|
65
|
+
<requestFocus/>
|
66
|
+
</EditText>
|
67
|
+
|
68
|
+
|
69
|
+
<TextView
|
70
|
+
android:id="@+id/textView3"
|
71
|
+
android:layout_width="wrap_content"
|
72
|
+
android:layout_height="wrap_content"
|
73
|
+
android:layout_alignParentLeft="true"
|
74
|
+
android:layout_alignParentRight="true"
|
75
|
+
android:layout_below="@+id/editName"
|
76
|
+
android:text="年齢"/>
|
77
|
+
|
78
|
+
|
79
|
+
|
80
|
+
<EditText
|
81
|
+
android:id="@+id/editAge"
|
82
|
+
android:layout_width="wrap_content"
|
83
|
+
android:layout_height="wrap_content"
|
84
|
+
android:layout_alignParentLeft="true"
|
85
|
+
android:layout_alignParentRight="true"
|
86
|
+
android:layout_below="@+id/textView3"
|
87
|
+
android:ems="10"
|
88
|
+
android:inputType="number" />
|
89
|
+
|
90
|
+
|
91
|
+
<Button
|
92
|
+
android:id="@+id/database"
|
93
|
+
android:layout_width="wrap_content"
|
94
|
+
android:layout_height="wrap_content"
|
95
|
+
android:layout_alignParentBottom="true"
|
96
|
+
android:layout_alignParentRight="true"
|
97
|
+
android:text="データベースの中身を閲覧"/>
|
98
|
+
|
99
|
+
|
100
|
+
</RelativeLayout>
|
101
|
+
|
102
|
+
```
|
103
|
+
|
104
|
+
MainActivity.java
|
105
|
+
```java
|
106
|
+
package com.example.kanehiro.database;
|
107
|
+
|
108
|
+
import android.app.Activity;
|
109
|
+
import android.content.ContentValues;
|
110
|
+
import android.content.Intent;
|
111
|
+
import android.database.sqlite.SQLiteDatabase;
|
112
|
+
import android.os.Bundle;
|
113
|
+
import android.view.View;
|
114
|
+
import android.view.View.OnClickListener;
|
115
|
+
import android.widget.Button;
|
116
|
+
import android.widget.EditText;
|
117
|
+
import android.widget.Toast;
|
118
|
+
|
119
|
+
public class MainActivity extends Activity {
|
120
|
+
@Override
|
121
|
+
protected void onCreate(Bundle savedInstanceState) {
|
122
|
+
super.onCreate(savedInstanceState);
|
123
|
+
setContentView(R.layout.activity_main);
|
124
|
+
|
125
|
+
MyOpenHelper helper = new MyOpenHelper(this);
|
126
|
+
final SQLiteDatabase db = helper.getWritableDatabase();
|
127
|
+
final EditText nameText = (EditText) findViewById(R.id.editName);
|
128
|
+
final EditText ageText = (EditText) findViewById(R.id.editAge);
|
129
|
+
|
130
|
+
Button entryButton = (Button) findViewById(R.id.insert);
|
131
|
+
entryButton.setOnClickListener(new OnClickListener() {
|
132
|
+
|
133
|
+
@Override
|
134
|
+
public void onClick(View v) {
|
135
|
+
String name = nameText.getText().toString();
|
136
|
+
String age = ageText.getText().toString();
|
137
|
+
|
138
|
+
ContentValues insertValues = new ContentValues();
|
139
|
+
insertValues.put("name", name);
|
140
|
+
insertValues.put("age", age);
|
141
|
+
long id = db.insert("person", name, insertValues);
|
142
|
+
}
|
143
|
+
|
144
|
+
});
|
145
|
+
Button updateButton = (Button) findViewById(R.id.update);
|
146
|
+
updateButton.setOnClickListener(new OnClickListener() {
|
147
|
+
|
148
|
+
@Override
|
149
|
+
public void onClick(View v) {
|
150
|
+
String name = nameText.getText().toString();
|
151
|
+
String age = ageText.getText().toString();
|
152
|
+
|
153
|
+
if (name.equals("")) {
|
154
|
+
Toast.makeText(MainActivity.this, "名前を入力してください。",
|
155
|
+
Toast.LENGTH_SHORT).show();
|
156
|
+
} else {
|
157
|
+
ContentValues updateValues = new ContentValues();
|
158
|
+
updateValues.put("age", age);
|
159
|
+
db.update("person", updateValues, "name=?", new String[] { name });
|
160
|
+
}
|
161
|
+
}
|
162
|
+
});
|
163
|
+
Button deleteButton = (Button) findViewById(R.id.delete);
|
164
|
+
deleteButton.setOnClickListener(new OnClickListener() {
|
165
|
+
|
166
|
+
@Override
|
167
|
+
public void onClick(View v) {
|
168
|
+
String name = nameText.getText().toString();
|
169
|
+
String age = ageText.getText().toString();
|
170
|
+
|
171
|
+
if (name.equals("")) {
|
172
|
+
Toast.makeText(MainActivity.this, "名前を入力してください。",
|
173
|
+
Toast.LENGTH_SHORT).show();
|
174
|
+
} else {
|
175
|
+
db.delete("person", "name=?", new String[]{name});
|
176
|
+
}
|
177
|
+
}
|
178
|
+
});
|
179
|
+
Button deleteAllButton = (Button) findViewById(R.id.deleteAll);
|
180
|
+
deleteAllButton.setOnClickListener(new OnClickListener() {
|
181
|
+
|
182
|
+
@Override
|
183
|
+
public void onClick(View v) {
|
184
|
+
String name = nameText.getText().toString();
|
185
|
+
String age = ageText.getText().toString();
|
186
|
+
db.delete("person", null, null);
|
187
|
+
}
|
188
|
+
});
|
189
|
+
Button detaBaseButton = (Button) findViewById(R.id.dataBase);
|
190
|
+
detaBaseButton.setOnClickListener(new OnClickListener() {
|
191
|
+
|
192
|
+
@Override
|
193
|
+
public void onClick(View v) {
|
194
|
+
Intent dbIntent = new Intent(MainActivity.this,
|
195
|
+
ShowDataBase.class);
|
196
|
+
startActivity(dbIntent);
|
197
|
+
}
|
198
|
+
});
|
199
|
+
}
|
200
|
+
}
|
201
|
+
```
|