-1

How to use Listview within Fragment. I have implemented almost similar code in an Activity, there it worked perfectly. But in case of Fragment, I have only modified a little to avoid the errors showed by Android Studio. But, it's not working here. here is my code:

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
  }

and my MaskotAdapter class is:

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
    }
  }
4
  • U should use recycler view instead of ListView, which is more flexible and memory management. Commented Jun 14, 2018 at 4:01
  • 1
    Not working?? Is it showing some error? Commented Jun 14, 2018 at 5:06
  • This looks similar stackoverflow.com/questions/22512833/… Commented Jun 14, 2018 at 5:09
  • yup, but that was for Java, this one is for Kotlin Commented Jun 14, 2018 at 6:14

4 Answers 4

1

I found the answer at last. We need to activity as a parameter as well. So, in the code mentioned in the question:

listview.adapter = MaskotAdapter(context)

will be changed into listview.adapter = MaskotAdapter(context,activity) and

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

line should be changed into

private class MaskotAdapter(context: Context, activity: Activity): BaseAdapter() {
Sign up to request clarification or add additional context in comments.

Comments

1

Here, activity parameter is very important. You can change like:

listview.adapter = MaskotAdapter(context)

will be changed into

listview.adapter = MaskotAdapter(context,activity) 

and

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

line should be changed into

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

Hopefully, it will work. But, move to recyclerView if possible, as that is more memory efficient.

Comments

0

You should use the Activity context (getActivity) in instantiating adapter. Fragment does not own a context.

Comments

0

Does not the list appear? Or is there a fatal exception?

and,

Use RecyclerView. Calling inflate each time without using the holder pattern in getView () is an inefficient way.

1 Comment

please attach log.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.