如何在片段Android Studio Kotlin中实现列表视图

时间:2018-08-10 01:30:15

标签: android listview kotlin fragment implements

如何在片段Android Studio Kotlin中实现列表视图 我是编程新手,我想让我的应用程序成为按地区列出的地名的简单列表。

这是片段名称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)
}

}

这是我的布局名称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>

我正试图从这里获取价值

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

5 个答案:

答案 0 :(得分:1)

自从你说你是新来的。在我所知道的许多语言中,return语句之后的代码永远不会运行。

做这样的事情

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
}

如果您正在阅读资源,请执行此操作

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
    }

确保您的资源看起来像@nitinkumarp所说的那样。

如果这对您有用,请检查它是否为答案。帮助其他初学者。谢谢

答案 1 :(得分:0)

您的字符串数组资源格式不正确。 按以下格式添加:

@Embeddable
public class Address {

    @NotNull
    @Size(max = 100)
    private String addressLine1;

    @NotNull
    @Size(max = 100)
    private String addressLine2;

    @NotNull
    @Size(max = 100)
    private String city;

    @NotNull
    @Size
    private String region;

    @NotNull
    @Size
    private String country;

    @NotNull
    @Size
    private String zipCode;

    public Address() {
    }

    public Address(@NotNull @Size(max = 100) String addressLine1, @NotNull @Size(max = 100) String addressLine2,
                   @NotNull @Size(max = 100) String city, @NotNull @Size String region,
                   @NotNull @Size String country, @NotNull @Size String zipCode) {
        this.addressLine1 = addressLine1;
        this.addressLine2 = addressLine2;
        this.city = city;
        this.region = region;
        this.country = country;
        this.zipCode = zipCode;
    }
getters and setters
}

答案 2 :(得分:0)

如文档所述:

  

注意:为了在列表中获得更好的性能,您应该构建   您的列表与RecyclerView。

此外,您可以在官方文档中找到有关这两种实现方式的完整示例。看到: ListView documentationRecyclerView documentation

编辑:+1用于将代码移到return语句上方,否则将不执行。

答案 3 :(得分:0)

我进行了测试,效果很好。这是我的代码:

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

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

值: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>

答案 4 :(得分:-1)

您的代码中有两个问题:

第一

覆写应具有结束点“}”,并删除最后一个“}”之后的另一个。

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

}

第二:

另一个问题是您对StringArrays的声明应该像这样:

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

当然要设置列表:

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

编辑:您的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
    }
}