繁体   English   中英

如何将我的应用程序从 firebase 获取的数据设置到我的 bottomSheetView? (科特林)

[英]How can I set the fetched data of my app from firebase to my bottomSheetView? (Kotlin)

这是我第一次在 stackoverflow 中提出问题,我希望你能理解我是如何像新手一样构造这个问题的。 所以,我在一个顶点项目中工作,特别是一个跟踪设备,我一直在取得进展,只是在这部分有点卡在这里。 所以我想要发生的是我希望从 firebase 数据库中获取的数据显示在我的底部视图中。 这部分来自登录页面。

private lateinit var auth: FirebaseAuth

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_login_page)

    auth= FirebaseAuth.getInstance()
    register.setOnClickListener {
        var intent = Intent(this,Register_page::class.java)
        startActivity(intent)
        finish()
    }

    loginpage()
}

fun loginpage() {
    val sharedPref = this.getSharedPreferences("logIn", Context.MODE_PRIVATE)?:return
    val editor = sharedPref.edit()
    editor.putBoolean("Finished", true)
    editor.apply()


    login.setOnClickListener {
        if(checking()){
            val email=email.text.toString()
            val password= password.text.toString()
            auth.signInWithEmailAndPassword(email, password)
                .addOnCompleteListener(this) { task ->
                    if (task.isSuccessful) {
                        var intent = Intent(this,MapsActivity::class.java)
                        intent.putExtra("email",email)
                        startActivity(intent)
                        Toast.makeText(this, "Welcome", Toast.LENGTH_LONG).show()
                        finish()
                    } else {
                        Toast.makeText(this, "Wrong Details", Toast.LENGTH_LONG).show()
                    }
                }
        }
        else{
            Toast.makeText(this,"Enter the Details", Toast.LENGTH_LONG).show()
        }
    }
}


private fun checking():Boolean
{
    if(email.text.toString().trim{it<=' '}.isNotEmpty()
        && password.text.toString().trim{it<=' '}.isNotEmpty())
    {
        return true
    }
    return false
}

这部分来自我的登录页面,我希望将登录时获取的数据设置在bottomsheetview中。

   menu.setOnClickListener {


        val sharedPref = this.getSharedPreferences("inMap", Context.MODE_PRIVATE)
        val isLogin = sharedPref.getString("Email","1")
        val editor = sharedPref.edit()
        editor.putBoolean("Finished", true)
        editor.apply()
       var email = intent.getStringExtra("email")
       Toast.makeText(this, "${email}", Toast.LENGTH_LONG).show()



       if(isLogin=="1"){
            var email = intent.getStringExtra("email")
            if(email != null){
                setText(email)
                with(sharedPref.edit())
                {
                    putString("email",email)
                    apply()
                }
            }else{
                var intent = Intent(this,Login_page::class.java)
                startActivity(intent)
                finish()
            }
        }else{
            db= FirebaseFirestore.getInstance()
            var email=intent.getStringExtra("email")
            if (email != null) {
                Toast.makeText(this, "Woi, waya", Toast.LENGTH_LONG).show()

                db.collection("USERS").document(email).get()
                    .addOnSuccessListener {
                            tasks->
                        name.text=tasks.get("Name").toString()
                        phone.text=tasks.get("Phone").toString()
                        emailLog.text=tasks.get("email").toString()

                    }
            }
        }

        val bottomSheetDialog = BottomSheetDialog(
            this, R.style.BottomSheetDialogTheme
        )

        val bottomSheetView = LayoutInflater.from(applicationContext).inflate(
            R.layout.fragment_bottom_sheet,
            findViewById<LinearLayout>(R.id.bottomSheet)
        )


        bottomSheetView.findViewById<View>(R.id.logout).setOnClickListener{
            val sharedPref = this.getSharedPreferences("inMap", Context.MODE_PRIVATE)
            sharedPref.edit().remove("Email").apply()
            var intent = Intent(this,Login_page::class.java)
            sharedPref.getBoolean("Finished", false)
            startActivity(intent)
            finish()

        }

        bottomSheetDialog.setContentView(bottomSheetView)
        bottomSheetDialog.show()

    }

这是 xml 文件,我试图在其中引用获取的数据

<?xml version="1.0" encoding="utf-8"?>
  <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/bottomSheet"
    android:layout_width="match_parent"
    android:background="@drawable/bottom_sheet_background"
    android:layout_height="match_parent"
    android:orientation="vertical">


    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="SIIT - SBMARS"
        android:gravity="center"
        android:padding="10dp"
        android:textColor="@color/black"
        android:textSize="18sp"
        android:textStyle="bold" />

    <View
        android:layout_width="match_parent"
        android:layout_height="1dp"
        android:layout_marginBottom="10dp"
        android:background="@color/grey"/>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:padding="5dp">

        <com.makeramen.roundedimageview.RoundedImageView
            android:layout_width="100dp"
            android:layout_height="100dp"
            android:layout_margin="10dp"
            android:scaleType="centerCrop"
            android:src="@drawable/siitlogo"
            app:riv_corner_radius="10dp" />

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_marginEnd="10dp"
            android:gravity="center_vertical"
            android:orientation="vertical">

            <TextView
                android:id="@+id/name"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="Rodgin P. Misterio"
                android:textColor="@color/black"
                android:textSize="17dp"
                android:textStyle="bold"/>

            <TextView
                android:id="@+id/phone"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="rodgin5@gmail.com"
                android:textColor="@color/black"
                android:textSize="15dp"/>

            <TextView
                android:id="@+id/emailLog"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="Digoy Residence"
                android:textColor="@color/black"
                android:textSize="17dp"
                android:textStyle="bold"/>

        </LinearLayout>

    </LinearLayout>

    <View
        android:layout_width="match_parent"
        android:layout_height="1dp"
        android:layout_marginTop="10dp"
        android:layout_marginBottom="10dp"
        android:background="@color/grey"/>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="?android:attr/selectableItemBackground"
        android:clickable="true"
        android:focusable="true"
        android:gravity="center_vertical"
        android:orientation="horizontal"
        android:padding="12dp">

        <ImageView
            android:layout_width="25dp"
            android:layout_height="25dp"
            android:layout_marginStart="10dp"
            android:src="@drawable/logs"/>

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginStart="15dp"
            android:layout_marginEnd="15dp"
            android:text="Sessions"/>

    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="?android:attr/selectableItemBackground"
        android:clickable="true"
        android:focusable="true"
        android:gravity="center_vertical"
        android:orientation="horizontal"
        android:padding="12dp">

        <ImageView
            android:layout_width="25dp"
            android:layout_height="25dp"
            android:layout_marginStart="10dp"
            android:src="@drawable/logs"/>

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginStart="15dp"
            android:layout_marginEnd="15dp"
            android:text="Logs"/>

    </LinearLayout>

    <Button
        android:id="@+id/logout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="20dp"
        android:text="Log-out"
        android:background="@drawable/custom_button"
        android:textColor="@color/white"
        android:textSize="15sp"
        android:textStyle="bold"/>

</LinearLayout>

请帮助呼呼

据我了解您的问题,对此的简单解决方案将创建一个 function 以打开底部工作表并从内部传递 3 个必需值作为参数.addOnSuccessListener

Function 声明 -

fun openBottomsheet(name: String, email: String, phone: String) {
    // open bottom sheet and pass these values to it
}

如果您可以使用数据绑定并创建一个视图模型来处理数据表示,那就太好了。 您的数据将自动从您的数据库更新到您的底页。

暂无
暂无

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

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