firestoreから受け取ったデータを一本の配列(Map)化

This commit is contained in:
nemukemo 2024-06-18 20:45:52 +09:00
parent 8e8916c47b
commit f0024f64d3
2 changed files with 69 additions and 10 deletions

View File

@ -4,11 +4,15 @@ 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.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class FirestoreReception {
@ -20,7 +24,7 @@ public class FirestoreReception {
}
//firestoreから受け取ったデータを束ねるためのマップ
public Map<String, Object> firestoreData;
public List<MyDataClass>myDataList = new ArrayList<>();
//ClassIdを引数にデータの作成を行う
public void getDocumentsByClassId(int classId, MainActivity context) {
@ -36,20 +40,33 @@ public class FirestoreReception {
for (QueryDocumentSnapshot document : task.getResult()) {
Map<String, Object> data = document.getData();
// CreateRootクラスのインスタンスを作成しdataを引数として渡す
GeoCoder geoCoder = new GeoCoder();
geoCoder.processData(data, context);
// firestoreDataにdataを追加
firestoreData = data;
//CreateRootクラスのインスタンスを生成dataを渡す
// GeoCoder geoCoder = new GeoCoder();
// geoCoder.processData(data, context);
// ドキュメントのデータを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 {
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 +
'}';
}
}