0

i would like to use a grind view in a fragment but i couldn't find how to do it. so i tried to adapte the way i did it in an activity but even if i don't get a crash, the grid is not displayed. Here is my code : Fragment

class DashboardFragment : Fragment() {

private var _binding: FragmentDashboardBinding? = null

private val binding get() = _binding!!

lateinit var courseGRV: GridView
lateinit var courseList: List<GridViewModal>

override fun onCreateView(
    inflater: LayoutInflater,
    container: ViewGroup?,
    savedInstanceState: Bundle?
): View {
    val dashboardViewModel =
        ViewModelProvider(this).get(DashboardViewModel::class.java)

    _binding = FragmentDashboardBinding.inflate(inflater, container, false)
    val root: View = binding.root

    //courseGRV = findViewById(R.id.idGRV)
    val view: View = inflater.inflate(R.layout.fragment_dashboard, container, false)
    courseGRV = view!!.findViewById(R.id.idGRV) as GridView
    courseList = ArrayList<GridViewModal>()

    courseList = courseList + GridViewModal("Art", R.drawable.art)
    courseList = courseList + GridViewModal("Culture", R.drawable.culture)
    courseList = courseList + GridViewModal("Hobbies", R.drawable.hobbies)

    println(courseList.size) //this work and give me 3
    val courseAdapter = GridRVAdapter(courseList = courseList, this.requireContext()

    courseGRV.adapter = courseAdapter
    //courseGRV.get(0) //this give me java.lang.IndexOutOfBoundsException: Index: 0, Size: 0
   
    courseGRV.onItemClickListener = AdapterView.OnItemClickListener { _, _, position, _ ->
 
        Toast.makeText(
            this.requireContext(), courseList[position].courseName + " selected",
            Toast.LENGTH_SHORT
        ).show()
    }

    return root
}

override fun onDestroyView() {
    super.onDestroyView()
    _binding = null
}

}

adapter

    internal class GridRVAdapter(
    private val courseList: List<GridViewModal>,
    private val context: Context) :BaseAdapter() {

    private var layoutInflater: LayoutInflater? = null
    private lateinit var courseTV: TextView
    private lateinit var courseIV: ImageView

    override fun getCount(): Int {
        return courseList.size
    }

    // below function is use to return the item of grid view.
    override fun getItem(position: Int): Any? {
        return null
    }

    override fun getItemId(position: Int): Long {
        return 0
    }

    override fun getView(position: Int, convertView: View?, parent: ViewGroup?): View? {
        var convertView = convertView
     
        println("getview")// never called
        if (layoutInflater == null) {
            layoutInflater =
                context.getSystemService(Context.LAYOUT_INFLATER_SERVICE) as LayoutInflater
        }
     
        if (convertView == null) {
            convertView = layoutInflater!!.inflate(R.layout.gridview_item, null)
        }
        courseIV = convertView!!.findViewById(R.id.idIVCourse)
        courseTV = convertView!!.findViewById(R.id.idTVCourse)

        courseIV.setImageResource(courseList.get(position).courseImg)
        
        
        courseTV.setText(courseList.get(position).courseName)
        
        return convertView
    }
}

dataclass

data class GridViewModal(

    val courseName: String,
    val courseImg: Int
)

griditem

<?xml version="1.0" encoding="utf-8"?>
<androidx.cardview.widget.CardView
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    android:layout_margin="5dp"
    app:cardCornerRadius="5dp"
    app:cardElevation="5dp">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">

        <ImageView
            android:id="@+id/idIVCourse"
            android:layout_width="80dp"
            android:layout_height="80dp"
            android:layout_gravity="center"
            android:layout_margin="5dp"
            android:padding="4dp"
            android:src="@mipmap/ic_launcher" />

        <TextView
            android:id="@+id/idTVCourse"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:layout_margin="5dp"
            android:padding="4dp"
            android:text="@string/app_name"
            android:textAlignment="center"
            android:textColor="@color/md_black_1000" />

    </LinearLayout>

</androidx.cardview.widget.CardView>

fragment xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".activities.ui.dashboard.DashboardFragment">

    <GridView
        android:id="@+id/idGRV"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:horizontalSpacing="6dp"
        android:numColumns="2"
        android:verticalSpacing="6dp" />

</androidx.constraintlayout.widget.ConstraintLayout>

So i don't get why my grid is not displayed, with some print i tried, i saw that the adapter is full before i add it to my gridView, but the getView of the adapter is never called. Also if i want to access to item inside my gridView i get an index out of bound so i assume that it's not fully initialize or populate.

If someone could help me with my problem i would really appreciate.

XCarb
  • 735
  • 10
  • 31
  • why are you using viewBinding and then you are inflating view using findViewById? this approach overrides viewBinding. I think this is the problem – Andrea Guadagni Jun 26 '23 at 08:00
  • Hum, i was wondering if this was the problem, it's why i left courseGRV = findViewById(R.id.idGRV) in comment when i inflate the findViewById. The problem is that i used to do findViewById when i was in activity but i can't do it in fragment. It why i tried to inflate it but how can i do it otherwise? – XCarb Jun 26 '23 at 13:03

0 Answers0