Kotlin:文本从 RecyclerView 中的按钮消失

问题描述 投票:0回答:1

我设置了一个包含按钮列表的

RecyclerView
。列表的长度和每个按钮的文本应来自数据库查询的结果(我在其中查找数据库中保存的所有食谱)。

不知何故,当我打开片段时,我看到正确的文本出现在按钮上几分之一秒,但它立即消失,留下空白按钮列表。我做错了什么?

这是

RecipeListAdapter.kt

package com.example.recipy

import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import com.example.recipy.databinding.RecipeRowViewBinding

class RecipeListAdapter(private val recipesList: ArrayList<RecipeRowViewModel>) : RecyclerView.Adapter<RecipeListAdapter.RecipeListViewHolder>() {

    inner class RecipeListViewHolder(private val binding : RecipeRowViewBinding) : RecyclerView.ViewHolder(binding.root) {
        fun bind(recipeRow : RecipeRowViewModel){
            binding.recipeNameButton.text = recipeRow.recipe
        }
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecipeListViewHolder =
        RecipeListViewHolder(RecipeRowViewBinding.inflate(LayoutInflater.from(parent.context), parent, false))

    override fun onBindViewHolder(holder: RecipeListViewHolder, position: Int) {
        holder.bind(recipesList[position])
    }

    override fun getItemCount(): Int {
        return recipesList.size
    }
}

RecipeRowViewModel.kt

package com.example.recipy

data class RecipeRowViewModel(var recipe : String)

recipe_row_view.xml

<?xml version="1.0" encoding="utf-8"?>
<layout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto">

    <data>
        <variable
            name="recipeRow"
            type="com.example.recipy.RecipeRowViewModel" />
    </data>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">

        <Button
            android:id="@+id/recipe_name_button"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:padding="5dp"
            android:text="@={recipeRow.recipe}">
        </Button>

    </LinearLayout>

</layout>

包含 RecyclerView 的

BrowseRecipeFragment

package com.example.recipy

import android.os.Bundle
import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.recyclerview.widget.LinearLayoutManager
import com.example.recipy.databinding.FragmentBrowseRecipesBinding

class BrowseRecipesFragment : Fragment() {

    private var _binding: FragmentBrowseRecipesBinding? = null

    private val binding get() = _binding!!

    private val recipesList : ArrayList<RecipeRowViewModel> = arrayListOf()

    private lateinit var customAdapter : RecipeListAdapter

    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        _binding = FragmentBrowseRecipesBinding.inflate(inflater, container, false)
        setAdapter()
        return binding.root
    }


    private fun setAdapter(){
        customAdapter = RecipeListAdapter(recipesList)
        binding.recipeRowRecyclerView.apply {
            layoutManager = LinearLayoutManager(requireContext())
            adapter = customAdapter
        }
    }

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

        // Retrieve recipes from db and add them to recipesList
        val db = DBHelper(requireContext(), null)
        val cursor = db.getAllRecipes()
        cursor!!.moveToFirst()
        recipesList.add(RecipeRowViewModel(cursor.getString(cursor.getColumnIndex(DBHelper.RECIPE_NAME_COL))))
        while(cursor.moveToNext()){
            recipesList.add(RecipeRowViewModel(cursor.getString(cursor.getColumnIndex(DBHelper.RECIPE_NAME_COL))))
        }
        cursor.close()
    }

    override fun onDestroyView() {
        super.onDestroyView()
        _binding = null
    }
}

fragment_browse_recipes.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=".BrowseRecipesFragment">

    <TextView
        android:id="@+id/textview_second"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Here will be the search bar"
        android:textSize="20sp"
        app:layout_constraintTop_toTopOf="parent" />

    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="0dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/textview_second" >

        <androidx.recyclerview.widget.RecyclerView
            android:id="@+id/recipe_row_recycler_view"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintTop_toBottomOf="parent"
            app:layout_constraintBottom_toBottomOf="parent"
            tools:listitem="@layout/recipe_row_view" />
    </ScrollView>
</androidx.constraintlayout.widget.ConstraintLayout>
android kotlin android-recyclerview android-button
1个回答
0
投票

setAdapter()
中调用函数
override onViewCreated()
而不是
onCreateView()

onViewCreated()
onCreateView()
之后被触发。

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

    // Retrieve recipes from db and add them to recipesList
    val db = DBHelper(requireContext(), null)
    val cursor = db.getAllRecipes()
    cursor!!.moveToFirst()
    recipesList.add(RecipeRowViewModel(cursor.getString(cursor.getColumnIndex(DBHelper.RECIPE_NAME_COL))))
    while(cursor.moveToNext()){
        recipesList.add(RecipeRowViewModel(cursor.getString(cursor.getColumnIndex(DBHelper.RECIPE_NAME_COL))))
    }
    cursor.close()

    setAdapter()
}

填充列表后,您必须使用

setAdapter()
才能传递给适配器。

© www.soinside.com 2019 - 2024. All rights reserved.