Search code examples
androidlistviewkotlinfragmentimplements

How to implement list view inside fragment android studio Kotlin


How to implement list view inside fragment android studio Kotlin I am new to programming and I want to make my app a simple listview of names of places by region.

This is the fragment name VisayasMindanao

package com.gumangan.uecficenterslist
import android.os.Bundle
import android.support.v4.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ArrayAdapter
import android.widget.ListView


class VisayasMindanao : Fragment(){

override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {

val centerlist = resources.getStringArray(R.array.region2)

//Creating Array of Region 
var lv = findViewById(R.id.content_main_lview) as ListView 
val adapter = ArrayAdapter(this, android.R.layout.simple_list_item_1, centerlist)
lv.adapter = adapter

    return LayoutInflater.from(container?.context).inflate(R.layout.visayas_mindanao, container, false)
}

}

This is in my layout name visayas_mindanao

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">


<ListView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/visayas_mindanao_lview"/>

</android.support.constraint.ConstraintLayout>

And I'm trying to get values from here

<resources>
<string name="region1">
    <item>ARANAAR TI BAGGAK TI DAYA
    acarra, Ilocos Norte
    </item>
</string>
</resources>

Solution

  • There are two issue in your codes:

    First:

    Override should have ending point "}" and remove the other after the last "}".

    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
        return LayoutInflater.from(container?.context).inflate(R.layout.visayas_mindanao, container, false)
    
    }
    

    Second:

    Another issue is your declaration of StringArrays which it should be like this:

    <string-array name="sample_region1">
            <item>
        Your first item
            </item>
             <item>
        Your Second item
            </item>
        ...
        ..
        .
    </string-array>
    

    And of course, setting the list:

    val centerlist = resources.getStringArray(R.array.sample_region1)
    

    Edit: Your ListView id is visayas_mindanao_lview but in java it is: content_main_lview just set in java:

    class VisayasMindanao : Fragment() {
    
       override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
    
            val view: View = inflater.inflate(R.layout.visayas_mindanao, container, false)
    
            val centerlist = resources.getStringArray(R.array.region2)
            var lv = view.findViewById<ListView>(R.id.visayas_mindanao_lview)
    
            val adapter = ArrayAdapter(context, android.R.layout.simple_list_item_1, centerlist)
            lv.adapter = adapter
    
            return view
        }
    }