Merge pull request #3 from N-YOKU-jp/newNemukemo

cloudFireStore追加
This commit is contained in:
ユタ氏 2024-06-05 14:25:15 +09:00 committed by GitHub
commit b10f3409a6
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 168 additions and 2 deletions

View File

@ -1,5 +1,8 @@
plugins { plugins {
id 'com.android.application' id 'com.android.application'
id 'com.google.gms.google-services'
id 'com.google.android.libraries.mapsplatform.secrets-gradle-plugin' id 'com.google.android.libraries.mapsplatform.secrets-gradle-plugin'
} }
@ -37,6 +40,7 @@ dependencies {
implementation 'androidx.appcompat:appcompat:1.6.1' implementation 'androidx.appcompat:appcompat:1.6.1'
implementation 'com.google.android.material:material:1.5.0' implementation 'com.google.android.material:material:1.5.0'
implementation 'androidx.constraintlayout:constraintlayout:2.1.4' implementation 'androidx.constraintlayout:constraintlayout:2.1.4'
implementation 'com.google.firebase:firebase-firestore:24.4.1'
implementation 'com.google.android.gms:play-services-maps:18.2.0' implementation 'com.google.android.gms:play-services-maps:18.2.0'
testImplementation 'junit:junit:4.13.2' testImplementation 'junit:junit:4.13.2'
androidTestImplementation 'androidx.test.ext:junit:1.1.5' androidTestImplementation 'androidx.test.ext:junit:1.1.5'

View File

@ -1,16 +1,41 @@
package com.example.oplogy; package com.example.oplogy;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity; import androidx.appcompat.app.AppCompatActivity;
import android.annotation.SuppressLint; import android.annotation.SuppressLint;
import android.os.Bundle; import androidd.os.Bunle;
import android.util.Log;
import android.view.View; import android.view.View;
import android.widget.Button; import android.widget.Button;
import android.widget.EditText; import android.widget.EditText;
import android.widget.TextView; 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();
public class MainActivity extends AppCompatActivity implements View.OnClickListener { public class MainActivity extends AppCompatActivity implements View.OnClickListener {
Button button; Button button;
// TextView textView; // TextView textView;
EditText editText; EditText editText;
@ -21,6 +46,54 @@ public class MainActivity extends AppCompatActivity implements View.OnClickListe
setContentView(R.layout.activity_main); 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);//編集したデータを画面下部に表示
});
});
findViewById(R.id.mapmapcreate).setOnClickListener( findViewById(R.id.mapmapcreate).setOnClickListener(
view->{ view->{
@ -33,5 +106,48 @@ public class MainActivity extends AppCompatActivity implements View.OnClickListe
@Override @Override
public void onClick(View v) { public void onClick(View v) {
} }
}
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;
}
} }

View File

@ -7,6 +7,47 @@
tools:context=".MainActivity" tools:context=".MainActivity"
android:orientation="vertical"> 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:id="@+id/showText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="" />
=======
<Button <Button
android:id="@+id/mapmapcreate" android:id="@+id/mapmapcreate"
android:text="マップ生成" android:text="マップ生成"
@ -24,4 +65,5 @@
android:text="" android:text=""
/> />
</LinearLayout> </LinearLayout>

View File

@ -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 { plugins {
id 'com.android.application' version '8.0.2' apply false id 'com.android.application' version '8.0.2' apply false
id 'com.android.library' version '8.0.2' apply false id 'com.android.library' version '8.0.2' apply false