在对话框中填充 RecyclerView

populating a RecyclerView in a Dialog

我找到了解决方案并将其作为答案附上

我目前无法使用来自我的适配器的信息在对话框中填充布局。数据是从 API 中获取并传递到我的数据 class 但由于我试图引用的 recyclerview 在对话框的布局文件中,而不是在我用来调用的文件中说对话框,视图只是 returns 一个空值。

这是我的上下文代码。

CheckboxActivity.kt(只是回调)people_list 返回 null

private val callbackGETUSERS = object : Callback<List<Users>> {
    override fun onFailure(call: Call<List<Users>>, t: Throwable) {
        Log.e("API-GET-USERS", "Problem GETTING USERS", t)        }

    override fun onResponse(call: Call<List<Users>>, response: Response<List<Users>>) {

        val result = UsersResult(response.body() ?: return run {
            Log.e("API-ME", "Problem calling USERS")
        })

        peopleList = result
        people_list.adapter = ManagePeopleAdapter(result)
    }

}

d_manage_people.xml(对话框资源文件)

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:paddingTop="16dp">

<TextView
        android:id="@+id/manage_people_title"
        android:gravity="center"
        android:height="24dp"
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        style="@style/Subtitle1"
        android:text="Create Item"
        android:layout_marginBottom="16dp"
        android:layout_gravity="center"/>

<androidx.recyclerview.widget.RecyclerView
        android:id="@+id/people_list"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

</androidx.recyclerview.widget.RecyclerView>

这是我的错误

java.lang.IllegalStateException: people_list must not be null

顺便说一下,我使用的插件允许我不使用 findViewById

任何帮助将不胜感激:)

您可以在 API 调用后显示对话框。如下所示

private val callbackGETUSERS = object : Callback<List<Users>> {
    override fun onFailure(call: Call<List<Users>>, t: Throwable) {
        Log.e("API-GET-USERS", "Problem GETTING USERS", t)        }

    override fun onResponse(call: Call<List<Users>>, response: Response<List<Users>>) {
        val result = UsersResult(response.body() ?: return run {
            Log.e("API-ME", "Problem calling USERS")
        })
        peopleList = result
        this@CheckboxActivity.showDialog()
    }
}

private fun showDialog() {
    val dialog = AlertDialog.Builder(this)
    val view = layoutInflater.inflate(R.layout.d_manage_people, null)
    view.manage_people_title.text = "Manage People" 
    people_list = view.findById(R.id.people_list)
    people_list.adapter = ManagePeopleAdapter(peopleList)
    dialog.setView(view)
    dialog.show() }
}

然后只需在单击按钮时调用 API。

onPeopleClicked(view: View) {
    dataRetriever.getUsers(callbackGETUSERS, getAuth(), listID)
}

问题恰好出在我没有附加视图的上下文

而不是像

那样在 CallBack 中调用 Recycler View
people_list

然后从那里设置适配器,我只是让回调调用了一个函数,然后创建了对话框,我使用了它的上下文来到达 RecyclerView

 val dialog = AlertDialog.Builder(this)
    val view = layoutInflater.inflate(R.layout.d_manage_people, null)
    dialog.setView(view)

    view.manage_people_title.text = "Manage People"

    val adapter = ManagePeopleAdapter(result)
    view.people_list.adapter = adapter
    view.people_list.layoutManager = LinearLayoutManager(this)