F
F
foonfyrick2020-11-22 12:54:23
Android
foonfyrick, 2020-11-22 12:54:23

Why does resail view display only one item and overwrite it?

I can’t figure it out for 2 hours, if I manually fill in the array in onCreate, then several items appear, and if I add through the button, then the same one is always overwritten, if I go to another activity and return, then everything disappears altogether

//Когда возвращаюсь в мэйн, получаю эти значения так:
//getArgs() возвращает navArgs

        val args = getArgs()
        if (args!=null){
            list.add(ModelList(args.header,args.body))
        }

        val recyclerView = idrecyclerView
        recyclerView.layoutManager = LinearLayoutManager(
            requireContext(),
            LinearLayoutManager.VERTICAL,
            false
        )

        val adapter = AdapterCustom(list)
        recyclerView.adapter = adapter

//адаптер
class AdapterCustom(val listOfItems:List<ModelList>):RecyclerView.Adapter<AdapterCustom.ViewHolder>() {
    class ViewHolder(itemView:View):RecyclerView.ViewHolder(itemView) {
        var header = itemView.item_tvHeader
        var body = itemView.item_tvBody
    }

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

    override fun getItemCount():Int = listOfItems.size

    override fun onBindViewHolder(holder: ViewHolder, position: Int) {
        val modelList = listOfItems[position]
        holder.header.text = modelList.header
        holder.body.text = modelList.body
    }
}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
F
foonfyrick, 2020-11-22
@foonfyrick

The error was that the receipt of arguments (navArgs) had to be moved to onCreate, because, with onResume, the same object was passed each time, in addition to this list, it was necessary to create a singleton, because, every time, when folding and expanding application worked onCreate and, accordingly, an instance of the list was re-created.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question