用户无法提取数据库

时间:2019-06-13 17:13:30

标签: android firebase kotlin google-cloud-firestore fetch

我正在尝试制作一个聊天应用程序。 ContactsActivity应该显示在我的应用程序中注册的所有用户,基本上可以作为“新消息”活动来工作。但是,由于某种原因,没有用户会出现。

ContactsActivity.kt

class ContactsActivity: AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        supportActionBar!!.setDisplayHomeAsUpEnabled(true)
        title = "Contacts"

        val firebaseUser = FirebaseAuth.getInstance().currentUser
        if (firebaseUser != null) {
            val fromUid = firebaseUser.uid
            val rootRef = FirebaseFirestore.getInstance()
            val uidRef = rootRef.collection("users").document(fromUid)
            uidRef.get().addOnCompleteListener { task ->
                if (task.isSuccessful) {
                    val document = task.result
                    if (document != null) {
                        if (document.exists()) {
                            val fromUser = document.toObject(User::class.java)
                            val userContactsRef = rootRef.collection("contacts").document(fromUid).collection("userContacts")
                            userContactsRef.get().addOnCompleteListener{ t ->
                                if (t.isSuccessful) {
                                    val listOfToUserNames = ArrayList<String>()
                                    val listOfToUsers = ArrayList<User>()
                                    val listOfRooms = ArrayList<String>()
                                    for (d in t.result!!) {
                                        val toUser = d.toObject(User::class.java)
                                        listOfToUserNames.add(toUser.userName)
                                        listOfToUsers.add(toUser)
                                        listOfRooms.add(d.id)
                                    }

                                    val arrayAdapter = ArrayAdapter(this, android.R.layout.simple_list_item_1, listOfToUserNames)
                                    list_viw.adapter = arrayAdapter
                                    list_viw.onItemClickListener = AdapterView.OnItemClickListener{_, _, position, _ ->
                                        val intent = Intent(this, ChatActivity::class.java)
                                        intent.putExtra("fromUser", fromUser)
                                        intent.putExtra("toUser", listOfToUsers[position])
                                        intent.putExtra("roomId", "noRoomId")
                                        startActivity(intent)
                                        finish()
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
    }

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
              xmlns:app="http://schemas.android.com/apk/res-auto"
              android:orientation="vertical"
              android:layout_width="match_parent"
              android:layout_height="match_parent"
              android:background="@android:color/holo_blue_dark">

    <ListView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:id="@+id/list_viw"/>

    <android.support.design.widget.BottomNavigationView
            android:id="@+id/bottom_navigation"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true"
            app:menu="@menu/bottom_nav_menu"
            app:itemBackground="@color/colorPrimary"
            app:itemIconTint="@android:color/white"
            app:itemTextColor="@android:color/white" />

</RelativeLayout>

我正在使用Firestore将所有数据存储在Firebase中。 json树如下所示: firestore json tree

你们对我如何解决有任何想法吗?

编辑:我不得不提到我迁移到AndroidX,并且遵循了Android X之外的Alex Mamo的教程。可能是阵列适配器的第48行引起问题吗?

0 个答案:

没有答案