【问题标题】:populating a RecyclerView in a Dialog在对话框中填充 RecyclerView
【发布时间】:2019-01-20 22:51:44
【问题描述】:

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

我目前在使用来自我的适配器的信息的对话框中填充布局时遇到问题。数据是从 API 获取并传递到我的数据类中,但由于我试图引用的 recyclerview 在对话框的布局文件中,而不是在我用来调用所述对话框的文件中,因此视图只返回一个 null .

这是我的上下文代码。

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

任何帮助将不胜感激:)

【问题讨论】:

  • 这意味着people_list不在Activity的布局中。
  • @EpicPandaForce 是的,它不是。我想知道是否有任何方法可以访问不在 Activity 布局中但在对话框布局中的视图
  • 不是 inside Activity :p 但您可以做的是使用布局充气器来充气视图,然后使用 .setView() 方法将其传递给对话框构建器, 然后你可以在你膨胀的视图上通过 id 找到视图
  • @EpicPandaForce 我会在回调中这样做吗?
  • @EpicPandaForce 因为当按下按钮时我已经在 onClick 方法上完成了所有这些操作fun onPeopleClicked(view: View) { val dialog = AlertDialog.Builder(this) val view = layoutInflater.inflate(R.layout.d_manage_people, null) val userName = view.person_name view.manage_people_title.text = "Manage People" dataRetriever.getUsers(callbackGETUSERS, getAuth(), listID) dialog.setView(view) dialog.show() } 格式很差的Yikes:/

标签: android kotlin android-recyclerview dialog adapter


【解决方案1】:

您可以在 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)
}

【讨论】:

  • 问题最终需要我将people_list 附加到view,这将是对话框的上下文。无论如何感谢您的帮助:D
【解决方案2】:

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

而不是像在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)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-02-09
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多