반응형
MainActivity.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88 |
package com.ktds.myaddressbook;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
public class MainActivity extends ActionBarActivity {
private EditText etName, etAge, etPhone, etJob;
private Button btnInsertDate, btnShowAllData;
private DatabaseHelper databaseHelper = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
databaseHelper = new DatabaseHelper(this);
etName = (EditText) findViewById(R.id.etName);
etAge = (EditText) findViewById(R.id.etAge);
etPhone = (EditText) findViewById(R.id.etPhone);
etJob = (EditText) findViewById(R.id.etJob);
btnInsertDate = (Button) findViewById(R.id.btnInsertDate);
btnShowAllData = (Button) findViewById(R.id.btnShowAllData);
// btnInsertDate을 클릭했을 때
btnInsertDate.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String name = etName.getText().toString();
String age = etAge.getText().toString();
String phone = etPhone.getText().toString();
String job = etJob.getText().toString();
if(name.length() == 0){
showDialog("이름을 입력하세요");
}
else if(age.length() == 0){
showDialog("나이를 입력하세요");
}
else if(phone.length() == 0){
showDialog("전화번호를 입력하세요");
}
else if(job.length() == 0){
showDialog("직업을 입력하세요");
}
else{
// 모두 다 입력했을 경우
AddressInfo addressInfo = new AddressInfo();
addressInfo.setName(name);
addressInfo.setAge(Integer.parseInt(age));
addressInfo.setPhone(phone);
addressInfo.setJob(job);
databaseHelper.insertAddressData(addressInfo);
}
}
});
}
private void showDialog(String message){
AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
builder.setTitle("알림!");
builder.setMessage(message);
// 버틀 클릭했을 때 실행되는 method
builder.setPositiveButton("확인", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
});
builder.show();
}
}
|
cs |
AddressInfo.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45 |
package com.ktds.myaddressbook;
/**
* Created by 206-001 on 2015-06-23.
*/
public class AddressInfo {
private String name;
private int age;
private String phone;
private String job;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
public String getJob() {
return job;
}
public void setJob(String job) {
this.job = job;
}
}
|
cs |
DatabaseHelper.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68 |
package com.ktds.myaddressbook;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;
/**
* Created by 206-001 on 2015-06-23.
*/
public class DatabaseHelper {
private SQLiteDatabase db;
private Context context;
// 생성자
public DatabaseHelper(Context context) {
this.context = context;
// app이 초기에 실행 될때 db 생성
this.db = context.openOrCreateDatabase("address", context.MODE_PRIVATE, null);
// app이 초기에 실행 될때 table 생성
this.createTable();
}
private void createTable(){
// table이 존재하지 않으면 만들어라.
if( !isExistsTable()){
String query = " CREATE TABLE ADDRESS( ";
query += " _ID INTEGER PRIMARY KEY AUTOINCREMENT, ";
query += " NAME VARCHAR, ";
query += " AGE INTEGER, ";
query += " PHONE VARCHAR, ";
query += " JOB VARCHAR); ";
db.execSQL(query);
}
}
// table이 존재하는지 확인하는 method
private boolean isExistsTable(){
String query= " SELECT COUNT(NAME) ";
query += " FROM sqlite_master ";
query += " WHERE type='table' ";
query += " AND name='ADDRESS'; ";
Cursor cursor = db.rawQuery(query,null);
if(cursor.moveToNext()){
return cursor.getInt(0) > 0;
}
return false;
}
public void insertAddressData(AddressInfo addressInfo){
// context를 이용해 INSERT_QUERY 가져오기
String query = context.getString(R.string.INSERT_QUERY);
query = String.format(query, "'" + addressInfo.getName() + "'",
addressInfo.getAge(),
"'" + addressInfo.getPhone()+ "'",
"'" + addressInfo.getJob()+ "'");
Log.d("QUERY", query);
db.execSQL(query);
}
}
|
cs |
activity_main.xml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 |
<resources>
<string name="app_name">MyAddressBook</string>
<string name="hello_world">Hello world!</string>
<string name="action_settings">Settings</string>
<string name="INSERT_QUERY">
INSERT INTO ADDRESS(
NAME, AGE, PHONE, JOB
)
VALUES(
%1$s, %2$d, %3$s, %4$s
)
</string>
</resources>
|
cs |
반응형
'IT > Android' 카테고리의 다른 글
Networking - 웹으로 요청하기 (0) | 2015.06.24 |
---|---|
Thead & Runnable (0) | 2015.06.24 |
Database (0) | 2015.06.23 |
Receiver (0) | 2015.06.23 |
2. Activity를 이용한 계산기 (0) | 2015.06.22 |