Blinking My Melody

로그인 화면 만들기 (database 입력)

2019. 4. 15. 22:06

- database를 이용해서 단순히 입력하고 출력하는 것이 아닌 입력한 값들을 database로 저장하는 역할

 

 

 

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
55
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">
 
    <LinearLayout
        android:id="@+id/layoutForm"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent">
 
        <EditText
            android:id="@+id/editTextUserName"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:ems="10"
            android:inputType="textPersonName"
            android:text="Name" />
 
        <Button
            android:id="@+id/buttonLogin"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="LOGIN"
            android:onClick="onLogin" />
    </LinearLayout>
    <LinearLayout
        android:id="@+id/layoutHome"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        android:visibility="gone"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent">
 
        <TextView
            android:id="@+id/textViewUserName"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="TextView" />
 
        <Button
            android:id="@+id/buttonLogout"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="LOGOUT"
            android:onClick="onLogout" />
    </LinearLayout>
 
</android.support.constraint.ConstraintLayout>
http://colorscripter.com/info#e" target="_blank" style="color:#e5e5e5; text-decoration:none">Colored by Color Scripter

 

 

 

 

 

 

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
package com.example.soyoung.example;
 
import android.content.SharedPreferences;
 
public class MainActivity extends AppCompatActivity {
    private int[] layoutIds = {R.id.layoutForm, R.id.layoutHome};
    private LinearLayout[] layouts;
    private EditText editTextUserName;
    private TextView textViewUserName;
    private SharedPreferences preferences;
 
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
 
        layouts = new LinearLayout[layoutIds.length];
        for(int i = 0; i < layouts.length; i++) {
            layouts[i] = findViewById(layoutIds[i]);
        }
        editTextUserName = findViewById(R.id.editTextUserName);
        textViewUserName = findViewById(R.id.textViewUserName);
        preferences = getSharedPreferences("user", MODE_PRIVATE);
        String userName = preferences.getString("userName"null);
        if(userName == null)
            showLayout(R.id.layoutForm);
        else {
            textViewUserName.setText(userName);
            showLayout(R.id.layoutHome);
        }
    }
 
    private void showLayout(int id) {
        for(LinearLayout layout: layouts) {
            if(layout.getId() == id)
                layout.setVisibility(View.VISIBLE);
            else
                layout.setVisibility(View.GONE);
        }
    }
 
    public void onLogin(View v) {
        if(preferences == nullreturn;
        String userName = editTextUserName.getText().toString().trim();
        if(userName.length() == 0return;
 
        editor.putString("userName", userName); //문자열 값을
        editor.apply(); //한 번 호출
 
        textViewUserName.setText(userName);
        showLayout(R.id.layoutHome);
    }
 
    public void onLogout(View v) {
        if (preferences == nullreturn;
        editor.remove("userName"); //문자열 값을 삭제 후
        editor.apply(); //한 번 호출 ???
        showLayout(R.id.layoutForm);
    }
}
 
http://colorscripter.com/info#e" target="_blank" style="color:#e5e5e5; text-decoration:none">Colored by Color Scripter

 

 

 

 

 

UserBean.java

- 데이터를 저장하고 DB와 Activity 사이에 데이터를 전달하는 역할

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
package com.example.soyoung.example;
 
public class UserBean {
    private int sequenceNumber;
    private String name;
 
    public int getSequenceNumber() {
        return sequenceNumber;
    }
 
    public void setSequenceNumber(int sequenceNumber) {
        this.sequenceNumber = sequenceNumber;
    }
 
    public String getName() {
        return name;
    }
 
    public void setName(String name) {
        this.name = name;
    }
}
 
http://colorscripter.com/info#e" target="_blank" style="color:#e5e5e5; text-decoration:none">Colored by Color Scripter

 

 

 

 

 

 

HistoryDBHelper.java

- OpenHelper를 상속받는 HistoryDBHelper 클래스 생성

 

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
package com.example.soyoung.example;
 
import android.content.ContentValues;
import android.content.Context;
import android.support.annotation.Nullable;
 
 
public class HistoryDBHelper extends SQLiteOpenHelper {
    public HistoryDBHelper (@Nullable Context context, @Nullable String name, @Nullable SQLiteDatabase.CursorFactory factory, int version) {
        super(context, name, factory, version);
    }
    @Override
    public void onCreate(SQLiteDatabase db) {
        // 사용자가 지정한 이름의 DB가 존재하지 않을 때 호출
        // create table 동작을 수행하면 됨
        String sql = "create table history(sequenceNumber integer primary key autoincrement, name text)";
        db.execSQL(sql);
    }
 
    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        // 사용자가 지정한 이름의 DB는 존재하지만 사용자가 전달한 버전이 현재 버전보다 높을 때 호출
        // 테이블의 구조를 수정하거나 새로 만드는 동작 등을 수행 가능
        String sql = "drop table history";
        db.execSQL(sql);
        onCreate(db);
    }
 
    public long insert(UserBean user) {
        SQLiteDatabase db = getWritableDatabase();
        ContentValues value = new ContentValues();
        value.put("name", user.getName());
        return db.insert("history"null, value);
    }
 
    public ArrayList<UserBean> getAll() {
        SQLiteDatabase db = getReadableDatabase();
        Cursor cursor = db.query("history"nullnullnullnullnullnull);
        ArrayList<UserBean> result = new ArrayList<>();
        while(cursor.moveToNext()) {
            UserBean user = new UserBean();
            user.setSequenceNumber(cursor.getInt(cursor.getColumnIndex("sequenceNumber")));
            user.setName(cursor.getString(cursor.getColumnIndex("name")));
            result.add(user);
        }
        return result;
    }
}
 
http://colorscripter.com/info#e" target="_blank" style="color:#e5e5e5; text-decoration:none">Colored by Color Scripter