gpt4 book ai didi

java - 从 Cloud Firestore 获取 map 数据

转载 作者:行者123 更新时间:2023-11-28 05:42:34 27 4
gpt4 key购买 nike

我想从云 Firestore 获取 map 数据,这是我需要快速访问同一个数据库的 java 代码。附上java代码和数据库结构图供引用。

FirebaseFirestore db = FirebaseFirestore.getInstance();
DocumentReference mDocRef = db.collection("hmdhAcademy").document("notifications");

mDocRef.get().addOnCompleteListener(task -> {

if ( task.isSuccessful() ) {

DocumentSnapshot documentSnapshot = task.getResult();

if ( documentSnapshot != null && documentSnapshot.exists() ){

notificationList = (ArrayList) documentSnapshot.get("userNotifications");
adapter = new NotificationAdapter(this,notificationList);
mRecyclerView.setAdapter(adapter);
alertDialog.dismiss();
}
}
else {

Toast.makeText(NotificationActivity.this,"Check Internet Connection",Toast.LENGTH_SHORT).show();
alertDialog.dismiss();
}

});

if ( list.get(i) instanceof HashMap ){

final Intent intent = new Intent(mContext,NotificationDetail.class);

String title = (String)((HashMap)list.get((list.size()-1)-i)).get("title");
String body = (String)((HashMap)list.get((list.size()-1)-i)).get("body");
String image = (String)((HashMap)list.get((list.size()-1)-i)).get("notiImage");
String detail = (String)((HashMap)list.get((list.size()-1)-i)).get("detail");
}

enter image description here

我需要相同的标题、正文、notiImage 和详细信息。

请帮助我,我正在寻找任何地方的答案但失败了。

最佳答案

试试这个代码:

docRef.getDocument { (document, error) in
if let document = document, document.exists {
print(document.data()!)
let userNotifications = document.data()["userNotifications"] as? [[String:Any]]
for notificaton in userNotifications {
let body = notificaton["body"] as? String ?? ""
let title = notificaton["title"] as? String ?? ""
print(body, title)

}

} else {
print("Document does not exist")
}
}

或者这样试试:

  1. 创建一个结构

    struct usernotifcatons {

    var body: String = ""
    var detail: String = ""
    var notiImage: String = ""
    var title: String = ""


    init(notificationData: [String:Any]) {

    let body = notificationData["body"] as? String ?? ""
    self.body = body

    let detail = notificationData["detail"] as? String ?? ""
    self.detail = detail

    let notiImage = notificationData["notiImage"] as? String ?? ""
    self.notiImage = notiImage

    let title = notificationData["title"] as? String ?? ""
    self.title = title
    }

    }
  2. 在您的 ViewController 中创建一个变量:

    var allNotifications: [usernotifcatons] = [usernotifcatons]()
  3. 将所有数据映射到您的 Firestore 代码中的一行

    docRef.getDocument { (document, error) in
    if let document = document, document.exists {
    print(document.data()!)
    let userNotifications = document.data()?["userNotifications"] as? [[String:Any]]
    for data in userNotifications! {
    self.allNotifications.append(usernotifications(notificationData: data))
    }
    print(self.allNotifications)

    } else {
    print("Document does not exist")
    }
    }

关于java - 从 Cloud Firestore 获取 map 数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56105966/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com