How to make parent Activity class data accessible inside OnClickListener?

42 views Asked by At

In an Android Activity class which implements a ListView, I have an OnClickListener which I can't seem to get any data from the class. The class has Strings, Arrays, JSONObjects, any of which would be helpful if only they were accessible inside the OnClickListener function. How is this done? Also sorry Stackoverflow and everyone else because I can't seem to make "Class CompanyListActivity : AppCompatActivity() {" appear inside the code block. Just pretend it is there.

Class CompanyListActivity : AppCompatActivity() {

lateinit var arrCompany: Array<Array<String?>>

arrCompany = // some valid data...

var mListView = findViewById<ListView>(R.id.listviewCompanyListCompanies)

mListView.onItemClickListener = AdapterView.OnItemClickListener { _, _, index, _ ->

        val intent = Intent (this@CompanyListActivity, CompanyActivity::class.java)
        intent.putExtra("Response", arrCompany[index])  // no data in arrCompany
        startActivity(intent)
}
1

There are 1 answers

0
Deaneaux On

Add a public Getter function to the Parent Activity class:

public fun GetCompanyArray (i: Int?): Array<String?> {
    return arrCompany[i!!] 
}

Then call it inside the OnClickListerner function:

var arrResponse = GetCompanyArray(index)