英文:
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="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() {
//Buraya Firestore dan kullanıcıya ait postları çekeceğiz.
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 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 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 = ""
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() {
//Buraya Firestore dan kullanıcıya ait postları çekeceğiz.
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 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()
}
}
}
}
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论