Мой RecyclerView не показывает ничего - PullRequest
0 голосов
/ 02 октября 2019

Я использую MVVM с постоянством комнаты и liveata. Я получаю данные из локальной базы данных и хочу показать их в виде списка в recyclerView, а мой recyclerView ничего не показывает. Мой адаптер похож на любой другой обычный адаптер Код RecyclerView

class MyInformationAdapter : RecyclerView.Adapter<MyInformationAdapter.ViewHolder>() {

    private var myList: List<PersonInformation> = ArrayList()

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
        val layoutInflater = LayoutInflater.from(parent.context)
        val view = layoutInflater.inflate(R.layout.my_adapter_data, parent, false)
        return ViewHolder(view)
    }

    override fun getItemCount(): Int = myList.size

    override fun onBindViewHolder(holder: ViewHolder, position: Int) {
        var myDataClass: PersonInformation = myList[position]
        holder.name.text = myDataClass.name
        holder.fName.text = myDataClass.fatherName
        holder.email.text = myDataClass.email
        holder.contact.text = myDataClass.contactNo.toString()
    }

    fun updateTheState(myList: List<PersonInformation>){
        this.myList = myList
        notifyDataSetChanged()
    }

    inner class ViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {

        var name: TextView = itemView.findViewById(R.id.yourName)
        var fName: TextView = itemView.findViewById(R.id.yourFatherName)
        var email: TextView = itemView.findViewById(R.id.yourEmail)
        var contact: TextView = itemView.findViewById(R.id.yourContact)


    }
}

RecyclerView Код активности


class FinalActivity : AppCompatActivity() {


    private var myDataList : List<PersonInformation> = ArrayList()

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_final)
        setAdapter()

    }

    private fun setAdapter() {
        val adapter = MyInformationAdapter()
        val layoutManager = LinearLayoutManager(this)
        recyclerView.layoutManager = layoutManager
        recyclerView.adapter = adapter
        adapter.updateTheState(myDataList)


    }
}

* Фрагмент как взгляд на MVVM *

class PersonalInformationFragment : Fragment() {

    private var viewModel: PersonInformationViewModel? = null

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


    }

    override fun onActivityCreated(savedInstanceState: Bundle?) {
        super.onActivityCreated(savedInstanceState)
        btn.setOnClickListener {
            viewModel = ViewModelProviders.of(this)[PersonInformationViewModel::class.java]
            viewModel?.getAllData()?.observe(this, Observer {
                this.setAllData(it)
            })
            val intent = Intent(activity, FinalActivity::class.java)
            startActivity(intent)
        }
    }

    private fun setAllData(personInformation: List<PersonInformation>) {
        val setData = PersonInformation(
            name.text.toString(),
            fName.text.toString(),
            email.text.toString(),
            contact.text.toString().toInt()
        )
        viewModel?.setTheData(setData)
    }
}

...