프로그래밍

안드로이드 스튜디오 DB- 파이어 베이스 (firebase)1

안드로이드 스튜디오
반응형

이후 나오는 순서에 따라 게정 연결하고, 필요 파일 설치

이후 파이어베이스에 들어가서 realtime database 생성 -> 테스트 모드 (규제에 들어가서 읽고 쓰기가 true로 되어 있는지 확인)

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".MainActivity">

    <TextView
        android:id="@+id/textView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textSize="20dp"
        android:layout_gravity="center"
        android:gravity="center"
        android:layout_weight="1"
        android:text="TextView" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <EditText
            android:id="@+id/edit_text"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:hint="TEXT"
            android:layout_weight="1"/>

        <Button
            android:id="@+id/button"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Button" />


    </LinearLayout>

</LinearLayout>
package com.example.firebase;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.google.firebase.database.DataSnapshot;
import com.google.firebase.database.DatabaseError;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import com.google.firebase.database.ValueEventListener;

public class MainActivity extends AppCompatActivity {
    private TextView textView;
    private Button button;
    private EditText editText;

    DatabaseReference myRootRef = FirebaseDatabase.getInstance().getReference();
    DatabaseReference conditionRef = myRootRef.child("Text");

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        textView = findViewById(R.id.textView);
        button = findViewById(R.id.button);
        editText = findViewById(R.id.edit_text);
        
    }
    @Override
    protected void onStart() {
        super.onStart(); //데이터 변활르 알기 위함

        conditionRef.addListenerForSingleValueEvent(new ValueEventListener() {
            @Override
            public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
                String text = dataSnapshot.getValue(String.class);
                textView.setText(text);
            } //데이터 값 변경 되었을 때

            @Override
            public void onCancelled(@NonNull DatabaseError databaseError) {

            } //에러가 나면
        });
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                conditionRef.setValue(editText.getText().toString());
            }
        });
    }
}

버튼을 누르면 입력한 내용이 파이어베이스에 저장됨

 

반응형