2017-06-28 57 views
0

據我所知,Firebase是異步獲取數據的,我試圖在數據加載到我的recyclerView適配器之前設置數據,以便它顯示在recyclerview中。但是,當我啓動應用程序,它只是變得我最初設定的「空」的字符串,好像火力甚至沒有要到onDataChange方法Firebase沒有從onDataChange方法獲取數據

CoursesFragment.kt

class CoursesFragment : android.support.v4.app.Fragment(), Contract.View { 
    val TAG: String = CoursesFragment::class.java.name 
    val presenter: Contract.Presenter.CoursePresenter = CoursePresenter(this) 
    var tempDataList: MutableList<String> = mutableListOf() 
    var dataArray: Array<String> = arrayOf("EMPTY") 

    override fun onCreate(savedInstanceState: Bundle?) { 
     super.onCreate(savedInstanceState) 
     tempDataList = presenter.retrieveCourseNames() 
     for (i in 0 until tempDataList.size) dataArray[i] = tempDataList[i] 
    } 

    override fun onCreateView(inflater: android.view.LayoutInflater?, container: android.view.ViewGroup?, 
           savedInstanceState: Bundle?): android.view.View? { 
     // Inflate the layout for this fragment 
     val v = inflater!!.inflate(R.layout.fragment_courses, container, false) 

//  Gets cardview widget and sets property 
     val rView = v.findViewById<RecyclerView>(R.id.recyclerView_courses) 
     rView.setHasFixedSize(true) 

//  Creates the 2 column recycler view with the cards 
     val layoutMan = GridLayoutManager(context, 1) 
     rView.layoutManager = layoutMan 
     //  Sets up cardview in recycler view 

     val adapter = CourseRecyclerAdapter(dataArray, presenter) 
     adapter.notifyDataSetChanged() 
     rView.adapter = adapter 

//  actually inflates view 
     return v 
    } 

    // ======================== INTERFACE OVERRIDE METHODS ======================== 

    // CourseContract 
    override fun showToastMessage(message: String) { 
     Toast.makeText(context, message, Toast.LENGTH_SHORT).show() 
    } 

    // ======================== USER METHODS ======================== 
// New Instance Method 
    companion object { 
     fun newInstance(page: Int = 0): CoursesFragment { 
      val argPage: String = "ARG_PAGE" 
      val args: Bundle = Bundle() 
      args.putInt(argPage, page) 
      val fragment = CoursesFragment() 
      fragment.arguments = args 
      return fragment 
     } 
    } 
} 

CoursePresenter.kt

class CoursePresenter(val courseView: Contract.View) : Contract.Presenter.CoursePresenter { 
    val courseInstance = com.alexoladele.cvap.MVP.Course.Model.Course() 
    override fun retrieveCourseNames(): MutableList<String> { 
     val returnList: MutableList<String> = mutableListOf() 
     courseInstance.getCourseNamesFromDB(object : MyCallBack{ 
      override fun doCallback(dbValue: String) { 
       returnList.add(dbValue) 
      } 
     }) 
     return returnList 
    } 

    fun testToast() { 
     courseView.showToastMessage("TOAST TEST") 
    } 
    override fun onCourseClick(cardPosition: Int) { 
     testToast() 
    } 
} 

Course.kt

class Course (val name: String = "GENERIC", val content: String = "PLACEHOLDER") : Contract.Model.CourseModel { 
    val dbReference: DatabaseReference = FirebaseDatabase.getInstance().reference 
    val courseRef = dbReference.child("Course") 
    val courseNames = mutableListOf<String>() 

    override fun getCourseNamesFromDB(callback: MyCallBack) { 
     courseRef.addValueEventListener(object : ValueEventListener { 
      override fun onCancelled(p0: DatabaseError?) { 
//    TODO("not implemented") //To change body of created functions use File | Settings | File Templates. 
      } 

      override fun onDataChange(p0: DataSnapshot) { 
       val tempString = p0.value as String 
       Log.d(TAG, "onDataChange: Value from DB is: $tempString"); 

       callback.doCallback(tempString) 
      } 
     }) 
    } 
} 
interface MyCallBack { 
    fun doCallback(dbValue: String) 
} 

編輯:我在CoursePresenter試過這種

override fun retrieveCourseNames(dataArray: Array<String>): Array<String> { 
//  val returnList: MutableList<String> = mutableListOf() 
     courseInstance.getCourseNamesFromDB(object : MyCallBack{ 
      override fun doCallback(dbValue: String) { 
       val returnList = mutableListOf<String>() 
       returnList.add(dbValue) 
       for (i in 0 until returnList.size) 
        dataArray[i] = returnList[i] 
      } 
     }) 

     return dataArray 
    } 

回答

0

爲了解決這個問題,需要申報和你CoursePresenter.kt文件中使用val returnList的重寫doCallback函數內。這樣就不會得到空的String。

希望它有幫助。

+0

那麼如何返回該函數中的returnList? –

+0

無論你想用它做什麼,只要在那裏使用它,在那個函數中。 –

+0

對不起,我只是想澄清。所以,returnList會返回數據庫中mutableList的項目,傳遞給CourseFragment中的dataArray傳遞給適配器。你是說我應該把它傳遞給這個函數中的dataArray變量? –

0

我通過完全消除回調來修復它!我在onDataChange()方法中設置了recyclerView適配器,因此在獲取數據之前我沒有設置佈局,這可能會導致它加載速度有點慢,可能違反了MVP,但它工作正常!