본문 바로가기
안드로이드 웹앱 콘테츠 개발자 양성(국비지원)/Data 저장

Android Studio Data 저장 3 (File, Shared Preference, Data base, web서버)

by 차누감 2019. 9. 24.

1) File - 두 가지로 분류 (Internal Storage, External Storage)

2) Shared Preference

3) Data base

4) Web서버

 

 

<실행 화면>

2) Shared Preference  [Shared_prefs에 저장되고,설정값은 int,String등 여러가지... 이 방법은 확장자를 내 마음대로 못한다. .xml로 저장한다.)

 

Shared Preference

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
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
    android:orientation="vertical"
    android:padding="16dp"
    tools:context=".MainActivity">
 
 
    <EditText
        android:id="@+id/et_name"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="input name"
        android:inputType="text"/>
    <EditText
        android:id="@+id/et_age"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="input name"
        android:inputType="number"/>
 
    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="save"
        android:onClick="clickSave"/>
 
    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="save"
        android:onClick="clickLoad"/>
    <TextView
        android:id="@+id/tv"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:padding="8dp"/>
 
</LinearLayout>
 
 

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
 
 
import android.content.SharedPreferences;
 
public class MainActivity extends AppCompatActivity {
 
    EditText etName, etAge;
    TextView tv;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        etName=findViewById(R.id.et_name);
        etAge=findViewById(R.id.et_age);
        tv=findViewById(R.id.tv);
    }
 
    public void clickSave(View view) {
        String name= etName.getText().toString();
        int age;
        age=Integer.parseInt(etAge.getText().toString());
 
        //SharedPreferences 객체 소환하기
        SharedPreferences pref= this.getSharedPreferences("Data",MODE_PRIVATE);
        //MODE_PRIVATE만 사용 가능, 파일 입출력은 APPEND도 가능하다.
 
        //문서 작성을 시작한다는 메소드
        //실행하면 문서에 글작성을 해주는 Editor 객체가 리턴됨
 
        editor.putString("Name",name); // 키 값, 벨류
        editor.putInt("Age",age);
 
        //문서 작성이 끝났다.. 라고
        editor.commit();
 
    }
 
    public void clickLoad(View view) {
        SharedPreferences pref= getSharedPreferences("Data",MODE_PRIVATE);
 
        String name= pref.getString("Name","ni name"); //key(식별자), default value
        int age=pref.getInt("Age",0);
 
        tv.setText(name+" , "+age);
 
    }
}
 
 
 

<실행 화면>

3) Data base

4) Web서버 는 다음 시간에...

댓글