Solution 1 :
Override the toString method in the model class that you are trying to put in your adapter.
In your com.example.newsreader.model.PreferenceM, do the following:
override fun toString(): String{
return <the field you want to put in your adapter>
}
Problem :
I Have a local database (Sqlite) that im trying to get all the data from one column and populate that into a Spinner inside a fragment. I’m trying to get the data from db and insert them to an array so it will be easy, Can you guys help me with this? A code sample will be much helpful. I’ve somehow done this but it’s not showing the String value but instead show the reference value like “[email protected]”… I dunno how to convert this into a readable String.
Relevant Code Snippet
private fun preferencesInit(){
//load the preferences list
val prefList = dbHandler.getAllPrefernces(activity!!.applicationContext)
val spinner = spinner_preferences
val preferences = prefList.toTypedArray()//arrayOfNulls<String>(prefList.size)
// prefList.toArray()
val arrayAdapter = ArrayAdapter(activity!!.applicationContext, android.R.layout.simple_spinner_dropdown_item, preferences)
spinner.adapter = arrayAdapter
// spinner.setOnClickListener {
spinner.onItemSelectedListener = object : AdapterView.OnItemSelectedListener {
override fun onItemSelected(
parent: AdapterView<*>,
view: View,
position: Int,
id: Long
) {
if (preferences.isEmpty() || preferences.size == 0){
//do nothing
}else{
Toast.makeText(
activity!!.applicationContext,"preference selected : " + preferences[position],Toast.LENGTH_SHORT).show()
checkQueryText(preferences[position].toString())
}
}
override fun onNothingSelected(parent: AdapterView<*>) {
// Code to perform some action when nothing is selected
queryTopHeadlines()
}
}
// }
}