cloudFireStore追加
This commit is contained in:
parent
3161cb1408
commit
2368bf2434
|
@ -1,6 +1,6 @@
|
|||
<project version="4">
|
||||
<component name="ExternalStorageConfigurationManager" enabled="true" />
|
||||
<component name="ProjectRootManager" version="2" languageLevel="JDK_17" default="true" project-jdk-name="jbr-17" project-jdk-type="JavaSDK">
|
||||
<component name="ProjectRootManager" version="2" languageLevel="JDK_17" project-jdk-name="jbr-17" project-jdk-type="JavaSDK">
|
||||
<output url="file://$PROJECT_DIR$/build/classes" />
|
||||
</component>
|
||||
<component name="ProjectType">
|
||||
|
|
|
@ -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'
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
|
||||
}
|
|
@ -1,22 +1,50 @@
|
|||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
<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"
|
||||
tools:context=".MainActivity">
|
||||
tools:context=".MainActivity"
|
||||
android:orientation="vertical">
|
||||
|
||||
<EditText
|
||||
android:id="@+id/editNumber"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:hint="番号" />
|
||||
<EditText
|
||||
android:id="@+id/editAddress"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:hint="住所" />
|
||||
<EditText
|
||||
android:id="@+id/editDate"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:hint="日付" />
|
||||
<EditText
|
||||
android:id="@+id/editTime"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:hint="時間" />
|
||||
<Button
|
||||
android:id="@+id/btnAdd"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:hint="追加"/>
|
||||
<Button
|
||||
android:id="@+id/btnShow"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:text="表示" />
|
||||
|
||||
|
||||
<TextView
|
||||
android:layout_width="wrap_content"
|
||||
android:id="@+id/showText"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:text="Hello World!"
|
||||
app:layout_constraintBottom_toBottomOf="parent"
|
||||
app:layout_constraintEnd_toEndOf="parent"
|
||||
app:layout_constraintStart_toStartOf="parent"
|
||||
app:layout_constraintTop_toTopOf="parent" />
|
||||
<Button
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
/>
|
||||
android:text="" />
|
||||
|
||||
</androidx.constraintlayout.widget.ConstraintLayout>
|
||||
|
||||
|
||||
</LinearLayout>
|
|
@ -1,4 +1,8 @@
|
|||
// Top-level build file where you can add configuration options common to all sub-projects/modules.
|
||||
buildscript {
|
||||
dependencies {
|
||||
classpath 'com.google.gms:google-services:4.3.14'
|
||||
}
|
||||
}// Top-level build file where you can add configuration options common to all sub-projects/modules.
|
||||
plugins {
|
||||
id 'com.android.application' version '8.0.2' apply false
|
||||
id 'com.android.library' version '8.0.2' apply false
|
||||
|
|
Loading…
Reference in New Issue
Block a user