【问题标题】:How to show AlertDialog after ArrayList is true or completed in Kotlin?如何在 ArrayList 为真或在 Kotlin 中完成后显示 AlertDialog?
【发布时间】:2021-09-14 04:28:36
【问题描述】:

我有来自 API 的列表,显示数据类如下:

data class Ibadah(
val ibadahText: String,
val id: Int,
val isComplete: Boolean,
val journalId: Int,
val userId: String
)

还有这个类:

@Singleton
class IbadahRVAdapter @Inject constructor(@ApplicationContext val context: Context) :
RecyclerView.Adapter<IbadahRVAdapter.IbadahViewHolder>() {
lateinit var ibadahslist: List<Ibadah>
@Named("journal_url")
@Inject
lateinit var journalInterface: JournalInterface
lateinit var ibadahs_isCompleted: ArrayList<Boolean>
lateinit var ibadahList: ArrayList<Ibadah>
private lateinit var recyclerView: RecyclerView
var jId = 0

inner class IbadahViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView)

override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): IbadahViewHolder {
    val view =
        LayoutInflater.from(parent.context).inflate(R.layout.ibadah_rv_item, parent, false)
    return IbadahViewHolder(view)
}

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

fun setQuranSurahList(quransurahlist: List<Ibadah>) {
    this.ibadahslist = quransurahlist;
    notifyDataSetChanged()
}

override fun onBindViewHolder(holder: IbadahViewHolder, position: Int) {
    ibadahs_isCompleted = ArrayList()
    holder.itemView.apply {
        ibadah_item_text.setText(ibadahslist[position].ibadahText)
        ibadah_item_chechbox.setChecked(ibadahslist[position].isComplete)
        ibadah_item_chechbox.setOnCheckedChangeListener { checkBox, isChecked ->
            patchIbadah(position)
            if (!ibadahs_isCompleted.contains(false)) {

                AlertDialog.Builder(context)
                    .setCancelable(true)
                    .setView(R.layout.finish_ibadh)


                    .show()


            } else {
                

            }
        }

    }
}
fun patchIbadah(position: Int) {
    getToken()
    journalInterface.patchIbadah(ibadahslist[position].id, token)
        .enqueue(object : Callback<ResponseBody> {
            override fun onResponse(
                call: Call<ResponseBody>,
                response: Response<ResponseBody>
            ) {
                if (!response.isSuccessful) {
                    getIbadahsList()
                    Log.e(this.toString(), "code: " + response.code())

                }
            }

            override fun onFailure(call: Call<ResponseBody>, t: Throwable) {
                Log.e(this.toString(), "error: " + t.message)
            }

        })

}

fun setupRV() {

    this.setQuranSurahList(ibadahList)
    recyclerView.layoutManager = LinearLayoutManager(this.context)
    recyclerView.adapter = this


}

fun getIbadahsList() {


    ibadahList = ArrayList()

    journalInterface.getIbadahs(jId, token).enqueue(object : Callback<List<Ibadah>> {
        override fun onResponse(
            call: Call<List<Ibadah>>,
            response: Response<List<Ibadah>>
        ) {
            if (response.isSuccessful) {
                response.body()?.forEach {
                    ibadahList.add(
                        Ibadah(
                            it.ibadahText, it.id, it.isComplete, it.journalId, it.userId
                        )
                    )
                    ibadahs_isCompleted.add(it.isComplete)
                }
                setupRV()


            } else {
                

            }
        }

        override fun onFailure(call: Call<List<Ibadah>>, t: Throwable) {
            
        }

    })

}


}

像这样显示列表:

如果我选中或取消选中任何项目,我当前的代码会显示 AlertDialog。

AlertDialog 放在onBindViewHolder 中。

我希望 AlertDialog 仅在选中所有项目时出现,如果列表中的任何项目未选中,则不要出现。

我在这里做错了什么?

【问题讨论】:

  • 我必须说你的代码结构很奇怪。将 RecyclerView 包含在 Adapter 中,以及负责显示 AlertDialog 的 Adapter 不被认为是一种常见的做法。适配器应该负责创建添加到 RecyclerView 的项目,就是这样。话虽如此,这个调用:patchIbadah(position) 是异步的,这意味着代码在到达此检查之前不会等待它完成:!ibadahs_isCompleted.contains(false)。

标签: java android kotlin arraylist dialog


【解决方案1】:

您不应该在适配器内显示 AlertDialog。使用监听器更新您在 Fragment/Activity 中的更改。

您的问题的解决方案:在 Fragment/Activity 中分配新的/另一个列表,无论所做的更改都通过侦听器并将这些更改添加到新列表中,然后验证原始列表大小等于新列表大小。如果条件为真,则显示 AlertDialog。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-09-20
    • 2019-06-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多