Skip to content

Snappy1

  • Home
  • Android
  • What
  • How
  • Is
  • Can
  • Does
  • Do
  • Why
  • Are
  • Who
  • Toggle search form

[FIXED] android – Populate Spinner Data from sqliteDB in Kotlin

Posted on November 11, 2022 By

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()
                }
            }
//            }
}
READ  [FIXED] android - how to set menu item title in java File?
Powered by Inline Related Posts
Android Tags:android, arrays, kotlin, spinner, sqlite

Post navigation

Previous Post: [FIXED] android – Build not succed in flutter
Next Post: [FIXED] kotlin – Opening the application from the results google.com, always directs to the home page of WebView Android

Related Posts

[FIXED] flutter doctor –android-licenses gives a java error Android
[FIXED] android – How to use a Firebase auth listener in a repository class MVVM? Android
[FIXED] java – Is it possible to setArguments() on onActivityResult() and pass it to Fragment? Android
[FIXED] xamarin.android – Recognising touch events on Xamarin Android and ‘dropping event due to no window focus’ Android
[FIXED] kotlin – Is it possible to get a recyclerview/listview to navigate to different screens? Android
[FIXED] java – Random Walker (circle) move up much more frequent than other axis Android

Archives

  • March 2023
  • February 2023
  • January 2023
  • December 2022
  • November 2022
  • October 2022
  • September 2022

Categories

  • ¿Cómo
  • ¿Cuál
  • ¿Cuándo
  • ¿Cuántas
  • ¿Cuánto
  • ¿Qué
  • Android
  • Are
  • At
  • C'est
  • Can
  • Comment
  • Did
  • Do
  • Does
  • Est-ce
  • Est-il
  • For
  • Has
  • Hat
  • How
  • In
  • Is
  • Ist
  • Kann
  • Où
  • Pourquoi
  • Quand
  • Quel
  • Quelle
  • Quelles
  • Quels
  • Qui
  • Should
  • Sind
  • Sollte
  • Uncategorized
  • Wann
  • Warum
  • Was
  • Welche
  • Welchen
  • Welcher
  • Welches
  • Were
  • What
  • What's
  • When
  • Where
  • Which
  • Who
  • Who's
  • Why
  • Wie
  • Will
  • Wird
  • Wo
  • Woher
  • you can create a selvedge edge: You can make the edges of garter stitch more smooth by slipping the first stitch of every row.2022-02-04
  • you really only need to know two patterns: garter stitch

Recent Posts

  • What is a good substitute for condensed milk?
  • ¿Como el cuerpo te avisa cuando te va a dar un infarto?
  • What is the luxury brand of Jaguar?
  • Who is Big poppa baseball player?
  • What material are foam runners?

Recent Comments

No comments to show.

Copyright © 2023 Snappy1.

Powered by PressBook Grid Dark theme