简体   繁体   中英

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>

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

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>

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>

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM