简体   繁体   English

如何在片段 android studio Kotlin 中实现列表视图

[英]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.如何在片段 android studio Kotlin 中实现列表视图我是编程新手,我想让我的应用程序成为按地区划分的地名的简单列表视图。

This is the fragment name VisayasMindanao这是片段名称 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这是我的布局名称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.在我知道的许多语言中,永远不会运行 return 语句之后的代码。

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.确保您的资源看起来像@nitinkumarp 所说的那样。

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) MainActivity(谁拥有 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活动_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

<?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片段.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值: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.注意:为了在您的列表中获得更好的性能,您应该使用 RecyclerView 构建您的列表。

Also, you can find a full example for both implementations in the official documentation.此外,您可以在官方文档中找到这两种实现的完整示例。 See: ListView documentation , RecyclerView documentation请参阅: ListView 文档RecyclerView 文档

Edit: +1 for moving your code above the return statement otherwise it won't be executed.编辑:+1 用于将代码移到 return 语句上方,否则将不会执行。

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:另一个问题是您对StringArrays的声明,它应该是这样的:

<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:编辑:你的 ListView id 是visayas_mindanao_lview但在java中它是: content_main_lview只是在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
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在 Kotlin 中的片段 android studio 中实现动态列表视图 - How to implement a dynamic list view inside fragment android studio in Kotlin 如何在Kotlin中实现View Pager相同片段? - How to implement View Pager same Fragment in Kotlin? 如何从片段视图内部实现Android Spinner - How to implement Android Spinner from inside a Fragment view Android Studio 如何为片段内的视图设置约束 - Android Studio how to set constraints for a view inside a fragment Android studio 如何使用片段在 Kotlin 中打开日期选择器 - Android studio how to open datepicker in Kotlin with Fragment 如何在Android Studio中使用Inflater从片段获取列表视图 - How to Using inflater to get a list view from fragment in Android Studio 片段中的Android Studio列表视图不显示 - Android Studio List View in Fragment Not Display 如何在 Android Studio 中的片段中实现背压 - How to implement onback press in fragment in Android Studio 如何在Android Studio中的片段中实现自定义字体 - How to Implement a Custom Font in a Fragment in Android Studio 如何在 Android Studio 的片段中实现带有适配器的列表视图 - How to implement a listview with an adapter in a fragment in Android Studio
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM