从kotlin中的firebase的子节点检索数据

时间:2018-05-25 14:32:19

标签: android firebase firebase-realtime-database kotlin

如何在图片中的火焰基地的儿童的孩子中检索数据?我试过但得到错误

enter image description here

这是我获取数据的片段活动

    package com.fish.isyania.aplikasipemesananreparasi.fragment

import android.os.Bundle
import android.support.v4.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ListView
import android.widget.Toast

import com.fish.isyania.aplikasipemesananreparasi.R
import com.fish.isyania.aplikasipemesananreparasi.adapter.OrderAdapter
import com.fish.isyania.aplikasipemesananreparasi.model.Order
import com.google.firebase.auth.FirebaseAuth
import com.google.firebase.database.*
import kotlinx.android.synthetic.main.fragment_order_list.view.*

class OrderListFragment : Fragment() {
    lateinit var ref : DatabaseReference
    lateinit var refChild : DatabaseReference
    lateinit var orderList : MutableList<Order>
    lateinit var listView : ListView
    var user = FirebaseAuth.getInstance().currentUser
            override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
            val rootView = inflater.inflate(R.layout.fragment_order_list, container, false)
                var uid  = user!!.uid
                orderList = mutableListOf()
                ref = FirebaseDatabase.getInstance().getReference("Order")

                ref.child(uid).addValueEventListener(object : ValueEventListener {
                    override fun onCancelled(p0: DatabaseError?) {
                        Toast.makeText(context,"sorry", Toast.LENGTH_LONG ).show()
                    }

                    override fun onDataChange(p0: DataSnapshot?) {
                        if(p0!!.exists()){
                            orderList.clear()
                            for(h in p0.children){
                                val order = h.getValue(Order::class.java)
                                orderList.add(order!!)

                            }
                            val adapter = OrderAdapter(context!!, R.layout.order_row, orderList)
                            rootView.listView.adapter = adapter


                        }
                    }

                })



                return rootView
            }
    companion object {
        fun newInstance(): OrderListFragment = OrderListFragment()
    }
}

这是我的模特

package com.fish.isyania.aplikasipemesananreparasi.model

class Order (val alamat : String, val harga : Long, val jenisPemesanan : String, val keterangan : String, val nama : String,  val  namaTeknisi : String,
             val noTeleponTeknisi : String, val nomorPesanan : String,  val nomorTelepon : String, val status : String
             ) {
    constructor() : this("", 0, "","","","","","","",
                            "")
}

这是我的适配器

package com.fish.isyania.aplikasipemesananreparasi.adapter
import android.content.Context
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ArrayAdapter
import android.widget.TextView
import com.fish.isyania.aplikasipemesananreparasi.R
import com.fish.isyania.aplikasipemesananreparasi.model.Order

class OrderAdapter(val mCtx : Context, val layoutResId : Int, val orderList : List<Order>) : ArrayAdapter<Order>(mCtx, layoutResId, orderList) {

    override fun getView(position: Int, convertView: View?, parent: ViewGroup?): View {
        val layoutInflater : LayoutInflater = LayoutInflater.from(mCtx)
        val view : View = layoutInflater.inflate(layoutResId, null)
        val nomorPesanan = view.findViewById<TextView>(R.id.nomorPesanan)
        val namaPemesan = view.findViewById<TextView>(R.id.namaPemesan)
        val alamatPemesan = view.findViewById<TextView>(R.id.alamatPemesan)
        val jenisPemesanan = view.findViewById<TextView>(R.id.jenisPemesanan)
        val namaTeknisi = view.findViewById<TextView>(R.id.namaTeknisi)
        val statusPemesanan = view.findViewById<TextView>(R.id.statusPemesanan)
        var harga = view.findViewById<TextView>(R.id.hargaPemesanan)


        val order = orderList[position]
        var hargaPemesanan = order.harga

        nomorPesanan.text = "Nomor Pesanan: " + order.nomorPesanan
        namaPemesan.text = "Nama Pemesan: " + order.nama
        alamatPemesan.text = "Alamat Pemesan: " + order.alamat
        jenisPemesanan.text = "Jenis Pemesanan: " +order.jenisPemesanan
        namaTeknisi.text = "Nama Teknisi: " + order.namaTeknisi
        statusPemesanan.text ="Status Pemesanan" + order.status
        harga.text = "Harga: " + hargaPemesanan.toString() + "(belum termasuk biaya pergantian suku cadang bila diperlukan)"
        return view
    }
}

如何从照片中的firebase儿童的孩子那里得到数据?

1 个答案:

答案 0 :(得分:0)

我认为您应该:

//将此添加到您的lateinit变量中     lateinit var orderList:MutableList

val generatedID = //the id thats showing your DB
val datechild = //the date and hour child thats showing on your DB
ref = FirebaseDatabase.getInstance().getReference().child(generatedID).child(datechild)







   ref.addValueEventListener(object : ValueEventListener {

        override fun onCancelled(p0: DatabaseError) {
            // handle errors for log
               Log.e("DB ERROR", "-------> " + p0)
        }

        override fun onDataChange(p0: DataSnapshot) {
                if(p0.exists()){
                  orderList.clear()
                for (p in p0.children){
                    val plc = p.getValue(Order::class.java)
                    orderList.add(plc!!)
                    Log.d("adding", "Adding data to list DONE: " + p.getValue(Order::class.java))
                }
                for (p in List){
                    //do what you need
                    //example
                }
            }
        }
    })