提问者:小点点

ListView可以出现在Activity中,但不能出现在Fragment中


ListView可以出现在Activity中,但不能出现在Fragment中,但是我在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
    }

}

共2个答案

匿名用户

您应该在onCreateView()中初始化后执行返回语句。

您正在使用kotlin-android-扩展直接获取您的视图。如果您使用这种方式,则只有在创建视图后才能获得您的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()在创建View之后运行。因此它确保您的视图已经创建。

您也为适配器传递了相同的布局。

在适配器代码中,将R. layout.fragment_test更改为模型布局。

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

希望有帮助:)

匿名用户

您在back语句之后在onCreateView中编写了代码,我们称之为死代码(无法访问的代码)。这样,您就永远不会在ListView中设置适配器。

遵循3个步骤作为一般方法-

  1. 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
}

确保您inittv1并且它应该是ListView引用。