我试图在收到API响应后在片段中填充RecyclerView。我得到了结果,但它们从未出现在屏幕上。
的数据
我错过了什么?
My MainActivity启动以下片段:
// Generate API Client
val client = RestClient(endpoint, TimeoutType.LONG_TIMEOUT)
// Generate dialog
val dialog = AccessHistoryFragment.newInstance(client)
dialog.isCancelable = true
dialog.show(supportFragmentManager, AccessHistoryFragment::class.java.name)
字符串
下面是Fragment的代码:
class AccessHistoryFragment: DialogFragment()
{
private lateinit var context: Context
private lateinit var progressBar: ProgressBar
private lateinit var noResultsTv: AppCompatTextView
private lateinit var accessHistoryRv: RecyclerView
private lateinit var adapter: AccessHistoryRecyclerViewAdapter
private lateinit var client: RestClient
private var elements = mutableListOf<Access>()
companion object
{
fun newInstance(client: RestClient): AccessHistoryFragment
{
val fragment = AccessHistoryFragment()
fragment.client = client
return fragment
}
}
override fun onCreateDialog(savedInstanceState: Bundle?): Dialog
{
// Get views
val view = layoutInflater.inflate(R.layout.fragment_access_history, null) as View
progressBar = view.findViewById(R.id.loading_bar) as ProgressBar
accessHistoryRv = view.findViewById(R.id.access_history_rv) as RecyclerView
noResultsTv = view.findViewById(R.id.no_results_tv) as AppCompatTextView
context = view.context
// Generate Recycler View
generateRecyclerView()
// Generate builder and title
val builder = AlertDialog.Builder(activity)
builder.setView(view)
return builder.create()
}
override fun onActivityCreated(savedInstanceState: Bundle?) {
super.onActivityCreated(savedInstanceState)
cargarHistorial()
}
private fun generateRecyclerView()
{
try
{
adapter = AccessHistoryRecyclerViewAdapter(elements)
accessHistoryRv.apply {
this.layoutManager = LinearLayoutManager(context)
this.adapter = adapter
}
}
catch (ex: Exception) { dismiss() }
}
private fun cargarHistorial()
{
Thread {
try
{
val history = client.getAccessHistory(DateHelper.getCurrentDate())
updateAccessHistory(history)
}
catch (ex: Exception) { dismiss() }
}.start()
}
private fun updateAccessHistory(accessList: MutableList<Access>)
{
requireActivity().runOnUiThread {
progressBar.visibility = View.GONE
if (accessList.size > 0)
{
elements.addAll(accessList)
adapter.notifyItemRangeInserted(0, accessList.size)
accessHistoryRv.visibility = View.VISIBLE
}
else noResultsTv.visibility = View.VISIBLE
}
}
}
型
最后是适配器代码:
class AccessHistoryRecyclerViewAdapter(private var elements: MutableList<Access>): RecyclerView.Adapter<AccessHistoryRecyclerViewAdapter.AccessHistoryViewHolder>()
{
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): AccessHistoryViewHolder
{
return AccessHistoryViewHolder(LayoutInflater.from(parent.context).inflate(R.layout.access_item, parent, false))
}
override fun onBindViewHolder(holder: AccessHistoryViewHolder, position: Int)
{
// Store current task
holder.access = elements[position]
// Init view elements
holder.accessText.text = holder.access.toString()
}
// Return the size of your list
override fun getItemCount() = elements.size
inner class AccessHistoryViewHolder(v: View) : RecyclerView.ViewHolder(v)
{
var accessText: TextView = v.findViewById(R.id.access_text)
lateinit var access: Access
}
}
型
编辑
使用ListView而不是RecyclerView,它可以正常工作:
val accessHistoryLv = view.findViewById(R.id.access_history_lv) as ListView
val elements = accessList.map { elem -> elem.toString() }
val adapter = ArrayAdapter(context, android.R.layout.simple_list_item_1, elements)
accessHistoryLv.adapter = adapter
accessHistoryLv.visibility = View.VISIBLE
型
1条答案
按热度按时间wljmcqd81#
我想问题可能出在这里
字符串
把它换成这个
型
**将示例替换为Access包含的任何模型