'Notify data change on spinner select
I can't seem to get my driver to update through my app. I have tried but with no luck. I have a possible solution but don't know what the adapter is that needs to be used. This data all happens in my MainActivity
. When the new spinner item is selected. I pass the driver through to do new calculations with the correct DRIVER
data.
My spinner class:
private fun Spinner( tripsheetlist: ArrayList<DataModel>): String {
var driverlist = tripsheetlist.distinctBy { it.DRIVER }
var driver : String = driverlist[1].DRIVER //this will be the default first picked driver
spnDriver.onItemSelectedListener = object : AdapterView.OnItemSelectedListener{
override fun onItemSelected(p0: AdapterView<*>?, p1: View?, p2: Int, p3: Long) {
driver = driverlist[p2].DRIVER.toString()
Toast.makeText(this@MainActivity, "Driver $driver selected", Toast.LENGTH_SHORT).show()
myAdapter.notifyItemChanged(p2);
// myAdapter.notifyDataSetChanged()
}
override fun onNothingSelected(p0: AdapterView<*>?) {
}
}
return driver
}
Calling class:
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
recyclerViewTripsheetlist.layoutManager = LinearLayoutManager(this)
val spnDriver: Spinner = findViewById(R.id.spnDriver)
var adapter = ArrayAdapter<String>(this, androidx.appcompat.R.layout.support_simple_spinner_dropdown_item)
conn(adapter, spnDriver)
}
private fun conn(adapter: ArrayAdapter<String>, spnDriver: Spinner) {
var tripsheetlist = ArrayList<DataModel>()
//populated by sql - removed for clarity
populatespinner(tripsheetlist, adapter)
var driver : String = Spinner(tripsheetlist)
var driverList : ArrayList<DataModel> = datafilter(tripsheetlist, driver)
tripsheetlist = driverList
weightsum(tvTotalweight, tripsheetlist)
totaldelNotes(tvTotaldelv,tripsheetlist)
runOnUiThread {
recyclerViewTripsheetlist.adapter = TableViewAdapter(tripsheetlist, driver, tvHeader, adapter)
}
}
Thank you for all and any help.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|