分享
三行代码  ›  专栏  ›  技术社区  ›  임세준

Android Recyclerview无法从数据库获取数据

  •  0
  • 임세준  · 技术社区  · 1 周前
    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)
        }
    
    
    }
    

    我以为是的和不是的都有东西。但不幸的是他们没有。 函数的作用是:返回0。

    但是执行者和处理者做得很好。而本地可变tmp有确切的值。但将tmp值赋给局部成员变量yesList或noList是行不通的。

    1 回复  |  直到 1 周前
        1
  •  2
  •   elbert rivas    1 周前

    你忘了打电话 在适配器中。

    mHandler.post(
          Runnable {
    
             yesList=  tmp
              notifyDataSetChanged() // Should call this to notify adapter
           }
     )