0

リサイクラー ビューに使用できるように、変更可能なリストを作成しようとしています。残念ながら、リストにデータを入力していると思いますが、まだ空のままで、リサイクラー ビューが機能していません (リストの問題が原因だと思います)。コードについては、以下を参照してください。

        private val newList: MutableList<NewListModel> = mutableListOf()
        private val oldList = retrieveOldList()

        private fun retrieveAndPopulate() {
                
                for (i in 0 until oldList.size){
        
                    val oldItem = oldList[i]
                    
                    val itemOne = oldItem.itemOne
                    val itemTwo = oldItem.itemTwo
                    val itemThree = oldItem.itemThree
                    val itemFour = oldItem.itemFour
    
                    val newItemData =
                        NewListModel(
                            itemOne, itemTwo, itemThree, itemFour
                        )
    
                newList.add(newItemData)
                Log.d(
                    "RetrieveData",
                    "${newItemData.itemOne} has been added to the list."
                )
            }
        }

以下のクラスは「NewListModel」用です

@Keep
@IgnoreExtraProperties
data class NewListModel (
    var itemOne: String ?= null,
    var itemTwo: String ?= null,
    var itemThree: String ?= null,
    var itemFour: String ?= null,
)

以下は、「oldList」にデータを入力しようとする方法です

fun retrieveData(): MutableList<OldListModel> {

        val list: MutableList<OldListModel> = mutableListOf()
        val ref = FirebaseDatabase.getInstance().getReference("/storage")

        ref.addValueEventListener(object : ValueEventListener {
            override fun onDataChange(snapshot: DataSnapshot) {
                if (snapshot.exists()) {
                    ref.get()
                        .addOnSuccessListener {

                            for (listItem in snapshot.children) {
                                val listItem = snapshot.getValue(OldListModel::class.java)

                                if (listItem != null) {
                                    list.add(listItem)
                                }
                            }
                        }
                } else {
                    Log.d(
                        "Data",
                        "Retrieving data was unsuccessful."
                    )
                }
            }

            override fun onCancelled(error: DatabaseError) {
            }
        })
        return list
}

ある変更可能なリストからデータを取得し、それを別のリストに追加していることは、おそらく言及する価値があります。どんな助けでも大歓迎です

(以下は、リサイクラービューにデータを入力しようとする方法です)

val newList = retrieveAndPopulate()

val recyclerView = findViewById<View>(R.id.recyclerView) as RecyclerView
val layoutManager = LinearLayoutManager(this)
recyclerView.layoutManager = layoutManager
val adapterAdapter = AdapterAdapter(newList)
recyclerView.adapter = adapterAdapter
4

2 に答える 2

2

あなたの問題は、コードが非同期で実行されているときに、コードを順番に実行していると思うことです。関数の番号付きコメントを参照して、実行順序を追跡します。

fun retrieveData(): MutableList<OldListModel> {

    // 1. Here you create a list
    val list: MutableList<OldListModel> = mutableListOf()
    val ref = FirebaseDatabase.getInstance().getReference("/storage")

    // 2. Here a listener is added that will let you know LATER when the data is ready
    ref.addValueEventListener(object : ValueEventListener {
        // 4. LATER the data changed will get called
        override fun onDataChange(snapshot: DataSnapshot) {
            if (snapshot.exists()) {
                ref.get()
                    .addOnSuccessListener {
                        // 5. EVEN LATER this listener is called with data
                        for (listItem in snapshot.children) {
                            val listItem = snapshot.getValue(OldListModel::class.java)

                            // 6. FINALLY - you add to a list that has long since stopped being relevant
                            if (listItem != null) {
                                list.add(listItem)
                            }
                        }
                    }
            } else {
                Log.d(
                    "Data",
                    "Retrieving data was unsuccessful."
                )
            }
        }

        override fun onCancelled(error: DatabaseError) {
        }
    })
    return list // 3. Here you return the EMPTY list that was created
}

解決策 -最善の解決策ではないかもしれませんが、コールバックが完了したらリストを更新することです:

private val theList = mutableListOf<YourDataModelType>

fun retrieveData(): { // No longer returning anything

    // Remove this, no longer returning anything
    // val list: MutableList<OldListModel> = mutableListOf()

    val ref = FirebaseDatabase.getInstance().getReference("/storage")
    ref.addValueEventListener(object : ValueEventListener {
        override fun onDataChange(snapshot: DataSnapshot) {
            if (snapshot.exists()) {
                ref.get()
                    .addOnSuccessListener {
                        theList.clear() // Clear out existing data

                        for (listItem in snapshot.children) {
                            val listItem = snapshot.getValue(OldListModel::class.java)

                            if (listItem != null) {
                                theList.add(listItem)
                            }
                        }
                        
                        // Since you're using Kotlin, you could use a map,
                        // but that's unrelated to this issue
                        // val list = snapshot.children.map { getValue(...) }.filterNotNull()
                        
                        // Now that we have a full list here, update:
                        updateAdapterWithNewData(theList)
                    }
            } else {
                Log.d(
                    "Data",
                    "Retrieving data was unsuccessful."
                )
            }
        }

        override fun onCancelled(error: DatabaseError) {
        }
    })
}

updateAdapterWithNewDataそれが言うようにあなたが書く関数はどこにありますか。

非同期プログラミングについてよく読んで、Firebase などのフレームワークでコールバック / リスナーを使用する場合のコードの流れを理解しておいてください。

于 2021-11-25T08:18:39.333 に答える
0

私が抱えていた問題はcallbacksonSuccessListener. これにより、リストがまったく更新されませんでした。

インターネットで 1 日スクロールした後、最終的にこれらの解決策を見つけました。

  1. 解決策:リンク

  2. ソリューションへの拡張:リンク

これで私の問題は解決しました。あなたの問題も解決することを願っています!

于 2021-11-26T01:05:05.997 に答える