How to take a variable from another class in Kotlin?

Asked

Viewed 288 times

1

I want to take a variable from one activity and use it in another class. This variable will be filled by a user in an editText called editTextSerie

 override fun searchSeries(listener: OnDataListener) {

        val retrofit = Retrofit.Builder().addConverterFactory(GsonConverterFactory.create())
            .baseUrl("http://api.themoviedb.org/3/")
            .build()

        val client = retrofit.create(MovieDBApiInterface::class.java)

        val objetoClasse1 = SearchActivity()

        var nomeS = objetoClasse1.editTextSerie.text.toString().trim()


        val responseCall = client.searchSeries("API_KEY", "pt-BR", nomeS)

        responseCall.enqueue(object : Callback<AllSeriesResponse> {
            override fun onResponse(call: Call<AllSeriesResponse>?, response1: Response<AllSeriesResponse>?) {
                listener.onSuccess(response1!!.body()!!.results)
            }

            override fun onFailure(call: Call<AllSeriesResponse>?, t: Throwable?) {
                listener.onFailure(t!!.message.toString())
            }
        })
    }

This "searchSeries" function is from the "Series" class".

I want to take the "editTextSerie" of another class called "Search Activity", so I created the variable "names" to receive the value of it.

class SearchActivity : AppCompatActivity() {

    var botaoSearch: AppCompatImageButton? = null


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



        botaoSearch = findViewById(R.id.btn_search)

        botaoSearch?.setOnClickListener {


          var nomeSerie = editTextSerie.text.toString().trim()


        }
    }
}

I want to receive this value (value of editTextSerie comes from the XML of SearchActivity) and use it in responseCall with the variable "names"

  • Places the complete code of the classes

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.