Android Recyclerview 从数据库获取数据失败
Android Recyclerview fail getting data from database
class ResultRecycleViewAdapter(val mDatabase : PromiseDatabase, val request:Int) : RecyclerView.Adapter<ResultRecycleViewAdapter.ResultViewHolder>() {
var yesList: List<Promise> = ArrayList<Promise>()
val executor = Executors.newSingleThreadExecutor()
var noList: List<Promise> = ArrayList<Promise>()
var mHandler = android.os.Handler()
override fun onAttachedToRecyclerView(recyclerView: RecyclerView) {
super.onAttachedToRecyclerView(recyclerView)
setYesList()
setNoList()
Log.d("RecyclerView", "onAttachedToRecyclerView() 실행")
}
fun setYesList() {
Log.d("RecyclerView", "setYesList() 실행")
executor.execute(Runnable {
val tmp = mDatabase.promiseDao().getSuccessPromise()
Log.d("RecyclerView", ""+tmp )
mHandler.post(
Runnable {
yesList= tmp
Log.d("RecyclerView", "mHandler.Post()"+this.yesList+" "+yesList)
}
)
})
}
fun setNoList() {
Log.d("RecyclerView", "setNoList() 실행")
executor.execute(Runnable {
val tmp = mDatabase.promiseDao().getFailPromise()
Log.d("RecyclerView", ""+tmp)
mHandler.post(
Runnable {
Log.d("RecyclerView", "mHandler.Post()")
noList = tmp
}
)
})
}
inner class ResultViewHolder(parent: ViewGroup) : RecyclerView.ViewHolder
(LayoutInflater.from(parent.context).inflate(R.layout.result_recyclerview, parent, false)) {
val content = parent.findViewById(R.id.content_textView_recyclerView) as TextView
val date = parent.findViewById(R.id.date_textView_recyclerView) as TextView
}
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ResultViewHolder {
Log.d("RecyclerView", "onCreateViewHolder() 실행")
val v =
LayoutInflater.from(parent.context).inflate(R.layout.result_recyclerview, parent, false)
return ResultViewHolder(v as ViewGroup)
}
override fun getItemCount(): Int {
Log.d("RecyclerView", "getItemCount() 실행 noListSize : "+noList.size+" yesListSize : "+yesList.size)
if(request == 0) return noList.size else return yesList.size
}
override fun onBindViewHolder(holder: ResultViewHolder, position: Int) {
Log.d("RecyclerView", "onBindViewHolder() 실행")
Log.d("RecyclerView", yesList.toString() +" "+noList.toString())
if(request == 0){//noList
for(item in noList){
holder.content.text = item.content
holder.date.text = unixTimeToDate(item.date)
}
}
else if(request == 1){
for(item in yesList){
holder.content.text = item.content
holder.date.text = unixTimeToDate(item.date)
}
}
}
fun unixTimeToDate(currentTimeMillis : Int) : String{
Log.d("RecyclerView", "unixTimeToDate() 실행")
val simpleDataFormat : SimpleDateFormat = SimpleDateFormat("yyyy년 MM월 dd일 HH시 mm분 ss초")
return simpleDataFormat.format(currentTimeMillis)
}
}
executor 用于访问房间。 mHandler 用于向mainThread 发送列表数据。
我希望 yesList 和 noList 有项目。但不幸的是他们没有。
getItemCount() 函数只是 returns 0.
但是执行者和处理者做得很好。并且局部变量 tmp 具有精确值。但是给局部成员变量yesList或noList赋tmp值是不行的。
我该怎么办?
您忘记在适配器中调用 notifyDatasetChanged()。
mHandler.post(
Runnable {
yesList= tmp
notifyDataSetChanged() // Should call this to notify adapter
}
)
每次您设置、添加或更新您的列表时,您的适配器都不知道这一点,除非您为 ex 调用适配器 notifyDataSetChanged。通过通知您的适配器,它会调用它的覆盖方法(例如 getItemCount()),从而刷新列表。
class ResultRecycleViewAdapter(val mDatabase : PromiseDatabase, val request:Int) : RecyclerView.Adapter<ResultRecycleViewAdapter.ResultViewHolder>() {
var yesList: List<Promise> = ArrayList<Promise>()
val executor = Executors.newSingleThreadExecutor()
var noList: List<Promise> = ArrayList<Promise>()
var mHandler = android.os.Handler()
override fun onAttachedToRecyclerView(recyclerView: RecyclerView) {
super.onAttachedToRecyclerView(recyclerView)
setYesList()
setNoList()
Log.d("RecyclerView", "onAttachedToRecyclerView() 실행")
}
fun setYesList() {
Log.d("RecyclerView", "setYesList() 실행")
executor.execute(Runnable {
val tmp = mDatabase.promiseDao().getSuccessPromise()
Log.d("RecyclerView", ""+tmp )
mHandler.post(
Runnable {
yesList= tmp
Log.d("RecyclerView", "mHandler.Post()"+this.yesList+" "+yesList)
}
)
})
}
fun setNoList() {
Log.d("RecyclerView", "setNoList() 실행")
executor.execute(Runnable {
val tmp = mDatabase.promiseDao().getFailPromise()
Log.d("RecyclerView", ""+tmp)
mHandler.post(
Runnable {
Log.d("RecyclerView", "mHandler.Post()")
noList = tmp
}
)
})
}
inner class ResultViewHolder(parent: ViewGroup) : RecyclerView.ViewHolder
(LayoutInflater.from(parent.context).inflate(R.layout.result_recyclerview, parent, false)) {
val content = parent.findViewById(R.id.content_textView_recyclerView) as TextView
val date = parent.findViewById(R.id.date_textView_recyclerView) as TextView
}
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ResultViewHolder {
Log.d("RecyclerView", "onCreateViewHolder() 실행")
val v =
LayoutInflater.from(parent.context).inflate(R.layout.result_recyclerview, parent, false)
return ResultViewHolder(v as ViewGroup)
}
override fun getItemCount(): Int {
Log.d("RecyclerView", "getItemCount() 실행 noListSize : "+noList.size+" yesListSize : "+yesList.size)
if(request == 0) return noList.size else return yesList.size
}
override fun onBindViewHolder(holder: ResultViewHolder, position: Int) {
Log.d("RecyclerView", "onBindViewHolder() 실행")
Log.d("RecyclerView", yesList.toString() +" "+noList.toString())
if(request == 0){//noList
for(item in noList){
holder.content.text = item.content
holder.date.text = unixTimeToDate(item.date)
}
}
else if(request == 1){
for(item in yesList){
holder.content.text = item.content
holder.date.text = unixTimeToDate(item.date)
}
}
}
fun unixTimeToDate(currentTimeMillis : Int) : String{
Log.d("RecyclerView", "unixTimeToDate() 실행")
val simpleDataFormat : SimpleDateFormat = SimpleDateFormat("yyyy년 MM월 dd일 HH시 mm분 ss초")
return simpleDataFormat.format(currentTimeMillis)
}
}
executor 用于访问房间。 mHandler 用于向mainThread 发送列表数据。 我希望 yesList 和 noList 有项目。但不幸的是他们没有。 getItemCount() 函数只是 returns 0.
但是执行者和处理者做得很好。并且局部变量 tmp 具有精确值。但是给局部成员变量yesList或noList赋tmp值是不行的。
我该怎么办?
您忘记在适配器中调用 notifyDatasetChanged()。
mHandler.post(
Runnable {
yesList= tmp
notifyDataSetChanged() // Should call this to notify adapter
}
)
每次您设置、添加或更新您的列表时,您的适配器都不知道这一点,除非您为 ex 调用适配器 notifyDataSetChanged。通过通知您的适配器,它会调用它的覆盖方法(例如 getItemCount()),从而刷新列表。