firestoreから送られてくるデータのmap化 #1

Merged
Utahshi merged 2 commits from murakumo-v2 into master 2024-06-19 04:48:48 +00:00
2 changed files with 69 additions and 10 deletions
Showing only changes of commit f0024f64d3 - Show all commits

View File

@ -4,11 +4,15 @@ import android.util.Log;
import com.google.android.gms.tasks.OnCompleteListener; import com.google.android.gms.tasks.OnCompleteListener;
import com.google.android.gms.tasks.Task; import com.google.android.gms.tasks.Task;
import com.google.firebase.Timestamp;
import com.google.firebase.firestore.CollectionReference; import com.google.firebase.firestore.CollectionReference;
import com.google.firebase.firestore.FirebaseFirestore; import com.google.firebase.firestore.FirebaseFirestore;
import com.google.firebase.firestore.QueryDocumentSnapshot; import com.google.firebase.firestore.QueryDocumentSnapshot;
import com.google.firebase.firestore.QuerySnapshot; import com.google.firebase.firestore.QuerySnapshot;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map; import java.util.Map;
public class FirestoreReception { public class FirestoreReception {
@ -20,7 +24,7 @@ public class FirestoreReception {
} }
//firestoreから受け取ったデータを束ねるためのマップ //firestoreから受け取ったデータを束ねるためのマップ
public Map<String, Object> firestoreData; public List<MyDataClass>myDataList = new ArrayList<>();
//ClassIdを引数にデータの作成を行う //ClassIdを引数にデータの作成を行う
public void getDocumentsByClassId(int classId, MainActivity context) { public void getDocumentsByClassId(int classId, MainActivity context) {
@ -36,18 +40,31 @@ public class FirestoreReception {
for (QueryDocumentSnapshot document : task.getResult()) { for (QueryDocumentSnapshot document : task.getResult()) {
Map<String, Object> data = document.getData(); Map<String, Object> data = document.getData();
// CreateRootクラスのインスタンスを作成しdataを引数として渡す //CreateRootクラスのインスタンスを生成dataを渡す
GeoCoder geoCoder = new GeoCoder(); // GeoCoder geoCoder = new GeoCoder();
geoCoder.processData(data, context); // geoCoder.processData(data, context);
// firestoreDataにdataを追加
firestoreData = data;
// ドキュメントのデータをMyDataClassのインスタンスにマッピング
MyDataClass myData = new MyDataClass(
(String) data.get("patronName"),
((Long) data.get("classId")).intValue(),
(List<String>) data.get("address"),
(List<Timestamp>) data.get("firstDay"),
((Long) data.get("studentNumber")).intValue(),
(String) data.get("childName"),
(List<Timestamp>) data.get("thirdDay"),
(List<Timestamp>) data.get("secondDay")
);
//リストに追加
myDataList.add(myData);
}
//取得したデータをログ表示
for(MyDataClass data :myDataList){
Log.i("FirestoreReceptiond", "data: " + data.toString());
} }
} else { } else {
Log.w("FirestoreReception", "Error getting documents.", task.getException()); Log.w("FirestoreReceptiond", "Error getting documents.", task.getException());
} }
Log.w ("FirestoreReception", "firestoreData: " + firestoreData.size());
} }
}); });

View File

@ -0,0 +1,42 @@
package com.example.oplogy;
import com.google.firebase.Timestamp;
import java.util.List;
public class MyDataClass {
String patronName;
int classId;
List<String> address;
List<Timestamp> firstDay;
int studentNumber;
String childName;
List<Timestamp> thirdDay;
List<Timestamp> secondDay;
double latitude;
public MyDataClass(String patronName, int classId, List<String> address, List<Timestamp> firstDay, int studentNumber, String childName, List<Timestamp> thirdDay, List<Timestamp> secondDay) {
this.patronName = patronName;
this.classId = classId;
this.address = address;
this.firstDay = firstDay;
this.studentNumber = studentNumber;
this.childName = childName;
this.thirdDay = thirdDay;
this.secondDay = secondDay;
}
@Override
public String toString() {
return "MyDataClass{" +
"patronName='" + patronName + '\'' +
", classId=" + classId +
", address=" + address +
", firstDay=" + firstDay +
", studentNumber=" + studentNumber +
", childName='" + childName + '\'' +
", thirdDay=" + thirdDay +
", secondDay=" + secondDay +
'}';
}
}