- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我在 Cloud Firestore 中有 2 个不同的集合:
目标:我希望将这两个集合放在 Fragment 中,并使用 Androidx RecyclerView 来显示带有用户名和图片 URL 的帖子。
我在 build.gradle 中使用了“androidx.recyclerview:recyclerview:1.1.0”。
我使用以下方法从 Firebase 获取帖子并将其存储在模型“FirebasePost”中:
/**
* Get all FirebasePost objects from Firebase Firestore collection
* @return
*/
public FirestoreRecyclerOptions<FirebasePost> getPosts() {
Query mQuery = mFirestore.collection("posts").orderBy("createdAt", Query.Direction.DESCENDING);
// Get the response elements
FirestoreRecyclerOptions<FirebasePost> response = new FirestoreRecyclerOptions.Builder<FirebasePost>()
.setQuery(mQuery, FirebasePost.class)
.build();
return response;
}
我通过以下代码填充 RecyclerView:
/**
* Prepares the Recycler View for showing the Posts data
* @param mView Gets the current view
*/
private void prepareRecyclerView(View mView, FirestoreRecyclerOptions<FirebasePost> response) {
adapter = new FirestoreRecyclerAdapter<FirebasePost, PostHolder>(response) {
@Override
public void onBindViewHolder(@NonNull PostHolder holder, int position, @NonNull FirebasePost model) {
// Bind the FirebasePost object to the PostHolder
holder.setPostText_to_UI(model.getText());
holder.setTimestamp_to_UI(model.getCreatedAt());
holder.setAuthorDisplayName_to_UI(model.getAuthorUserId());
}
@NonNull
@Override
public PostHolder onCreateViewHolder(@NonNull ViewGroup group, int i) {
// Create a new instance of the ViewHolder, in this case we are using a custom
// layout called R.layout.message for each item
View view = LayoutInflater.from(group.getContext())
.inflate(R.layout.posts_item_cardview, group, false);
return new PostHolder(view);
}
};
mPostsRecyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
mPostsRecyclerView.setItemAnimator(new DefaultItemAnimator());
mPostsRecyclerView.setAdapter(adapter);
adapter.notifyDataSetChanged();
}
如何向 Firebase 实现第二个请求以获取用户信息并将这些信息添加到 RecyclerView 的正确位置?问题是,我得到了 AuthorUserId,但没有得到用户的 displayName,因为在将模型提供给 RecyclerView 之前,我无法向 Firestore 执行另一个请求。
最佳答案
I want to have these two collections in Fragment with an AndroidX
RecyclerView
to show the posts with the username andpictureURL
.
Firestore 中的查询是浅层查询,这意味着您只能从运行查询的集合中获取项目。无法在单个查询中从不同集合中获取文档。在 NoSQL 世界中,不存在基于两个表中存在的 id 的 INNER JOIN 概念。 Firestore 不支持一次性跨不同集合进行查询,除非您使用 Firestore collection group queries ,但事实并非如此。单个查询只能使用单个集合中文档的属性。
我能想到的最简单的解决方案是在每个帖子下存储用户的数据(用户名和图片URL)。这种做法称为非规范化,是 Firebase 的常见做法。如果您不熟悉 NoSQL 数据库,我建议您观看此视频,Denormalization is normal with the Firebase Database为了更好的理解。它适用于 Firebase 实时数据库,但相同的规则适用于 Cloud Firestore。
有关更多信息,我建议您还阅读我在以下帖子中的回答:
编辑:
复制数据时,需要记住一件事。同样的,你在添加数据,你需要维护它。换句话说,如果您想要更新/删除某个项目,您需要在该项目存在的每个位置执行此操作。这就是该技术的工作原理。
I want to avoid to iterate over all (maybe millions of) posts and delete the duplicated user data there.
对于数据库中的大量更新/删除,每次都是一种权衡,但这始终取决于您的具体用例和要求。另一种解决方案可能是查询数据库两次,一次获取帖子,然后获取相应的用户详细信息。这项技术并不像您想象的那么慢。所以你也应该考虑将其作为一种选择。
您还应该考虑的另一个解决方案是增强数据以接受反向查找。也就是说,您可以在每个用户对象下添加子集合中相应的帖子。如果需要查询所有用户的所有帖子,则需要进行集合组查询。
关于java - 使用 Firebase Firestore 中两个集合的数据填充 Androidx RecyclerView 和 CardView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60801247/
我回到我的 Android 项目来完成它的工作,突然我收到此错误消息 “无法解析 androidxAffected 模块:app” “错误:无法解析:androidx。受影响的模块:应用程序” 这是我
我今天尝试使用 Android Studio 中的“重构 -> 迁移到 AndroidX”选项将我当前的项目迁移到 AndroidX,每当我尝试编译我的应用程序时,我都会收到一个 Program ty
当我尝试构建应用程序时,它在构建选项卡上给了我这个错误 More than one file was found with OS independent path 'META-INF/androidx
我在尝试构建 Android 应用程序时遇到以下问题。我遇到了一些关于强制或使用手动依赖项解决策略的 android 帖子。这似乎并没有解决问题。 有人问过类似的问题:Similar stack ov
我刚刚通过 Android Studio 菜单选项 Refactor -> Refactor to AndroidX 迁移到 androidx 我收到以下错误: android.view.Inflat
我用 kotlin 开始新的 android 应用程序项目。我的 sdk gradle 配置是 compileSdkVersion 29 和 buildToolsVersion "29.0.1" 我试
我迁移到 AndroidX,然后如果我尝试使用 API 29 将项目运行到模拟器中,我会收到错误。 API 28 及之前的模拟器/真实设备没有问题。 java.lang.RuntimeExceptio
我正在开发 Android MVVM 应用程序。我只是手动切换到 androidx 库。这是我的 build.gradle 中的库: implementation 'com.google.code.g
在迁移到 AndroidX 并将所有 AndriodX 包更新到最新版本后,构建将失败,在 obj 中的构建生成的 XML 文件中显示三个类似的错误文件夹: \obj\Debug\100\lp\117
从 更新导航组合依赖后2.4.0-alpha03 至 2.4.0-alpha05 在尝试在可组合屏幕之间导航(例如从 taskComposable 导航到 listComposable 屏幕)后,我遇
我刚刚通过 Android Studio 菜单选项 重构 -> 重构到 AndroidX 迁移到 androidx 我收到以下错误: android.view.InflateException: Bi
我创建了一个 Android 应用程序,并从 Gallery 添加了“Navigation Drawer Activity”,我删除并重命名了菜单项。当我单击抽屉 Activity 中的任何菜单项启动
我正在制作一个项目,但是当我编译我的项目并在 Android Studio 上启动应用程序时,我的应用程序崩溃了。 在我的 Logcat 错误中 Process: com.passionategeek
完整的堆栈仅包括 android 核心代码: java.lang.NullPointerException: Attempt to invoke virtual method 'boolean and
将项目迁移到 AndroidX 后出现以下错误:程序类型已经存在androidx.concurrent.futures.DirectExecutor 我的应用Gradle如下: configu
我想在 Play 商店中更新应用程序的版本,但在测试中我开始收到此错误。 Superclass androidx.core.app.f of androidx.activity.ComponentAc
AndoirdX 升级后出现错误。 android.databinding.tool.util.LoggedErrorException: failure, see logs for details.
将项目迁移到 AndroidX 后我遇到了这个崩溃 Unable to start activity ComponentInfo{oackagename/com.paymob.acceptsdk.P
我的导航 Controller 中有此错误,我已经更改了 XML 中的声明,但没有帮助,我更改了 Fragment 为 FragmenContainer,但没有帮助。谁能帮我解决这个错误? 日志: E
我创建了一个水平回收器 View ,其中加载了项目。之后,我需要选择该项目并执行点击事件。到这个存档(代码在下面可用),现在我想改变点击项目的颜色,剩下的项目应该被取消选择。如果我没有正确查询,请道歉
我是一名优秀的程序员,十分优秀!