Listview Can Appear In Activity But Can't Appear In Fragment
Solution 1:
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.
overridefunonCreateView(inflater: LayoutInflater?, container: ViewGroup?, savedInstanceState: Bundle?): View? {
return inflater!!.inflate(R.layout.fragment_test, container, false)
}
overridefunonViewCreated(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:)
Solution 2:
You written code in onCreateView
after return
statement which we called dead code(code which is unreachable). In that manner you never set your adapter in ListView
.
Follow 3 steps as a general approach -
- Init all data in
onCreate
method. In your case addition of data inArrayList
will go there.
so that will look like -
overridefunonCreate(savedInstanceState: Bundle?) {
super.onCreate(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"))
}
Only init your layout resource in
onCreateView
. Like this -overridefunonCreateView(inflater: LayoutInflater?, container: ViewGroup?, savedInstanceState: Bundle?): View? { return inflater!!.inflate(R.layout.fragment_test, container, false) }
Which is related to rendering data on UI, that should be done in
onViewCreated
method. In your case, it should like -overridefunonViewCreated(view: View?, savedInstanceState: Bundle?) { super.onViewCreated(view, savedInstanceState) adapter = mo3dAdapter (context ,listOfMkabala) // Init your listview tv1 = view?.findViewById(R.id.your_listview_id); tv1.adapter = adapter }
Make sure you init tv1
and it should be ListView
reference.
Post a Comment for "Listview Can Appear In Activity But Can't Appear In Fragment"