Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/201.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/variables/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 为什么我在Kotlin片段中得到一个空的arraylist?_Android_Variables_Android Fragments_Kotlin_Arraylist - Fatal编程技术网

Android 为什么我在Kotlin片段中得到一个空的arraylist?

Android 为什么我在Kotlin片段中得到一个空的arraylist?,android,variables,android-fragments,kotlin,arraylist,Android,Variables,Android Fragments,Kotlin,Arraylist,我有一个片段和一个数组列表,我试图在函数中向这个数组列表添加值。当我在函数中打印它时,它就工作了。但是,当我试图在函数数组列表之外打印它时,它将变为空 我可以看出我在这里遗漏了一个逻辑 谁能帮我一下我哪里做错了 这是片段文件 class HourlySalesFragment : Fragment() { val tt = arrayListOf<Double>() override fun onCreateView(inflater: LayoutInflater, conta

我有一个片段和一个数组列表,我试图在函数中向这个数组列表添加值。当我在函数中打印它时,它就工作了。但是,当我试图在函数数组列表之外打印它时,它将变为空

我可以看出我在这里遗漏了一个逻辑

谁能帮我一下我哪里做错了

这是片段文件

class HourlySalesFragment : Fragment() {

val tt = arrayListOf<Double>()

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

 //some other code here.

 val valueEventListener08_30 = object : ValueEventListener {


        var subtotalList = arrayListOf<Double>()


        override fun onDataChange(p0: DataSnapshot) {
            if (p0.exists()) {
                for(ds in p0.children) {
                    println("data snapshot "+ds)

                    val subtotal = ds.child("subTotal").getValue(Double::class.java)!!

                    subtotalList.add(subtotal)
                    textSubTotal8_30.text = subtotalList.sum().toString()

                }

                tt.add(subtotalList.sum())
                println("ttt 8:30 "+tt)
            }
        }

        override fun onCancelled(databaseError: DatabaseError) {
            Log.d(TAG, databaseError.getMessage()) //Don't ignore errors!
        }
    }
    println("ttt outside"+tt)
提前感谢。

因为println(“ttt-outside”+tt)语句是在异步方法完成执行之前首先执行的

val valueEventListener08_30 = object : ValueEventListener {


        var subtotalList = arrayListOf<Double>()


        override fun onDataChange(p0: DataSnapshot) {
            if (p0.exists()) {
                for(ds in p0.children) {
                    println("data snapshot "+ds)

                    val subtotal = ds.child("subTotal").getValue(Double::class.java)!!

                    subtotalList.add(subtotal)
                    textSubTotal8_30.text = subtotalList.sum().toString()

                }

                tt.add(subtotalList.sum())
                println("ttt 8:30 "+tt)
                useArray()
            }
        }

        override fun onCancelled(databaseError: DatabaseError) {
            Log.d(TAG, databaseError.getMessage()) //Don't ignore errors!
        }
    }




   fun useArray(){
        println("ttt outside"+tt)

    }
val valueEventListener08\u 30=对象:ValueEventListener{
var subtotalList=arrayListOf()
覆盖数据更改(p0:DataSnapshot){
if(p0.exists()){
用于(p0.05儿童中的ds){
println(“数据快照”+ds)
val subtotal=ds.child(“subtotal”).getValue(Double::class.java)!!
小计列表.添加(小计)
textSubTotal8_30.text=subtotalList.sum().toString()
}
tt.add(subtotalList.sum())
println(“ttt 8:30”+tt)
useArray()
}
}
覆盖已取消(databaseError:databaseError){
Log.d(标记,databaseError.getMessage())//不要忽略错误!
}
}
fun useArray(){
println(“ttt外部”+tt)
}

我明白了,谢谢你的回复。有什么方法可以获取这些
tt
值吗?是的,只有在onDataChange()方法完成其执行后,即该方法的最后一条语句后,才能使用该数组列表值。谢谢,如果我想在
ValueEventListener
之外获取这些值如何?如果您想在ValueEventListener之外的其他方法中使用该数组,那么您必须在异步方法完成其执行后调用该方法。这就是异步方法的工作原理,否则您将无法获得更新的数据,请参阅我的更新答案。谢谢Nehal。但现在的问题是它的付出<代码>未解析引用:useArray我已将
useArray()
函数移动到
valueeventlistener
之前,现在它可以工作了。非常感谢。在
onDataChange
回调之前调用外部打印,因此此时
tt
为空。您想实现什么?我想在
onDataChange
内部填充一个arraylist,并在
onDataChange
外部获取这些值,以便将其用于其他用途。
val valueEventListener08_30 = object : ValueEventListener {


        var subtotalList = arrayListOf<Double>()


        override fun onDataChange(p0: DataSnapshot) {
            if (p0.exists()) {
                for(ds in p0.children) {
                    println("data snapshot "+ds)

                    val subtotal = ds.child("subTotal").getValue(Double::class.java)!!

                    subtotalList.add(subtotal)
                    textSubTotal8_30.text = subtotalList.sum().toString()

                }

                tt.add(subtotalList.sum())
                println("ttt 8:30 "+tt)
                useArray()
            }
        }

        override fun onCancelled(databaseError: DatabaseError) {
            Log.d(TAG, databaseError.getMessage()) //Don't ignore errors!
        }
    }




   fun useArray(){
        println("ttt outside"+tt)

    }