안드로이드 스튜디오 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());
}
});
}
}
반응형
'안드로이드 스튜디오' 카테고리의 다른 글
파이어베이스 연결해서 아두이노 제어 하는 앱 만들기 (0) | 2020.07.07 |
---|---|
안드로이드 스튜디오 DB- 파이어 베이스 (firebase)2 (0) | 2020.05.24 |
추가) recyclerview (0) | 2020.05.17 |
안드로이드 스튜디오 RecyclerView (0) | 2020.05.10 |
안드로이드 스튜디오 프래그먼트 2 (0) | 2020.05.09 |