From 2368bf243481516c2d242702ccc62e8f47adff46 Mon Sep 17 00:00:00 2001 From: nemukemo Date: Tue, 4 Jun 2024 20:43:18 +0900 Subject: [PATCH] =?UTF-8?q?cloudFireStore=E8=BF=BD=E5=8A=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .idea/misc.xml | 2 +- app/build.gradle | 2 + .../java/com/example/oplogy/MainActivity.java | 113 ++++++++++++++++++ app/src/main/res/layout/activity_main.xml | 54 +++++++-- build.gradle | 6 +- 5 files changed, 162 insertions(+), 15 deletions(-) diff --git a/.idea/misc.xml b/.idea/misc.xml index 8978d23..773fe0f 100644 --- a/.idea/misc.xml +++ b/.idea/misc.xml @@ -1,6 +1,6 @@ - + diff --git a/app/build.gradle b/app/build.gradle index 7c8f3cd..a7db911 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -1,5 +1,6 @@ plugins { id 'com.android.application' + id 'com.google.gms.google-services' } android { @@ -33,6 +34,7 @@ dependencies { implementation 'androidx.appcompat:appcompat:1.6.1' implementation 'com.google.android.material:material:1.5.0' implementation 'androidx.constraintlayout:constraintlayout:2.1.4' + implementation 'com.google.firebase:firebase-firestore:24.4.1' testImplementation 'junit:junit:4.13.2' androidTestImplementation 'androidx.test.ext:junit:1.1.5' androidTestImplementation 'androidx.test.espresso:espresso-core:3.5.1' diff --git a/app/src/main/java/com/example/oplogy/MainActivity.java b/app/src/main/java/com/example/oplogy/MainActivity.java index 86bff69..30ba0e1 100644 --- a/app/src/main/java/com/example/oplogy/MainActivity.java +++ b/app/src/main/java/com/example/oplogy/MainActivity.java @@ -1,14 +1,127 @@ package com.example.oplogy; +import androidx.annotation.NonNull; import androidx.appcompat.app.AppCompatActivity; import android.os.Bundle; +import android.util.Log; +import android.view.View; +import android.widget.Button; +import android.widget.EditText; +import android.widget.TextView; + +import com.google.android.gms.tasks.OnFailureListener; +import com.google.android.gms.tasks.OnSuccessListener; +import com.google.firebase.firestore.DocumentReference; +import com.google.firebase.firestore.DocumentSnapshot; +import com.google.firebase.firestore.FirebaseFirestore; +import com.google.firebase.firestore.QueryDocumentSnapshot; + +import java.util.HashMap; +import java.util.Map; public class MainActivity extends AppCompatActivity { + private TextView textView; + private Button btnShow; + private Button btnAdd; + private EditText number; + private EditText address; + private EditText date; + private EditText time; + private FirebaseFirestore db = FirebaseFirestore.getInstance(); @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); + + textView=findViewById(R.id.showText); + btnShow=findViewById(R.id.btnShow); + btnAdd=findViewById(R.id.btnAdd); + + number=findViewById(R.id.editNumber); + address=findViewById(R.id.editAddress); + date=findViewById(R.id.editDate); + time=findViewById(R.id.editTime); + + + //データの追加(適当なデータを追加しています。実際にはデータベースに保存したいデータを追加してください。) + + findViewById(R.id.btnAdd).setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View v) { + User user = new User(number.getText().toString(), address.getText().toString(),date.getText().toString(),time.getText().toString()); + + + db.collection("users") + .add(user) + .addOnSuccessListener(documentReference -> Log.d("@FB1", "DocumentSnapshot added with ID: " + documentReference.getId())) + .addOnFailureListener(e -> Log.w("@FB1", "Error adding document", e)); + } + }); + + + btnShow.setOnClickListener(v -> { + // ⑤Read Data + // Firestoreのコレクション「users」のドキュメント一覧を取得する + // 非同期で取得処理が動作する。結果を受け取るために処理完了時のリスナーをセットする + db.collection("users").get().addOnCompleteListener(task -> { + String data = ""; + if (task.isSuccessful()) { + for (QueryDocumentSnapshot document : task.getResult()) { + Log.d("@FB1", document.getId() + "=>" + document.getData()); + User user = document.toObject(User.class); + data += user + "\n"; + } + } else { + data = "Error getting documents." + task.getException().getMessage(); + } + textView.setText(data);//編集したデータを画面下部に表示 + }); + + + }); } +} +class User { + private String number; + private String address; + private String date; + private String time; + + public User() { + } + + public User(String number, String address, String data, String time) { + this.number = number; + this.address = address; + this.date = data; + this.time = time; + } + //getterとsetter + public String getNumber() { + return number; + } + public String getAddress() { + return address; + } + public String getDate() { + return date; + } + public String getTime() { + return time; + } + public void setNumber(String number) { + this.number = number; + } + public void setAddress(String address) { + this.address = address; + } + public void setDate(String date) { + this.date = date; + } + public void setTime(String time) { + this.time = time; + } + } \ No newline at end of file diff --git a/app/src/main/res/layout/activity_main.xml b/app/src/main/res/layout/activity_main.xml index c052e59..039dfbc 100644 --- a/app/src/main/res/layout/activity_main.xml +++ b/app/src/main/res/layout/activity_main.xml @@ -1,22 +1,50 @@ - + tools:context=".MainActivity" + android:orientation="vertical"> + + + + + +