1

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>
4
  • in onCreateView, put your code to above return Commented Aug 10, 2018 at 1:31
  • Im sorry I dont understand, what code shall I add Commented Aug 10, 2018 at 1:40
  • Please mention the issue you have Commented Aug 10, 2018 at 3:42
  • I am creating an app that when i click one of options in navigation drawer a fragment will open and a listview would show, I trying to implement listview in fragment with this simple basic code. Commented Aug 11, 2018 at 8:52

5 Answers 5

2

Since you said you are new. in many languages that i know, codes after return statements are never run.

Do something like this

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

override fun onViewCreated(){
 val lv = findViewById(R.id.visayas_mindanao_lview) as ListView
    val adapter = ArrayAdapter(this, android.R.layout.simple_list_item_1,listOf("car", "plane"))
    lv.adapter = adapter
}

If you reading from resources, do this

override fun onViewCreated(){
     val lv = findViewById(R.id.visayas_mindanao_lview) as ListView
        val adapter = ArrayAdapter(this, android.R.layout.simple_list_item_1, resources.getStringArray(// the resource ID of the array))
        lv.adapter = adapter
    }

Make sure your resources looks like what @nitinkumarp said.

If this worked for you, check it as the answer. to help other beginners. thanks

Sign up to request clarification or add additional context in comments.

2 Comments

but i am getting strings from resources, because i have more than 50 to input.
Thanks you for helping but still getting error with findViewById, I already run it by the code given by Moshen
1

I tested and it worked very well. Here is my code:

MainActivity (who has the frameLayout)

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import androidx.fragment.app.Fragment

class MainActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        setFragment(FirstFragment())
    }

    fun setFragment(f: Fragment) {

        val ft = supportFragmentManager.beginTransaction()

        ft.replace(R.id.framelayout, f)
        ft.commit()
    }
}

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <FrameLayout
        android:id="@+id/framelayout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>

fragment.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.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"
    tools:context=".FirstFragment">


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

</androidx.constraintlayout.widget.ConstraintLayout>

fragment.kt


import android.os.Bundle
import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ArrayAdapter
import android.widget.ListView

/**
 * A simple [Fragment] subclass.
 */
class FirstFragment : Fragment() {

    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_first, container, false)
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)

        val context = context as MainActivity
        val centerlist = resources.getStringArray(R.array.region1)

        val lv = context.findViewById(R.id.visayas_mindanao_lview) as ListView
        val adapter = ArrayAdapter(context, android.R.layout.simple_list_item_1, centerlist)
        lv.adapter = adapter
    }
}

values: strings.xml

<resources>
    <string-array name="region1">
        <item>item1</item>
        <item>item2</item>
        <item>item3</item>
        <item>item4</item>
        <item>item5</item>
        <item>item6</item>
    </string-array>
</resources>

Comments

0

Your String Array resources is not in correct format. Add it like in following format:

<string-array name="sample_region1">
    <item>ACAYA
Nalasin, Solsona, Ilocos Norte</item>
    <item>ALPHA KEN OMEGA
Imelda Marcos, Ilocos Norte</item>

</string-array>

Comments

-1

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

9 Comments

Thanks no error but the the app crash when its opening
Which error? could you show me or update your post? Thank you
I updated my post above, still having error findViewById is red
Check my updated answer. problem is your id of ListView
no thats not the error, its findViewById and Array adapter is underlinedin red
|
-1

As the documentation says:

Note: For better performance in your list, you should instead build your list with RecyclerView.

Also, you can find a full example for both implementations in the official documentation. See: ListView documentation, RecyclerView documentation

Edit: +1 for moving your code above the return statement otherwise it won't be executed.

Comments

Your Answer

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