send data to fragment from activity android

Viewed 31

I´m trying to send data user to fragment in kotlin.

I have a volley class that connect to mysql db with endpoint and return data login ok.

In my fragment i have a companion object with function and in this block i can show my data ok. In my onCreateView function if i try to set text to my EditText this field it´s empty and if i do a log my data it´s null. I don´t know that i´m doing wrong. I´m very novel with kotlin and android.

In MainActivity i have a login function:

val stringRequest: StringRequest = object : StringRequest(
        Method.POST, EndPoint.URL,
        Response.Listener { response ->
            try {
                val jsonObject = JSONObject(response)

                if (jsonObject != null) {
                    val dataJson: JSONObject = jsonObject.getJSONObject("user")
                    val name = dataJson.getString("name")
                    val address = dataJson.getString("address")
                    val email = dataJson.getString("email")
                    val phone = dataJson.getString("phone")
                    val password = dataJson.getString("password")

                    val intent = Intent(this, ProfileActivity::class.java)
                    // set parameter to activity
                    intent.putExtra("name", name)
                    intent.putExtra("address", address)
                    intent.putExtra("email", email)
                    intent.putExtra("phone", phone)
                    intent.putExtra("password", password)

                    startActivity(intent)
                    Toast.makeText(applicationContext, "Logged ok", Toast.LENGTH_SHORT).show()
                }
            } catch (e: JSONException) {
                e.printStackTrace()
            }
        },
        Response.ErrorListener { println("volley Error. Not connection") }) {
        @Throws(AuthFailureError::class)
        override fun getParams(): Map<String, String>? {
            val params: MutableMap<String, String> = HashMap()
            params["action"] = "login"
            params["email"] = email
            params["password"] = password
            return params
        }
    }
    VolleySingleton.instance?.addToRequestQueue(stringRequest)

This function instaciate my class volley and send all data. Init my activity ProfileActivity and send data. In my ProfileActivity i send data to my companionObject from Fragment

@SuppressLint("ResourceType")
        override fun onCreate(savedInstanceState: Bundle?) {
            super.onCreate(savedInstanceState)
            setContentView(R.layout.activity_profile)
            // pager show content
            var pager = findViewById<ViewPager2>(R.id.viewPagerProfile)
            // tabLayout it´s TabsMenu
            var tabLayout = findViewById<TabLayout>(R.id.profileMenu)
            // Personaliced Adaprter
            pager.adapter = MyAdapter(supportFragmentManager, lifecycle)

            TabLayoutMediator(tabLayout, pager) { tab, position ->
                tab.text = tabTitle[position]
            }.attach()

            val profileName = intent.getStringExtra("name")
            val profileAddress = intent.getStringExtra("address")
            val profileEmail = intent.getStringExtra("email")
            val profilePhone = intent.getStringExtra("phone")
            val profilePassword = intent.getStringExtra("password")

            if (profileName != null && profileAddress != null
                && profileEmail != null && profilePhone != null
                && profilePassword != null) {

                ProfileFragment.Companion.newInstance(profileName, profileAddress, profileEmail,
                                            profilePhone, profilePassword)
            }
        }

And in my fragment i have my companion object:

companion object {
        fun newInstance(param1:String, param2:String, param3:String, param4:String, param5:String) =
            ProfileFragment().apply {
                arguments = Bundle().apply {
                    name = param1
                    address = param2
                    userEmail = param3
                    phone = param4
                    password = param5
                }
            }
    }

In this block i can show my data, but if i tray to set text in EditText in function onCreateView it´s null.

override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle? ): View? {
        // Inflate the layout for this fragment
        val view =  inflater.inflate(R.layout.fragment_profile, container, true)
        val bundle = arguments

        editUserName = view.findViewById<EditText>(R.id.userName)
        userAddresEdit = view?.findViewById<EditText>(R.id.userAddress)
        userEmailEdit = view?.findViewById<EditText>(R.id.userEmailText)
        userPhoneEdit = view?.findViewById<EditText>(R.id.userPhone)
        userPasswordEdit = view?.findViewById<EditText>(R.id.userPasswordEdit)

        editUserName?.setText(name)
        userAddresEdit?.setText(address)
        userEmailEdit?.setText(userEmail)
        userPhoneEdit?.setText(phone)
        userPasswordEdit?.setText(password)

        Log.d(TAG, "entry: "+name)

        return view
    }

What i´m doing bad? anybody can help me?

Thanks and sorry for my bad english

0 Answers
Related