gpt4 book ai didi

java - 如何更改 .push() 在 Android 上的 Firebase 实时数据库中添加数据的索引

转载 作者:行者123 更新时间:2023-12-02 02:15:45 24 4
gpt4 key购买 nike

使用 firebase .push() 使用随机键在指定引用的末尾创建一个新的子项,然后我们可以向其中添加数据。不过我想在它之间添加数据。例如:

如果这是最初的情况:

{ 
"-L7kYVU5-CxkqBCmJLO-" : {
"articleCategory" : "tech",
"articleText" : "Hi , today lets talk about android....",
"articleTitle" : "new Android version announced",
"articleUrl" : "www.somewebsite.com/android_article",
"imgUrl" : "www.image.com"
},
"-L7kZozxhubLtJaOG1cy" : {
"articleCategory" : "tech",
"articleText" : "Apple Homepod - is the new product by Apple.....",
"articleTitle" : "Apple HomePod",
"articleUrl" : "www.somewebsite.com/apple_homepod",
"imgUrl" : "www.someotherimage.com"
}
}

我想要什么-

 { 
"-L7kYVU5-CxkqBCmJLO-" : {
"articleCategory" : "tech",
"articleText" : "Hi , today lets talk about android....",
"articleTitle" : "new Android version announced",
"articleUrl" : "www.somewebsite.com/android_article",
"imgUrl" : "www.image.com"
},
{
"-L7kYVU5-CxkqBOKAtO-" : {
"articleCategory" : "tech",
"articleText" : "The new iphone....",
"articleTitle" : "Iphone X review",
"articleUrl" : "www.somewebsite.com/iphoneX_article",
"imgUrl" : "www.otherimage.com"
},
"-L7kZozxhubLtJaOG1cy" : {
"articleCategory" : "tech",
"articleText" : "Apple Homepod - is the new product by Apple.....",
"articleTitle" : "Apple HomePod",
"articleUrl" : "www.somewebsite.com/apple_homepod",
"imgUrl" : "www.someotherimage.com"
}
}

所以基本上我想在第一个子项之后使用随机键(或任何键)在两个现有项之间添加一个子项,而不是在通常使用 .push() 发生的列表末尾添加一个子项

使用 orderByChild() 方法可用于更改接收数据的顺序,也可以按特定的升序或降序排列(我对此可能是错误的,但这就是我从文档中理解的)

我不知道是否可能,但如果可以请帮助我,或者如果不可以有任何解决方法吗?

最佳答案

推送 ID 是随机的,但也基于它们的生成时间。他们总是按时间顺序排序。如果您需要按其他条件对记录进行排序,请使用子值。

关于java - 如何更改 .push() 在 Android 上的 Firebase 实时数据库中添加数据的索引,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49332010/

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