Fragment doesnt show data from database (I recently started learning programming)

Database is working since i can see the data in app inspection i dont know what i am doing wrong and cant display the data in the FragmentShow fragment i want the data to be displayed in the above fragment in a recyclerview Any help will be appreciated ( i am a beginner )

Fragment with recyclerview

  class FragmentShow: Fragment(R.layout.fragment_show) {
private var _binding: FragmentShowBinding? = null
private val binding get() = _binding!!
private lateinit var sqLiteHelper: SQLiteHelper
private var adapter: EntryAdapter? = null

override fun onCreateView(
    inflater: LayoutInflater,
    container: ViewGroup?,
    savedInstanceState: Bundle?
): View? {
    sqLiteHelper = SQLiteHelper(requireActivity() as MainActivity)
    _binding = FragmentShowBinding.inflate(inflater,container,false)
    initRecyclerView()
    getEntries()

    return view
}

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
    super.onViewCreated(view, savedInstanceState)
    adapter?.notifyDataSetChanged()
    sqLiteHelper = SQLiteHelper(requireActivity() as MainActivity)

}



private fun initRecyclerView() {
    binding.recyclerviewID.layoutManager = LinearLayoutManager(requireActivity() as MainActivity)
    adapter = EntryAdapter()
    binding.recyclerviewID.adapter = adapter



}

private fun getEntries() {
    val entrylist = sqLiteHelper.getEntry()
    adapter?.addItems(entrylist)

}


override fun onDestroyView() {
    super.onDestroyView()
    return
}

}

Adapter

 class EntryAdapter : RecyclerView.Adapter<EntryAdapter.ViewHolder>() {
private var entryList: ArrayList<Entry> = ArrayList()
private var onClickItem: ((Entry) -> Unit)? = null
private var onClickDeleteItem: ((Entry) -> Unit)? = null

fun addItems(items: ArrayList<Entry>) {
    this.entryList = items
    notifyDataSetChanged()
}
fun setOnClickItem(callback: (Entry) -> Unit) {
    this.onClickItem = callback
}

fun onClickDeleteItem(callback: (Entry) -> Unit) {
    this.onClickDeleteItem = callback
}


override fun onCreateViewHolder(parent: ViewGroup, viewType: Int)= ViewHolder (
   LayoutInflater.from(parent.context).inflate(R.layout.recycler_view_row,parent,false)
)

override fun onBindViewHolder(holder: ViewHolder, position: Int) {
    val ent = entryList[position]
    holder.bindView(ent)
    holder.itemView.setOnClickListener { onClickItem?.invoke(ent) }
    holder.btnDelete.setOnClickListener { onClickDeleteItem?.invoke(ent) }
}

override fun getItemCount(): Int {
    return entryList.size
}

class ViewHolder(view: View): RecyclerView.ViewHolder(view) {
    private var id = view.findViewById<Button>(R.id.IdTv)
    private var name = view.findViewById<TextView>(R.id.NameTV)
    private var work = view.findViewById<TextView>(R.id.WorkTv)
    private var problems = view.findViewById<TextView>(R.id.ProblemTv)
    var btnDelete = view.findViewById<Button>(R.id.btnDelete)

    fun bindView(entry: Entry){
        id.text = entry.id.toString()
        name.text = entry.name
        work.text = entry.work
        problems.text = entry.problems

    }

}

}

MainActivity

    class MainActivity : AppCompatActivity() {
lateinit var binding: ActivityMainBinding
private lateinit var adapter: EntryAdapter
private lateinit var sqLiteHelper: SQLiteHelper
override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)

    sqLiteHelper = SQLiteHelper(this)
    binding = ActivityMainBinding.inflate(layoutInflater)
    setContentView(binding.root)

    binding.btnFirstFragment.setOnClickListener {
        fragmentNav(FragmentEntry())
    }


    binding.btnSecondFragment.setOnClickListener {
        fragmentNav(FragmentShow())

    }

}

private fun fragmentNav(fragment: Fragment) {
    val fragmentManager = supportFragmentManager
    val fragmentTransaction = fragmentManager.beginTransaction()
    fragmentTransaction.replace(R.id.fragmentContainerView2,fragment)
    fragmentTransaction.commit()
}

}



Comments

Popular posts from this blog

Today Walkin 14th-Sept

Spring Elasticsearch Operations

Hibernate Search - Elasticsearch with JSON manipulation