【问题标题】:Kotlin: ListVew inside a fragmentKotlin:片段内的 ListView
【发布时间】:2022-07-04 23:17:12
【问题描述】:

我正在尝试在片段中创建一个非常简单的列表视图,但是我做错了一些事情并且列表视图没有显示在屏幕上。

这是我的fragmentWifi.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.Button
import android.widget.ListView

// TODO: Rename parameter arguments, choose names that match
// the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
private const val ARG_PARAM1 = "param1"
private const val ARG_PARAM2 = "param2"

/**
 * A simple [Fragment] subclass.
 * Use the [fragmentWifi.newInstance] factory method to
 * create an instance of this fragment.
 */
class fragmentWifi : Fragment() {
    // TODO: Rename and change types of parameters
    private var param1: String? = null
    private var param2: String? = null

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        arguments?.let {
            param1 = it.getString(ARG_PARAM1)
            param2 = it.getString(ARG_PARAM2)
        }
    }

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

        // Inflate the layout for this fragment
        //return inflater.inflate(android.R.layout.fragment_wifi, container, false)

        val contentView: View = inflater.inflate(R.layout.fragment_wifi, container, false)
        val listView: ListView = contentView.findViewById(R.id.wifiList)
        val buttonScan: Button = contentView.findViewById(R.id.scanBtn)


        // sample data
        val list: MutableList<String> = ArrayList()
        for (i in 0..99) list.add("Item $i")
        //val listAdapter = customAdapter(this, list)
        //val listAdapter = ArrayAdapter(this, R.layout.simple_list_item, list)
        val listAdapter = ArrayAdapter(this.requireContext(), R.layout.simple_list_item, list)
        listView.adapter = listAdapter
        return contentView
    }







    companion object {
        /**
         * Use this factory method to create a new instance of
         * this fragment using the provided parameters.
         *
         * @param param1 Parameter 1.
         * @param param2 Parameter 2.
         * @return A new instance of fragment fragmentwifi.
         */
        // TODO: Rename and change types and number of parameters
        @JvmStatic
        fun newInstance(param1: String, param2: String) =
            fragmentWifi().apply {
                arguments = Bundle().apply {
                    putString(ARG_PARAM1, param1)
                    putString(ARG_PARAM2, param2)
                }
            }
    }
}

这是我的fragment_wifi.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:padding="10dp"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <ListView
        android:id="@+id/wifiList"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="0.97"/>

    <Button
        android:id="@+id/scanBtn"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:layout_gravity="bottom"
        android:layout_margin="15dp"
        android:background="@android:color/holo_red_light"
        android:text="Scan WiFi networks"/>
</LinearLayout>

这是R.layout.simple_list_item

<?xml version="1.0" encoding="utf-8"?>
<!-- Copyright (C) 2006 The Android Open Source Project

     Licensed under the Apache License, Version 2.0 (the "License");
     you may not use this file except in compliance with the License.
     You may obtain a copy of the License at

          http://www.apache.org/licenses/LICENSE-2.0

     Unless required by applicable law or agreed to in writing, software
     distributed under the License is distributed on an "AS IS" BASIS,
     WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     See the License for the specific language governing permissions and
     limitations under the License.
-->

<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@android:id/text1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:textAppearance="?android:attr/textAppearanceListItemSmall"
    android:gravity="center_vertical"
    android:paddingStart="?android:attr/listPreferredItemPaddingStart"
    android:paddingEnd="?android:attr/listPreferredItemPaddingEnd"
    android:minHeight="?android:attr/listPreferredItemHeightSmall" />

我认为问题出在fragmentWifi.kt 类中,第 55 行:

val listAdapter = ArrayAdapter(this.requireContext(), R.layout.simple_list_item, list)

我是否传递了错误的上下文?我也试过super.requireContext(),但没有任何改变。我什至尝试过创建一个自定义数组适配器,结果总是一样的:c

【问题讨论】:

    标签: android kotlin listview android-fragments


    【解决方案1】:

    您对布局的膨胀程度超出了您的要求。请按照以下代码进行操作。

    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        val contentView = inflater.inflate(R.layout.fragment_wifi, container, false)
        val listView: ListView = contentView.findViewById(R.id.wifiList)
        val buttonScan: Button = contentView.findViewById(R.id.scanBtn)
    
    
        // sample data
        val list: MutableList<String> = ArrayList()
        for (i in 0..99) list.add("Item $i")
        //val listAdapter = customAdapter(this, list)
        //val listAdapter = ArrayAdapter(this, R.layout.simple_list_item, list)
        val listAdapter = ArrayAdapter(this.requireContext(), R.layout.simple_list_item, list)
        listView.adapter = listAdapter
        return contentView
    }
    

    【讨论】:

      猜你喜欢
      • 2019-03-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-06-29
      • 2021-12-31
      • 1970-01-01
      • 1970-01-01
      • 2016-08-20
      相关资源
      最近更新 更多