0

Im trying to get a method that gets data from firebase to finish executing before the rest of my code is run so that it can add the data to a spinner.

at the moment i'm using a call back but it seems that this only prints the data when all the data is fetched

this is my code and i've added some println()s to see the order of how the code is running:

class FollowingFragment: Fragment() {

private val database = FirebaseDatabase.getInstance()
private val mAuth = FirebaseAuth.getInstance()
private var currentUid = mAuth.currentUser?.uid
val sharedPreferences = activity?.getSharedPreferences("savedCategories", Context.MODE_PRIVATE)

val categories: ArrayList<String> = ArrayList()
val test: ArrayList<String> = ArrayList()

lateinit var spinner : Spinner

override fun onCreateView(
    inflater: LayoutInflater,
    container: ViewGroup?,
    savedInstanceState: Bundle?
) =
    inflater.inflate(R.layout.fragment_following, container, false)!!

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
    super.onViewCreated(view, savedInstanceState)

        println("Order 1")
    fetchUserFollowings(object:FollowingCallback {
        override fun onCallback(value: ArrayList<String>) {
            println("Order 2")
            test.addAll(categories)
            println(test)
            println("Order 3")
        }
    })
        println("Order 4")


    //Bind views
    val list = view.findViewById<RecyclerView>(R.id.followingRecyclerView)

    //create layout manager
    val layoutManager = LinearLayoutManager(context)
    list.layoutManager = layoutManager

    spinner = view.findViewById(R.id.sp_option) as Spinner
    //Set Dropdown list
        println("Order 5")
        val adapter = ArrayAdapter<String>(view.context, android.R.layout.simple_list_item_1, test)
        println("Order 6")

    spinner.adapter = adapter

    spinner.onItemSelectedListener = object : AdapterView.OnItemSelectedListener {
        override fun onItemSelected(
            parent: AdapterView<*>?,
            view: View?,
            position: Int,
            id: Long
        ) {
            //fetchNews(categories[position])
            //Log.e("CATEGORY", "${categories[position]}")
        }
        override fun onNothingSelected(parent: AdapterView<*>?) {
            TODO("Not yet implemented")
        }
    }
}

private fun fetchNews(category: String) {

    val c = Calendar.getInstance()

    val year = c.get(Calendar.YEAR)
    val month = c.get(Calendar.MONTH)
    val day = c.get(Calendar.DAY_OF_MONTH)
    val date = "$year-${month+1}-$day"
    val dateBefore = "$year-${month+1}-${day-1}"

    val url = "http://newsapi.org/v2/everything?q=$category&from=$dateBefore&to=$date&sortBy=popularity&apiKey=${Constants.API_KEY}"

    val request = Request.Builder().url(url).build()

    val client = OkHttpClient()
    client.newCall(request).enqueue(object : Callback {

        override fun onResponse(call: Call, response: Response) {
            val body = response?.body?.string()
            val gson = GsonBuilder().create()
            val homeFeed = gson.fromJson(body, HomeFeed::class.java)

            activity?.runOnUiThread {
                followingRecyclerView.adapter = MyAdapter(homeFeed)
            }
        }

        override fun onFailure(call: Call, e: IOException) {
            println("Failed to execute request")
        }
    })
}

interface FollowingCallback {
    fun onCallback(value:ArrayList<String>)
}

private fun fetchUserFollowings(myCallback:FollowingCallback) {

    val myRef = database.getReference("User-following").child(currentUid!!)

    myRef.addValueEventListener(object : ValueEventListener {
        override fun onDataChange(dataSnapshot: DataSnapshot) {
            categories.clear()
            for (childSnapshot in dataSnapshot.children) {
                if (childSnapshot.hasChildren()) {
                    var test = childSnapshot.child("category").value
                    categories.add(test as String)
                }
            }
            myCallback.onCallback(categories)
        }

        override fun onCancelled(error: DatabaseError) {
            Log.e("Database Error", error.toString())
        }
    })
}
}

this is how it prints out:

Order 1
Order 4
Order 5
Order 6
Order 2
["List of from firebase"]
Order 3

but i need the "list from firebase" before order 4,5,6 are run as i need to use this list to populate the spinner

how can i fix this?

es915
  • 137
  • 3
  • 11

0 Answers0