简体   繁体   English

如何在一行 LazyColumn 中显示 Firestore 中的多个文档字段

[英]How to show a multiple fields of documents in Firestore in one row of LazyColumn

I'm new to Firestore.我是 Firestore 的新手。 I need to show two of the values of the fields, "category" and "title" 3 , of documents in Firestore in one row of LazyColumn like this 1 .我需要在像这样的LazyColumn的一行中显示 Firestore 中文档的两个字段值“类别”和“标题” 3 I tried some codes as below but they are shown like this 2 .我尝试了一些如下代码,但它们显示如下2 Is it possible?可能吗?

My code of MainActivity我的 MainActivity 代码

var t = mutableListOf<String>()
    db.collection("posts")
        .get()
        
        .addOnSuccessListener { result ->
            for (document in result) {
                val category = document.data["category"].toString()
                val title = document.data["title"].toString()
                t.add(category)
                t.add(title)
                Log.d("black", "${document.id} => ${category}")
                Log.d("red", "${document.id} => ${title}")
            }
        }
        .addOnFailureListener { exception ->
            Log.w("gray", "Error getting documents.", exception)
        }

My code of View我的查看代码

LazyColumn(
            modifier = Modifier.background(Beige),
            verticalArrangement = Arrangement.spacedBy(10.dp),
            contentPadding = PaddingValues(5.dp)
        ) {

            items(posts) { post ->
                Row(
                    modifier = Modifier
                        .fillMaxWidth()

                        .selectable(
                            selected = true,
                            onClick = { navController.navigate("PostContent") }
                        )
                        .background(Color.White, RoundedCornerShape(5.dp))
                ) {

                    Text(
                        text = "$post/category\n$post/title",
                        color = Brown,
                        modifier = Modifier
                            .padding(40.dp)
                    )
                }

Thank you.谢谢你。

You have a bad implementation of the storing mechanism.你有一个糟糕的存储机制实现。

In the class where you are calling the database, add this line of code在调用数据库的 class 中,添加这行代码

data class Post(category: String, title: String)

Then, update the t variable like so然后,像这样更新t变量

var t = mutableStateListOf<Post>()

Then, update the storing mechanism然后,更新存储机制

db.collection("posts")
        .get()
        
        .addOnSuccessListener { result ->
            for (document in result) {
                val category = document.data["category"].toString()
                val title = document.data["title"].toString()
                t.add(Post(category, title))
                Log.d("black", "${document.id} => ${category}")
                Log.d("red", "${document.id} => ${title}")
            }
        }
        .addOnFailureListener { exception ->
            Log.w("gray", "Error getting documents.", exception)
        }

Now, I assume it is the same t variable you are somehow accessing in your LazyColumn as posts , so update that code like this现在,我假设它与您在LazyColumn中作为posts访问的t变量相同,因此像这样更新该代码

LazyColumn {

  items(posts) { post ->
                Row(
                    modifier = Modifier
                        .fillMaxWidth()

                        .selectable(
                            selected = true,
                            onClick = { navController.navigate("PostContent") }
                        )
                        .background(Color.White, RoundedCornerShape(5.dp))
                ) {

                    Text(
                        text = "${post.category}\n${post.title}",
                        color = Brown,
                        modifier = Modifier
                            .padding(40.dp)
                    )
                }

All done, modify the design however you like, the logic is good like this.大功告成,随心所欲修改设计,这样逻辑就好了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM