Notice
Recent Posts
Recent Comments
Link
«   2024/11   »
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
Tags
more
Archives
Today
Total
관리 메뉴

재훈재훈

책 정보 입력기능 앱 만들기 본문

Computer Engineering/Android

책 정보 입력기능 앱 만들기

jaehoonx2 2018. 5. 6. 15:40

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
89
90
91
92
93
package com.tistory.jaehoonx2.doitmission_17;
 
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
 
 
/*
* App : 책 정보 입력 기능 어플리케이션
* 사용자가 책 정보를 입력하면 데이터베이스에 저장하는 앱
* 내용을 입력하고 저장 버튼을 누르면 토스트로 작성 내용을 띄워줌
*/
public class MainActivity extends AppCompatActivity {
 
    EditText title;
    EditText writer;
    EditText story;
    SQLiteDatabase db;
 
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
 
        title = (EditText) findViewById(R.id.title);
        writer = (EditText) findViewById(R.id.writer);
        story = (EditText) findViewById(R.id.story);
 
        openDatabase("BookList");
 
        Button button = (Button) findViewById(R.id.button);             // 저장 버튼
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String titletext = title.getText().toString();
                String writertext = writer.getText().toString();
                String storytext = story.getText().toString();
 
                insertData(titletext, writertext, storytext);
            }
        });
 
    }
 
    public void openDatabase(String databaseName) {                     // DB 오픈 함수 - Helper 사용
        DatabaseHelper dbHelper
                = new DatabaseHelper(this, databaseName, null1);
        db = dbHelper.getWritableDatabase();
    }
 
 
    public void insertData(String name, String writer, String story){   // DB에 데이터 삽입하는 함수
 
        if(db != null){
            String sql =
                    "insert into list(name, writer, story) values(?, ?, ?)";
            Object[] params = {name, writer, story};
 
            db.execSQL(sql, params);
 
            Toast.makeText(this,"제목 : " + name + " 저자 : " + writer + " 내용 : " + story,
                    Toast.LENGTH_LONG).show();
        } else {
            Toast.makeText(this,"ERROR!",Toast.LENGTH_LONG).show();
        }
    }
 
 
    class DatabaseHelper extends SQLiteOpenHelper {                     // Helper 클래스
 
        public DatabaseHelper(Context context, String name,
                              SQLiteDatabase.CursorFactory factory, int version) {
            super(context, name, factory, version);
        }
 
        @Override
        public void onCreate(SQLiteDatabase db) {                      // DB가 처음 생성될 때 호출되는 초기화 함수
            String tableName = "list";
            String sql = "create table if not exists " + tableName +
                    "(_id integer PRIMARY KEY autoincrement, name text, writer text, story text)";
            db.execSQL(sql);
        }
 
        @Override
        public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { }
    }
}
cs



activity_main.xml

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
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">
 
    <EditText
        android:id="@+id/title"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp"
        android:layout_marginLeft="5dp"
        android:layout_marginRight="5dp"
        android:paddingRight="5dp"
        android:paddingLeft="5dp"
        android:background="@drawable/back"
        android:hint="제목"
        android:textSize="40dp" />
 
    <EditText
        android:id="@+id/writer"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp"
        android:layout_marginLeft="5dp"
        android:layout_marginRight="5dp"
        android:paddingRight="5dp"
        android:paddingLeft="5dp"
        android:background="@drawable/back"
        android:hint="저자"
        android:textSize="40dp" />
 
    <EditText
        android:id="@+id/story"
        android:layout_width="match_parent"
        android:layout_marginTop="10dp"
        android:layout_marginLeft="5dp"
        android:layout_marginRight="5dp"
        android:layout_height="320dp"
        android:background="@drawable/back"
        android:paddingRight="5dp"
        android:paddingLeft="5dp"
        android:gravity="top"
        android:hint="내용"
        android:inputType="textMultiLine"
        android:textSize="40dp" />
 
    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:text="저장" />
</LinearLayout>
cs


back.xml (EditText 서식)

1
2
3
4
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle" >
    <solid android:color="#ffffff" />
    <stroke android:width="1dip" android:color="#4fa5d5"/>
</shape>
cs