クラスIDを基にデータの受信を行う

ログに出力してます
This commit is contained in:
nemukemo 2024-06-11 14:07:48 +09:00
parent 8c0f2530a5
commit 3149a5c498
3 changed files with 89 additions and 131 deletions

View File

@ -0,0 +1,66 @@
package com.example.oplogy;
import android.util.Log;
import com.google.android.gms.tasks.OnCompleteListener;
import com.google.android.gms.tasks.Task;
import com.google.firebase.Timestamp;
import com.google.firebase.firestore.CollectionReference;
import com.google.firebase.firestore.FirebaseFirestore;
import com.google.firebase.firestore.QueryDocumentSnapshot;
import com.google.firebase.firestore.QuerySnapshot;
import java.util.List;
import java.util.Map;
public class FirestoreReception {
private FirebaseFirestore db;
public FirestoreReception() {
db = FirebaseFirestore.getInstance();
}
//ClassIdを引数にデータの作成を行う
public void getDocumentsByClassId(int classId) {
CollectionReference collectionRef = db.collection("questionnaireForms");
// classIdが引数のものを取得する
collectionRef.whereEqualTo("classId", classId).get()
.addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
@Override
public void onComplete(Task<QuerySnapshot> task) {
if (task.isSuccessful()) {
// データの取得に成功した場合
for (QueryDocumentSnapshot document : task.getResult()) {
Map<String, Object> data = document.getData();
// デバッグ用のログ出力
Log.d("FirestoreReception", "Document ID: " + document.getId());
Log.d("FirestoreReception", "Data: " + data);
// ここでデータを取得し必要に応じて処理を行います
String parentName = (String) data.get("patronName");
String childName = (String) data.get("childName");
String studentId = (String) data.get("studentNumber");
Timestamp address = (Timestamp) data.get("adress");
List<Timestamp> firstDay = (List<Timestamp>) data.get("firstDay");
List<Timestamp> secondDay = (List<Timestamp>) data.get("secondDay");
List<Timestamp> thirdDay = (List<Timestamp>) data.get("thirdDay");
// 取得したデータを使って必要な処理を行う
Log.d("FirestoreReception", "ParentName: " + parentName);
Log.d("FirestoreReception", "ChildName: " + childName);
Log.d("FirestoreReception", "StudentNumber: " + studentId);
Log.d("FirestoreReception", "Address: " + address.toDate());
Log.d("FirestoreReception", "First Day: " + firstDay);
Log.d("FirestoreReception", "Second Day: " + secondDay);
Log.d("FirestoreReception", "Third Day: " + thirdDay);
}
} else {
Log.w("FirestoreReception", "Error getting documents.", task.getException());
}
}
});
}
}

View File

@ -1,8 +1,6 @@
package com.example.oplogy; package com.example.oplogy;
import android.annotation.SuppressLint;
import android.os.Bundle; import android.os.Bundle;
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;
@ -11,118 +9,37 @@ import android.widget.TextView;
import androidx.appcompat.app.AppCompatActivity; import androidx.appcompat.app.AppCompatActivity;
import com.google.firebase.firestore.FirebaseFirestore; import com.google.firebase.firestore.FirebaseFirestore;
import com.google.firebase.firestore.QueryDocumentSnapshot;
public class MainActivity extends AppCompatActivity implements View.OnClickListener { public class MainActivity extends AppCompatActivity implements View.OnClickListener {
private TextView textView; private TextView showText;
private Button btnShow; private EditText selectText;
private Button btnAdd;
private EditText number;
private EditText address;
private EditText date;
private EditText time;
private FirebaseFirestore db = FirebaseFirestore.getInstance();
private Button btnshow;
private FirebaseFirestore db;
private FirestoreReception firestoreReception;
Button button;
// TextView textView;
EditText editText;
@SuppressLint("MissingInflatedId")
@Override @Override
protected void onCreate(Bundle savedInstanceState) { protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState); super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); setContentView(R.layout.activity_main);
textView=findViewById(R.id.showText); db = FirebaseFirestore.getInstance();
btnShow=findViewById(R.id.btnShow); firestoreReception = new FirestoreReception();
btnAdd=findViewById(R.id.btnAdd); showText = findViewById(R.id.showText);
btnshow = findViewById(R.id.btnShow);
selectText = findViewById(R.id.selectEdit);
number=findViewById(R.id.editNumber); btnshow.setOnClickListener(v -> {
address=findViewById(R.id.editAddress); String classId = selectText.getText().toString();
date=findViewById(R.id.editDate); firestoreReception.getDocumentsByClassId(Integer.parseInt(classId));
time=findViewById(R.id.editTime);
// Read Data
// Firestoreのコレクションusersのドキュメント一覧を取得する
// 非同期で取得処理が動作する結果を受け取るために処理完了時のリスナーをセットする
db.collection("questionnaireForms").get().addOnCompleteListener(task -> {
String data = "";
if (task.isSuccessful()) {
for (QueryDocumentSnapshot document : task.getResult()) {
Log.d("@FB1", document.getId() + "=>" + document.getData());
data += document.getId() + "=>" + document.getData() + "\n";
}
} else {
data = "Error getting documents." + task.getException().getMessage();
}
textView.setText(data);//編集したデータを画面下部に表示
}); });
btnShow.setOnClickListener(v -> {
});
findViewById(R.id.mapmapcreate).setOnClickListener(
view->{
}
);
} }
@Override @Override
public void onClick(View v) { public void onClick(View view) {
} }
} }
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

@ -8,46 +8,21 @@
android:orientation="vertical"> android:orientation="vertical">
<TextView
android:id="@+id/showText"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<EditText <EditText
android:id="@+id/editNumber" android:id="@+id/selectEdit"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:hint="番号" /> 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 <Button
android:id="@+id/btnShow" android:id="@+id/btnShow"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:text="表示" /> android:hint="追加"/>
<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="マップ生成"