Nour Medhat Nour Medhat - 1 month ago 10
Android Question

ListView can appear in Activity but can't appear in Fragment

ListView can appear in

Activity
but can't appear in
Fragment
However I use the same code in Kotlin with Android Studio 3.0?

And this is my fragment code:

class testFrag : Fragment() {
var adapter : mo3dAdapter?=null
var listOfMkabala = ArrayList<MeetingDetails>()


override fun onCreateView(inflater: LayoutInflater?, container: ViewGroup?, savedInstanceState: Bundle?): View? {
return inflater!!.inflate(R.layout.fragment_test, container, false)


listOfMkabala .add ( MeetingDetails(" nour1", "ahmed1" , "aya1"))
listOfMkabala .add ( MeetingDetails(" nour2", "ahmed2" , "aya2"))
listOfMkabala .add ( MeetingDetails(" nour3", "ahmed3" , "aya3"))
listOfMkabala .add ( MeetingDetails(" nour4", "ahmed4" , "aya4"))
listOfMkabala .add ( MeetingDetails(" nour5", "ahmed5" , "aya5"))
listOfMkabala .add ( MeetingDetails(" nour6", "ahmed6" , "aya6"))


adapter = mo3dAdapter (context ,listOfMkabala)
tv1.adapter = adapter

}


fun test ( view: View){

}


this my adapter:

inner class mo3dAdapter: BaseAdapter {
var listOfMkabaln= ArrayList<MeetingDetails>()
var context:Context?=null
constructor(context:Context, listOfMkabaln: ArrayList<MeetingDetails>):super(){
this.listOfMkabaln=listOfMkabaln
this.context=context
}

override fun getView(p0: Int, p1: View?, p2: ViewGroup?): View {
val mo3d = listOfMkabaln[p0]

var inflator = context!!.getSystemService(Context.LAYOUT_INFLATER_SERVICE) as LayoutInflater
var myView = inflator.inflate(R.layout.fragment_test, null)
myView.text1.text = mo3d.name1!!
myView.text2.text = mo3d.name2!!
myView.text3.text = mo3d.name3!!

return myView

}

override fun getItem(p0: Int): Any {
return listOfMkabaln[p0]
}

override fun getItemId(p0: Int): Long {
return p0.toLong()
}

override fun getCount(): Int {

return listOfMkabaln.size
}

}

Answer Source

You should make a return statement after your initialization in your onCreateView().

You are using kotlin-android-extensions to get your view directly. If you use that way, you can get your LisView only after view created.

Your code should be like this.

override fun onCreateView(inflater: LayoutInflater?, container: ViewGroup?, savedInstanceState: Bundle?): View? {
 return inflater!!.inflate(R.layout.fragment_test, container, false)
}

override fun onViewCreated(view: View?, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
    listOfMkabala .add ( MeetingDetails(" nour1", "ahmed1" , "aya1"))
    listOfMkabala .add ( MeetingDetails(" nour2", "ahmed2" , "aya2"))
    listOfMkabala .add ( MeetingDetails(" nour3", "ahmed3" , "aya3"))
    listOfMkabala .add ( MeetingDetails(" nour4", "ahmed4" , "aya4"))
    listOfMkabala .add ( MeetingDetails(" nour5", "ahmed5" , "aya5"))
    listOfMkabala .add ( MeetingDetails(" nour6", "ahmed6" , "aya6"))
    adapter = mo3dAdapter (context  ,listOfMkabala)
    tv1.adapter = adapter
}

onViewCreated() runs after the View has been created. So it ensures your view already created.

You have passed the same layout for your adapter too.

In your adapter code change the R.layout.fragment_test to your model layout.

var myView = inflator.inflate(R.layout.fragment_test, null)

Hope it helps:)