【问题标题】:Why I cannot use Listview within Fragment为什么我不能在 Fragment 中使用 Listview
【发布时间】:2018-06-14 03:04:59
【问题描述】:

如何在Fragment 中使用Listview。我在Activity 中实现了几乎相似的代码,它运行得很好。但是在Fragment 的情况下,我只做了一点修改以避免Android Studio 显示的错误。但是,它在这里不起作用。 这是我的代码:

class FragmentMascot : Fragment() {
  override fun onCreateView(inflater: LayoutInflater?, container: ViewGroup?, savedInstanceState: Bundle?): View? {
    val view= inflater!!.inflate(R.layout.fragment_mascot, container, false)
    val listview = view.findViewById<ListView>(R.id.maskot_list)
    //listview.adapter = MaskotAdapter(this) -> this does't work in fragment but worked in activity
    listview.adapter = MaskotAdapter(context)
    return view
  }

而我的MaskotAdapter 班级是:

private class MaskotAdapter(context: Context): BaseAdapter() {

    private val mContext: Context

    //values can be entered from database too.

    private val maskot_names_list = arrayListOf<String>("","","","")

    private val maskot_details_list = arrayListOf<String>("","","","")

    private val maskot_images_list = arrayListOf<Int>(0,0,0,0)

    init {
      this.mContext = context
    }
    //row count
    override fun getCount(): Int {
      return maskot_names_list.size //To change body of created functions use File | Settings | File Templates.
    }

    override fun getItem(position: Int): Any {
      return "test String" //To change body of created functions use File | Settings | File Templates.
    }

    override fun getItemId(position: Int): Long {
      return position.toLong()//To change body of created functions use File | Settings | File Templates.
    }
    //renders each row
    override fun getView(position: Int, convertView: View?, parent: ViewGroup?): View {
      val layoutInflater = LayoutInflater.from(mContext)
      val maskot_row=layoutInflater.inflate(R.layout.activity_maskot_fragment, parent, false)

      val maskot_names = maskot_row.findViewById<TextView>(R.id.maskot_name)
      maskot_names.text=maskot_names_list.get(position)

      // showing ??? if maskot name is empty
      // and giving it a little padding top, to not to keep it on top
      if(maskot_names.text=="")
      {
        maskot_names.text="???"
        maskot_names.setPadding( 0, 16, 0, 0)
      }

      val maskot_details = maskot_row.findViewById<TextView>(R.id.maskot_details)
      maskot_details.text=maskot_details_list.get(position)


      if(maskot_images_list.get(position)!=0)
      {
        // showing particular maskot image if maskot image is not empty
        val maskot_image = maskot_row.findViewById<ImageView>(R.id.maskot_image)
        maskot_image.setImageResource(maskot_images_list.get(position))
      }
      return maskot_row
    }
  }

【问题讨论】:

  • U应该使用recycler view而不是ListView,这样更灵活和内存管理。
  • 不工作??它显示一些错误吗?
  • 是的,但那是给 Java 的,这个是给 Kotlin 的

标签: android listview kotlin fragment


【解决方案1】:

我终于找到了答案。我们也需要将活动作为参数。所以,在问题中提到的代码中:

listview.adapter = MaskotAdapter(context)

会变成listview.adapter = MaskotAdapter(context,activity) 和

private class MaskotAdapter(context: Context): BaseAdapter() {

行应该改成

private class MaskotAdapter(context: Context, activity: Activity): BaseAdapter() {

【讨论】:

    【解决方案2】:

    这里,活动参数非常重要。您可以更改如下:

    listview.adapter = MaskotAdapter(context)
    

    会改成

    listview.adapter = MaskotAdapter(context,activity) 
    

    private class MaskotAdapter(context: Context): BaseAdapter() {
    

    行应该改成

    private class MaskotAdapter(context: Context, activity: Activity): BaseAdapter() {
    

    希望它会起作用。但是,如果可能,请移至 recyclerView,因为这样更节省内存。

    【讨论】:

      【解决方案3】:

      您应该在实例化适配器时使用活动上下文(getActivity)。 Fragment 不拥有上下文。

      【讨论】:

        【解决方案4】:

        列表没有出现吗?还是有致命异常?

        和,

        使用 RecyclerView。 每次调用 inflate 而不使用 getView() 中的 holder 模式是一种低效的方式。

        【讨论】:

        • 请附上日志。
        猜你喜欢
        • 1970-01-01
        • 2013-11-01
        • 1970-01-01
        • 1970-01-01
        • 2022-01-16
        • 2021-02-03
        • 2020-05-06
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多