From f7566d9df06f86b04fd7efaa94a24eb866a55f50 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E3=82=8D=E3=82=80=E3=81=AD=E3=81=93?= Date: Mon, 15 Jan 2024 12:12:42 +0900 Subject: [PATCH] =?UTF-8?q?=E5=AE=9F=E8=A3=85=E3=82=92=E6=95=B4=E7=90=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/example/childguard/HomeFragment.java | 68 +++++++++++-------- 1 file changed, 40 insertions(+), 28 deletions(-) diff --git a/app/src/main/java/com/example/childguard/HomeFragment.java b/app/src/main/java/com/example/childguard/HomeFragment.java index 9405ba8..03dea26 100644 --- a/app/src/main/java/com/example/childguard/HomeFragment.java +++ b/app/src/main/java/com/example/childguard/HomeFragment.java @@ -12,6 +12,7 @@ import android.view.ViewGroup; import android.widget.Button; import android.widget.FrameLayout; import android.widget.TextView; +import android.widget.Toast; import androidx.annotation.NonNull; import androidx.core.content.res.ResourcesCompat; @@ -91,41 +92,52 @@ public class HomeFragment extends Fragment { @Override public void onClick(View v) { //初回起動かを保存する変数 - int i = Integer.parseInt(preferences.getString("kidoukaisuu", "1")); +// int i = Integer.parseInt(preferences.getString("kidoukaisuu", "1")); + boolean alreadySaved = preferences.getBoolean("alreadySaved", false); //ボタン変数の宣言 Button parent = view.findViewById(R.id.QRprinting); Button born = view.findViewById(R.id.QRprinting); - if (i == 1) {//QRコード印刷を初めて押したときにFireBaseへの登録を行う - String QRYomitorisya = parent.getText().toString();//変数に文字列を代入 - String valueBorn = born.getText().toString();//変数に文字列を代入 - Map user = new HashMap<>();//mapの宣言 + if (alreadySaved) { + Log.d("HomeFragment", "already printed"); + return; + } else Log.d("HomeFragment", "not printed yet"); // debug - Log.d("HomeFragment", "onClick is called"); + String valueParent = parent.getText().toString();//変数に文字列を代入 + String valueBorn = born.getText().toString();//変数に文字列を代入 + Map user = new HashMap<>();//mapの宣言 - //mapに入れる - user.put("parent", QRYomitorisya); - user.put("born", valueBorn); - //新しいドキュメントにIDを作って追加 - db.collection("users") - .add(user) - .addOnSuccessListener(new OnSuccessListener() { - @Override - public void onSuccess(DocumentReference documentReference) { - Log.d(TAG, "DocumentSnapshot added with ID: " + documentReference.getId()); - } - }) - .addOnFailureListener(new OnFailureListener() { - @Override - public void onFailure(@NonNull Exception e) {//エラー処理 - Log.w(TAG, "Error adding document", e); - } + Log.d("HomeFragment", "onClick is called"); - }); + //mapに入れる + user.put("parent", valueParent); + user.put("born", valueBorn); + //新しいドキュメントにIDを作って追加 + db.collection("users") + .add(user) + .addOnSuccessListener(new OnSuccessListener() { + @Override + public void onSuccess(DocumentReference documentReference) { + //成功したら + Log.d(TAG, "DocumentSnapshot added with ID: " + documentReference.getId()); + SharedPreferences.Editor e = preferences.edit(); + e.putBoolean("alreadySaved", true); + e.apply(); + //画面遷移 + replaceFragment(new QrPrintFragment()); + } + }) + .addOnFailureListener(new OnFailureListener() { + @Override + public void onFailure(@NonNull Exception e) { + //失敗したら + Log.w(TAG, "Error adding document", e); + } + }); - } - SharedPreferences.Editor e = preferences.edit(); - e.putString("kidoukiroku", "2"); - e.apply(); +// +// SharedPreferences.Editor e = preferences.edit(); +// e.putString("kidoukiroku", "2"); +// e.apply(); replaceFragment(new QrPrintFragment()); } });