CFragment.kt 3.08 KB
package com.example.vip


import android.os.Bundle
import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.core.content.ContextCompat
import com.google.firebase.database.DataSnapshot
import com.google.firebase.database.DatabaseError
import com.google.firebase.database.FirebaseDatabase
import com.google.firebase.database.ValueEventListener
import kotlinx.android.synthetic.main.fragment_a.*

class CFragment : Fragment() {

    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_a, container, false)
    }

    override fun onActivityCreated(savedInstanceState: Bundle?) {
        super.onActivityCreated(savedInstanceState)
        val policyList = ArrayList<PolicyItem>()
        var recomdatabase = FirebaseDatabase.getInstance().reference

        recomdatabase.orderByChild("View").addListenerForSingleValueEvent(object :
            ValueEventListener {
            override fun onCancelled(p0: DatabaseError) {
                //Toast.makeText(this@RecommendActivity, "실패부분", Toast.LENGTH_SHORT).show()
            }
            override fun onDataChange(dataSnapshot: DataSnapshot) {
                for (memoSnapshot in dataSnapshot.children){
                    val memo = memoSnapshot.getValue(MemoItemRecom::class.java)

                    policyList.add(
                        PolicyItem(
                            ContextCompat.getDrawable(context!!.applicationContext, R.drawable.recommend_default_blue)!!,
                            memo!!.Policy,
                            memo!!.Content,
                            memo!!.Link,
                            memo!!.View,
                            memo!!.Keyword,
                            memo!!.Date,
                            memo!!.Review,
                            memo!!.Score,
                            memo!!.Category1,
                            memo!!.Category2,
                            memo!!.Category3,
                            memo!!.Category4,
                            memo!!.Category5,
                            memo!!.Category6,
                            memo!!.Category7,
                            memo!!.Category8,
                            memo!!.Category9,
                            memo!!.Category10,
                            memo!!.Category11,
                            memo!!.Category12,
                            memo!!.Category13,
                            memo!!.Category14,
                            memo!!.Category15,
                            memo!!.Category16,
                            memo!!.Value,
                            memo!!.D_day
                        )
                    )
                }
                val adapter = PolicyAdapter(policyList)
                policyRecyclerView.adapter = adapter
                policyRecyclerView.setItemViewCacheSize((200000000))
            }
        })
    }
}