recycleview仅显示最后一行数据

时间:2019-09-05 07:24:26

标签: android kotlin android-recyclerview

我在onCreateVew(片段)中创建了数据加载功能,以使用Google Volley,rxandroid和rxjava从数据库加载数据库来处理回调

我已经按照以下示例操作了此页面:https://android.jlelse.eu/using-recyclerview-in-android-kotlin-722991e86bf3

除此片段外,其他片段工作都很好

数据GET请求结果

[{
    idPenilaian: 1,
    email: 'jnsbstn391@gmail.com',
    tgl: 2019-09-05T03:40:58.000Z,
    hasil: 'Eos 80D' },{
    idPenilaian: 2,
    email: 'jnsbstn391@gmail.com',
    tgl: 2019-09-05T04:03:14.000Z,
    hasil: 'Eos 80D' },{
    idPenilaian: 3,
    email: 'jnsbstn391@gmail.com',
    tgl: 2019-09-05T04:21:00.000Z,
    hasil: 'Eos 80D' },{
    idPenilaian: 4,
    email: 'jnsbstn391@gmail.com',
    tgl: 2019-09-05T04:33:18.000Z,
    hasil: 'a7s' },{
    idPenilaian: 5,
    email: 'jnsbstn391@gmail.com',
    tgl: 2019-09-05T05:18:33.000Z,
    hasil: 'a6000' } ]

HomeFragment.kt

class HomeFragment : Fragment() {
    var menu: RecyclerView? = null
    var bag = CompositeDisposable()

    companion object {

        fun newInstance(): HomeFragment {
            return HomeFragment()
        }
    }

    override fun onDestroy() {
        bag.clear()
        bag.dispose()
        super.onDestroy()
    }


    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        val root = inflater.inflate(R.layout.fragment_home, container, false)

        menu = root.findViewById(R.id.historyView) as RecyclerView

        menu?.layoutManager = LinearLayoutManager(context)
        setUp()
        bindAndFire()
        val a = context
        if(a != null){
            val sharedPref = a.getSharedPreferences("user_login", 0)
            var email: String? = sharedPref.getString("email","")
            val email_temp = email
            if(email_temp != ""){
                historyViewModel.getInstance(a).showHistoryUser(email_temp!!)
            }

        }
        return root
    }

    private fun setUp(){
        val list = listOf<modelHistory>()
        menu?.adapter = cellHistoryUser(list)
    }

    private fun bindAndFire(){
        val a = context
        if(a != null){
            bag.add(historyViewModel.getInstance(a).historyListener.flatMap { response ->
                    response.map { return@map it }.onExceptionResumeNext { Observable.empty<Any>() }
                }.subscribe({
                    menu?.adapter = cellHistoryUser(it)
                },{
                },{}))
        }
    }
}

cellHistoryUser.kt

class cellHistoryUser(private val list:List<modelHistory>):RecyclerView.Adapter<historyPlaceholder>(){
    init {
        this.notifyDataSetChanged()
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): historyPlaceholder {
        val inflater = LayoutInflater.from(parent.context)
        return  historyPlaceholder(inflater,parent)
    }

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

    override fun onBindViewHolder(holder: historyPlaceholder, position: Int) {
        val data:modelHistory = list[position]
        holder.bind(data)
    }


}

class historyPlaceholder(inflater: LayoutInflater, parent: ViewGroup): RecyclerView.ViewHolder(inflater.inflate(R.layout.cell_hirstory,parent,false)){
    private var hasil_history: TextView? = null
    private var tgl_history: TextView? = null
    private var email_history: TextView? = null

    init {
        hasil_history = parent.findViewById(R.id.hasilHistory)
        tgl_history = parent.findViewById(R.id.tglHistory)
        email_history = parent.findViewById(R.id.emailHistory)
    }

    fun bind(input: modelHistory){
        hasil_history?.text = input.hasil
        tgl_history?.text = input.tgl
        email_history?.text = input.email
        itemView.setOnClickListener(View.OnClickListener {
            Log.e("id",input.id.toString())
        })

    }
}

我查看了所有显示在reycleview行中的数据,但实际上我得到的结果是recycleview仅显示了顶行的最后一行数据,而其他空行看起来像这样result tht i not expeted

2 个答案:

答案 0 :(得分:0)

替换

menu?.layoutManager = LinearLayoutManager(context)

TO

linearLayoutManager = LinearLayoutManager(activity)
        linearLayoutManager.orientation = LinearLayoutManager.VERTICAL
        menu?.layoutManager = linearLayoutManager

答案 1 :(得分:0)

一个小时的决赛之后,我找到了答案

更改:

init {
    hasil_history = parent.findViewById(R.id.hasilHistory)
    tgl_history = parent.findViewById(R.id.tglHistory)
    email_history = parent.findViewById(R.id.emailHistory)
}

收件人:

init {
    hasil_history = itemView.findViewById(R.id.hasilHistory)
    tgl_history = itemView.findViewById(R.id.tglHistory)
    email_history = itemView.findViewById(R.id.emailHistory)
}