Problema al enviar parámetros a fragment

  • Respuestas:0
scorpions
  • Posts del Foro: 1

22 jul. 2022 12:48:10 vía Web

Buenas tardes.

Tengo un problema a la hora de pasar mis variables obtenidas mediante una peticion volley a un webservices con php para hacer un login.

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

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

    val editTextName = findViewById<EditText>(R.id.userName)
    val editTextEmail = findViewById<EditText>(R.id.userEmail)
    val editTextphone = findViewById<EditText>(R.id.userPhone)
    val editTextPassword = findViewById<EditText>(R.id.userPassword)

    val b = intent.extras
    var name: String
    var address: String
    var userEmail: String
    var phone: String
    var password: String

    if (b != null) {
        name = intent.getStringExtra("name").toString()
        address = intent.getStringExtra("address").toString()
        userEmail = intent.getStringExtra("email").toString()
        phone = intent.getStringExtra("phone").toString()
        password = intent.getStringExtra("password").toString()

        Log.i(TAG, "name: "+name)
        Log.i(TAG, "userEmail: "+userEmail)
        Log.i(TAG, "phone: "+phone)
        Log.i(TAG, "pass: "+password)

        ProfileFragment.newInstance(name, address, userEmail, phone, password)

De esta manera envío mis variables y compruebo en el log que recibo los valores y así es. Pero en el fragment para setear los valores en los inputs, estoy haciendo esto:

class ProfileFragment() : Fragment() {
var name: String? = null
var address: String? = null
var userEmail: String? = null
var phone: String? = null
var password: String? = null
var bundle: Bundle? = null
var editUserName: EditText? = null
var userAddresEdit: EditText? = null
var userEmailEdit: EditText? = null
var userPhoneEdit: EditText? = null
var userPasswordEdit: EditText? = 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, false)
    val bundle = this.arguments
    return view
}

companion object {

    @JvmStatic
    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
            }

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

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

}

También estoy recibiendo los valores, pero si ejecuto la app, en los inputs no veo el valor que recibo, pero en el log si.

Gracias por la ayuda y por leerme.

Un saludo

Contesta el/la primero/a