片段中的快照返回为空。

huangapple go评论56阅读模式
英文:

Snapshot in the fragment is returning empty

问题

class PostListFragment() : Fragment() {

    var user_mail = "example@example.com"
    var postList = ArrayList<Posts>()
    private lateinit var binding: FragmentPostListBinding
    private lateinit var recyclerViewAdapter: RecyclerAdapter

    private lateinit var firestore: FirebaseFirestore
    private lateinit var auth: FirebaseAuth

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding = FragmentPostListBinding.inflate(requireActivity().layoutInflater)
        firestore = FirebaseFirestore.getInstance()
        auth = FirebaseAuth.getInstance()
        getPostSelf()

        for (i in postList) {
            println(i.postLabel)
        }
        // postList is empty because snapshot is empty

        recyclerViewAdapter = RecyclerAdapter(postList)
        val layoutManager = LinearLayoutManager(activity?.applicationContext)
        binding.recyclerView.layoutManager = layoutManager
        binding.recyclerView.adapter = recyclerViewAdapter
    }

    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_post_list, container, false)
    }

    @SuppressLint("NotifyDataSetChanged")
    fun getPostSelf() {
        // Here we will retrieve posts belonging to the user from Firestore.
        firestore.collection("Posts").whereEqualTo("post_mail", user_mail)
            .addSnapshotListener { snapshot, exception ->
                if (exception != null) {
                    Toast.makeText(activity?.applicationContext, "Error", Toast.LENGTH_LONG).show()
                } else {
                    println("Snapshot control: " + snapshot?.isEmpty())
                    // Logcat screen is giving "Snapshot control: true"
                    if (snapshot != null) {
                        if (!snapshot.isEmpty) {
                            var documents = snapshot.documents
                            postList.clear()

                            for (document in documents) {
                                var postObject = Posts(user_mail)
                                postObject.postLabel = document.get("post_label") as String
                                postObject.postDesc = document.get("post_desc") as String
                                postObject.postTime = document.get("post_time") as String
                                postObject.postFullName = document.get("post_FullName") as String
                                postList.add(postObject)
                                println(postObject.postLabel)
                            }
                            recyclerViewAdapter.notifyDataSetChanged()
                        }
                    }
                }
            }
    }
}
英文:

I'm trying to create recycler view in fragment this class is fragment class.But snapshot is returning empty.I'm using same code for getting data( getPostSelf() ) in another class.It is working.So getting data code( getPostSelf() ) is true.

class PostListFragment() : Fragment() {

    var user_mail=&quot;example@example.com&quot;
    var postList =ArrayList&lt;Posts&gt;()
    private lateinit var binding: FragmentPostListBinding
    private lateinit var recyclerViewAdapter : RecyclerAdapter

    private lateinit var firestore: FirebaseFirestore
    private lateinit var auth: FirebaseAuth
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding=FragmentPostListBinding.inflate(requireActivity().layoutInflater)
        firestore=FirebaseFirestore.getInstance()
        auth= FirebaseAuth.getInstance()
        getPostSelf()
        
        for(i in postList){
            println(i.postLabel)
        }
        //postList is empty because snapshot is empty 

        recyclerViewAdapter= RecyclerAdapter(postList)
        val layoutManager= LinearLayoutManager(activity?.applicationContext)
        binding.recyclerView.layoutManager=layoutManager
        binding.recyclerView.adapter=recyclerViewAdapter
    }

    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_post_list, container, false)
    }

    @SuppressLint(&quot;NotifyDataSetChanged&quot;)
    fun getPostSelf() {

        //Buraya Firestore dan kullanıcıya ait postları &#231;ekeceğiz.

        firestore.collection(&quot;Posts&quot;).whereEqualTo(&quot;post_mail&quot;, user_mail)
            .addSnapshotListener { snapshot, exception -&gt;
                if (exception != null) {
                    Toast.makeText(activity?.applicationContext, &quot;deeee&quot;, Toast.LENGTH_LONG).show()
                } else {
                     println(&quot;Snapshot control : &quot;+snapshot?.isEmpty())
                     // Logcat screen is giving &quot;Snapshot control : true&quot;
                    if (snapshot != null) {
                        if (!snapshot.isEmpty) {

                            var documents = snapshot.documents
                            postList.clear()

                            for (document in documents) {
                                var postObject = Posts(user_mail)
                                postObject.postLabel = document.get(&quot;post_label&quot;) as String
                                postObject.postDesc = document.get(&quot;post_desc&quot;) as String
                                postObject.postTime = document.get(&quot;post_time&quot;) as String
                                postObject.postFullName = document.get(&quot;post_FullName&quot;) as String
                                postList.add(postObject)
                                println(postObject.postLabel)
                            }
                            recyclerViewAdapter.notifyDataSetChanged()

                        }


                    }

                }
            }
    }

    }

I tried this function in the activity where the fragment is located.It isn't working.

答案1

得分: 0

user_mail是一个空字符串。我认为这是因为没有匹配的项目,其post_mail等于""。

另一个问题是视图在错误的地方创建。它应该在onCreateView中创建。

class PostListFragment() : Fragment() {
    var user_mail = ""
    var postList = ArrayList<Posts>()
    private lateinit var binding: FragmentPostListBinding
    private lateinit var recyclerViewAdapter: RecyclerAdapter

    private lateinit var firestore: FirebaseFirestore
    private lateinit var auth: FirebaseAuth

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        firestore = FirebaseFirestore.getInstance()
        auth = FirebaseAuth.getInstance()
        getPostSelf()

        for (i in postList) {
            println(i.postLabel)
        }
        // postList is empty because snapshot is empty
    }

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        getPostSelf()

        // Inflate the layout for this fragment
        binding = FragmentPostListBinding.inflate(inflater, container, false)

        recyclerViewAdapter = RecyclerAdapter(postList)
        val layoutManager = LinearLayoutManager(activity?.applicationContext)
        binding.recyclerView.layoutManager = layoutManager
        binding.recyclerView.adapter = recyclerViewAdapter

        return binding.root
    }

    @SuppressLint("NotifyDataSetChanged")
    fun getPostSelf() {
        // 在这里从Firestore获取用户的帖子。
        firestore.collection("Posts").whereEqualTo("post_mail", user_mail)
            .addSnapshotListener { snapshot, exception ->
                if (exception != null) {
                    Toast.makeText(activity?.applicationContext, "deeee", Toast.LENGTH_LONG).show()
                } else {
                    println("Snapshot control : " + snapshot?.isEmpty())
                    // Logcat屏幕上显示"Snapshot control : true"
                    if (snapshot != null) {
                        if (!snapshot.isEmpty) {
                            var documents = snapshot.documents
                            postList.clear()

                            for (document in documents) {
                                var postObject = Posts(user_mail)
                                postObject.postLabel = document.get("post_label") as String
                                postObject.postDesc = document.get("post_desc") as String
                                postObject.postTime = document.get("post_time") as String
                                postObject.postFullName = document.get("post_FullName") as String
                                postList.add(postObject)
                                println(postObject.postLabel)
                            }
                            recyclerViewAdapter.notifyDataSetChanged()
                        }
                    }
                }
            }
    }
}
英文:

user_mail is an empty string. I think because of it, there is no matching item that has post_mail equal to "".

Another problem is the view is created in a wrong place. It should be create in onCreateView.

class PostListFragment() : Fragment() {
var user_mail = &quot;&quot;
var postList = ArrayList&lt;Posts&gt;()
private lateinit var binding: FragmentPostListBinding
private lateinit var recyclerViewAdapter : RecyclerAdapter
private lateinit var firestore: FirebaseFirestore
private lateinit var auth: FirebaseAuth
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
firestore = FirebaseFirestore.getInstance()
auth = FirebaseAuth.getInstance()
getPostSelf()
for(i in postList){
println(i.postLabel)
}
// postList is empty because snapshot is empty
}
override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
getPostSelf()
// Inflate the layout for this fragment
binding = FragmentPostListBinding.inflate(inflater, container, false)
recyclerViewAdapter = RecyclerAdapter(postList)
val layoutManager = LinearLayoutManager(activity?.applicationContext)
binding.recyclerView.layoutManager = layoutManager
binding.recyclerView.adapter = recyclerViewAdapter
return binding.root
}
@SuppressLint(&quot;NotifyDataSetChanged&quot;)
fun getPostSelf() {
//Buraya Firestore dan kullanıcıya ait postları &#231;ekeceğiz.
firestore.collection(&quot;Posts&quot;).whereEqualTo(&quot;post_mail&quot;, user_mail)
.addSnapshotListener { snapshot, exception -&gt;
if (exception != null) {
Toast.makeText(activity?.applicationContext, &quot;deeee&quot;, Toast.LENGTH_LONG).show()
} else {
println(&quot;Snapshot control : &quot; + snapshot?.isEmpty())
// Logcat screen is giving &quot;Snapshot control : true&quot;
if (snapshot != null) {
if (!snapshot.isEmpty) {
var documents = snapshot.documents
postList.clear()
for (document in documents) {
var postObject = Posts(user_mail)
postObject.postLabel = document.get(&quot;post_label&quot;) as String
postObject.postDesc = document.get(&quot;post_desc&quot;) as String
postObject.postTime = document.get(&quot;post_time&quot;) as String
postObject.postFullName = document.get(&quot;post_FullName&quot;) as String
postList.add(postObject)
println(postObject.postLabel)
}
recyclerViewAdapter.notifyDataSetChanged()
}
}
}
}
}
}

huangapple
  • 本文由 发表于 2023年7月13日 18:25:30
  • 转载请务必保留本文链接:https://go.coder-hub.com/76678343.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定