片段未接收到参数集

时间:2018-11-04 13:30:20

标签: android android-fragments kotlin

我正在尝试将数据传递到活动的片段中。但是,该片段似乎根本没有在接收数据。

这是我捆绑数据的地方:

        R.id.nav_chart -> {
            val currentUser = mAuth!!.currentUser
            if (currentUser != null) {
                val completed : List<String> = getProgress()

                var basics = 0
                var loops = 0
                var functions = 0

                completed.forEach {
                    if (it == "Data Type" || it == "Operation" || it == "Condition" || it == "Null Safety") {
                        basics++
                    } else if (it == "For" || it == "Break" || it == "Repeat" || it == "When" || it == "While") {
                        loops++
                    } else if (it == "Call" || it == "Main" || it == "Recursion" || it == "Single Expression") {
                        functions++
                    }
                }

                val bundle = Bundle()
                bundle.putDouble("basics",basics.toDouble())
                bundle.putDouble("loops",loops.toDouble())
                bundle.putDouble("functions",functions.toDouble())
                chartFragment(bundle)

            }

在这里加载片段:

fun chartFragment(bundle: Bundle){
    this.setTitle(R.string.navigation_drawer_chart)
    val fragmentManager = supportFragmentManager
    val transaction = fragmentManager.beginTransaction()
    val fragment = ChartFragment()
    fragment.setArguments(bundle)
    transaction.replace(R.id.frameLayout1, fragment)
    transaction.commit()
}

这是片段的onCreate视图:

override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?,
                          savedInstanceState: Bundle?): View? {

    val view = inflater.inflate(R.layout.fragment_chart, container, false)
    val graph = view?.findViewById(R.id.graph) as GraphView
    val basics = arguments!!.getDouble("basics")
    val loops = arguments!!.getDouble("loops")
    val functions  = arguments!!.getDouble("functions")

    val series = BarGraphSeries(arrayOf(
            DataPoint(1.0, basics),
            DataPoint(2.0, loops),
            DataPoint(3.0, functions)
    ))

    graph.addSeries(series)
    series.spacing = 5
    series.setValuesOnTopColor(R.color.green)

    return view
}

在这里,我可以将数据打包:

/**
 * For firebase database
 * Read user progress from db
 */
private fun getProgress() : List<String> {
    val uid = FirebaseAuth.getInstance().getCurrentUser()?.uid
    val completed = mutableListOf<String>()

    val rootRef = FirebaseDatabase.getInstance().getReference()
    val progressRef = rootRef.child("Users").child(uid!!).child("Progress")
    val valueEventListener = object : ValueEventListener {
        override fun onDataChange(dataSnapshot:DataSnapshot) {
            for(dSnapshot : DataSnapshot in dataSnapshot.getChildren()) {
                for(ds : DataSnapshot in dSnapshot.getChildren()) {
                    val key = ds.getKey() as String
                    completed.add(key)
                    Log.d(TAG2, key)
                }
            }
        }

        override fun onCancelled(@NonNull databaseError : DatabaseError) {
            Log.d(TAG2, databaseError.getMessage())
        }
    }
    progressRef.addListenerForSingleValueEvent(valueEventListener)
    return completed
}

我猜我捆绑数据的方式有错误,或者某些排序错误。当执行getProgress()时,我的logcat会显示肯定有从数据库返回的数据:

enter image description here

2 个答案:

答案 0 :(得分:0)

enter code here

代替completed?
completed.forEach {

}

因为结果不可为空。


或者尝试使用putParcelableArrayList并将收到的结果转换为ArrayList

val completed: ArrayList<String> = arguments?.getgetParcelableArrayList("completed") as ArrayList<String>

答案 1 :(得分:0)

Const intForReading = 1 Const intForWriting = 2 Const intForAppending = 8 strQueryFile = "C:\numbers_test.txt" strDataSetFile = "C:\data_test.csv" strOutputFile = "C:\results_test.csv" Set objFSO = CreateObject("Scripting.FileSystemObject") Set objFileToRead = objFSO.OpenTextFile(strQueryFile, intForReading) Set objFileToQuery = objFSO.OpenTextFile(strDataSetFile, intForReading) Set objFileToWrite = objFSO.OpenTextFile(strOutputFile, intForWriting, True) Do Until objFileToQuery.AtEndOfStream Do Until objFileToRead.AtEndOfStream strNumber = objFileToRead.ReadLine() WScript.Echo strNumber strLine = objFileToQuery.ReadLine() If InStr(strLine,strNumber) > 0 Then strFoundText = strLine objFileToWrite.WriteLine(strFoundText) Loop Loop objFileToQuery.Close objFileToRead.Close objFileToWrite.Close 下将val completed = mutableListOf<String>()全局而不是在函数内部解决后,解决了该问题。