-2

I am new to firebase, so some things are still a bit confusing to me.

I have an array: var spinnerArray = arrayOf("Dumbell", "Punching Bag", "Yoga Ball", "Skipping Rope") which I then pass to a spinner. All that is working. Although, now I want to take it to the next level, which is instead of populating the spinner with the strings I added to the array, I want it to pass the data I have on my firebase database. Reason is, some items have subitems, so I need to use objects instead.

I really don't know how to go around and replace this. Could you please help me

I have already my app connected to the realtime database.

Thanks


  {
  "KitList" : {
    "kettle" : {
      "10KG" : "10KG",
      "5KG" : "5KG"
    },
    "punchBag" : "punch bag",
    "sandbag" : "sandbag"
  }
}

Someone told me I shouldn't have the value equal to the key?


This is my model

class KitList(val title: String) {

override fun toString(): String {
    return super.toString()
}}

This is the activity where the spinner is:

class NewKitListActivity : AppCompatActivity() {
var database = FirebaseDatabase.getInstance()
var myRef = database.getReference("KitList")



var spinnerArray = arrayOf("Dumbell", "Punching Bag", "Yoga Ball", "Skipping Rope")

inner class Item {
    var IdItem: Int = 0
    var Item: String? = null
    var SubItems: ArrayList<Item>? = null
}

var newKitList = mutableListOf<String>()
val selectedItems: MutableList<String>? = null

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_new_kit_list)
    var context = this;
    var addButton = addKitItemBtn

    val spinner = newKitItemSpinner
    val spinnerArrayAdapter = ArrayAdapter(this, android.R.layout.simple_spinner_dropdown_item, spinnerArray)
    //selected item will look like a spinner set from XML
    spinnerArrayAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item)
    spinner.adapter = spinnerArrayAdapter

    val kitList = newKistListView
    val listViewAdapter = ArrayAdapter(this, android.R.layout.simple_list_item_1, newKitList)
    kitList.adapter = listViewAdapter

    addButton.setOnClickListener(object : View.OnClickListener {
        override fun onClick(v: View?) {
            val selectedItem = spinner.selectedItem.toString()
            newKitList.add(selectedItem)
            listViewAdapter.notifyDataSetChanged()
        }
    })}}
  • just create a model so that data in your database can be handled. You can show some sample data with subitems and I can show you how. – Orvenito Sep 28 '17 at 10:25
  • updated post with the data. Someone told me I shouldn't have the value equal to the key? – EmanyHeather Sep 28 '17 at 10:26
  • Nothing in your code reads anything from the database yet. While I understand you're having a hard time writing the complete code, it's much easier to help if you show us how far you got. That way we can complete it and explain what you missed or where you made a mistake, instead of just providing the code as we would implement it. – Frank van Puffelen Sep 28 '17 at 13:31

2 Answers2

0

You basically have two options:

  1. Implement a SpinnerAdapter so you can pass your Firebase Objects and use that instead of an ArrayAdapter.
  2. Convert your Firebase Objects to Strings and pass these to the ArrayAdapter.
k0bin
  • 349
  • 3
  • 11
0

I have completely removed my answer yesterday and change it with this one

I have found this library from this answer

this should be the best way for parsing json in kotlin now.

you can find all the answer on the link but here are some basic steps:

  1. Install

    repositories {
    jcenter()
    }
    
    dependencies {
    compile 'com.beust:klaxon:0.30'
    }
    
  2. Let's say you have this json:

    [
    {
        "name" : "John",
        "age" : 20
    },
    {
        "name" : "Amy",
        "age" : 25
    },
    {
        "name" : "Jessica",
        "age" : 38
    }
    ]
    

We can easily collect all the ages as follows:

val array = parse("/e.json") as JsonArray<JsonObject>

val ages = array.long("age")
println("Ages: $ages")

// Prints: Ages: JsonArray(value=[20, 25, 38])
Orvenito
  • 437
  • 2
  • 14