ListView可以显示在Activity
但不能显示在Fragment
但是我在Kotlin中使用与Android Studio 3.0相同的代码?
这是我的片段代码:
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){
}
这是我的适配器:
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
}
}
答案 0 :(得分:2)
你应该在你的初始化后做一个return语句
onCreateView()
。
您正在使用kotlin-android-extensions
直接获取您的观看次数。如果您使用这种方式,则只能在创建视图后获取LisView
。
您的代码应该是这样的。
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()
。因此,它可以确保您的视图已经创建。
您也为适配器传递了相同的布局。
在适配器代码中,将R.layout.fragment_test
更改为您的模型布局。
var myView = inflator.inflate(R.layout.fragment_test, null)
希望有所帮助:)
答案 1 :(得分:1)
您在onCreateView
语句之后在return
中编写了代码,我们称之为死代码(代码无法访问)。以这种方式,您永远不会在ListView
中设置适配器。
作为一般方法遵循3个步骤 -
onCreate
方法初始化所有数据。在您的情况下,ArrayList
中的数据将添加到那里。所以看起来像 -
override fun onCreate(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"))
}
仅在onCreateView
中初始化您的布局资源。像这样 -
override fun onCreateView(inflater: LayoutInflater?, container: ViewGroup?,
savedInstanceState: Bundle?): View? {
return inflater!!.inflate(R.layout.fragment_test, container, false)
}
这与在UI上呈现数据有关,应该在onViewCreated
方法中完成。在你的情况下,它应该 - -
override fun onViewCreated(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
}
确保您初始化tv1
,并且应该ListView
参考。