Solucionado (ver solução)
Solucionado
(ver solução)
1
resposta

Nao pega valores digitados com toString, apenas com text

To tentando pegar dados de um EditText e salvar em um objeto que espera uma String, se eu usar o xy.text.toString() não salva nada. Se usar apenas o xy.text da erro: Type mismatch. Required: String Found: Editable! Mesmo usando um log.i só consigo mostra o valor digitado se for apenas o xy.text, se coloco toString() da erro. Alguma ideia de como posso fazer essa conversão?

package com.adilson.orgsprojeto.ui.activity

class CadastroFreela : AppCompatActivity() {

    private lateinit var textName: EditText
    private lateinit var textDate: EditText
    private lateinit var btn_salvar: Button

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


        textName = findViewById<EditText>(R.id.editTextFotografo)
        textDate = findViewById(R.id.editTextDate)
        btn_salvar = findViewById<Button>(R.id.buttonSalvarFreela);


        val nome = textName.text.toString()
        val date: String  = textDate.text.toString()


      val freela = Freelas(name = nome, date = date )


        btn_salvar.setOnClickListener {
//            startActivity(Intent(this, MainActivity::class.java))
            Log.i("TesteGetNome", "onCreat: $freela")
        }

    }
}

resultado do log Cast: onCreat: Freelas(name=, date=)

1 resposta
solução!

Resolvido!

Tinha que fazer a conversão dentro do listem. Ficou Assim:

package com.adilson.projetoFreelances.ui.activity

import android.os.Bundle
import android.widget.Button
import android.widget.EditText
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import com.adilson.projetoFreelances.DAO.FreelasDAO
import com.adilson.projetoFreelances.R
import com.adilson.projetoFreelances.model.Freelas


class CadastroFreela : AppCompatActivity(R.layout.activity_cadastro_freela) {

    private lateinit var textName: String
    private lateinit var textDate: String
    private lateinit var textNoivos: String
    private lateinit var textCelular: String
    private lateinit var textHora: String
    private lateinit var textLocal: String
    private lateinit var btn_salvar: Button

    private val dao = FreelasDAO()


    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        linksTheEditTextAndButton()
        configSaveButton()

    }


    private fun configSaveButton() {

        btn_salvar.setOnClickListener {
            linksTheEditTextAndButton()

            if (textDate.isBlank() && textName.isBlank()){
                Toast.makeText(this, "Nome e Data Sao OBRIGATORIOS", Toast.LENGTH_SHORT).show()
            } else{
                salvarDadosNoDAO()
                finish()
            }
        }
    }

    private fun salvarDadosNoDAO() {

        val freela = Freelas(
            date = textDate,
            horas =  textHora,
            nomeFotografo = textName,
            celular = textCelular,
            noivos =  textNoivos,
            local = textLocal
        )
        dao.add(freela)
    }

    private fun linksTheEditTextAndButton() {

        textName = findViewById<EditText>(R.id.editTextFotografo).text.toString()

        textDate = findViewById<EditText>(R.id.editTextDate).text.toString()

        textNoivos = findViewById<EditText>(R.id.editTextNomeNoivos).text.toString()

        textCelular = findViewById<EditText>(R.id.editTextPhone).text.toString()

        textHora = findViewById<EditText>(R.id.editTextTime).text.toString()

        textLocal = findViewById<EditText>(R.id.editTextLocal).text.toString()

        btn_salvar = findViewById<Button>(R.id.buttonSalvarFreela)

    }
}

Quer mergulhar em tecnologia e aprendizagem?

Receba a newsletter que o nosso CEO escreve pessoalmente, com insights do mercado de trabalho, ciência e desenvolvimento de software