Home > front end >  Loop through the list in a loop. Android
Loop through the list in a loop. Android

Time:12-31

I have such a task. I want to implement a function like this in my application: I have a list with subscriptions and subscriptions have a subscription price. I understand that I need to implement this through a loop, but I do not know how to do it. I ask the community to help me figure out this problem.

class ListFragment : Fragment() {
    private var _binding: FragmentListBinding? = null
    private val binding get() = _binding!!

    private lateinit var mSubViewModel: SubViewModel

    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?,
    ): View {
        _binding = FragmentListBinding.inflate(inflater, container, false)
        val view = binding.root

        binding.floating.setOnClickListener {
            findNavController().navigate(R.id.action_listFragment_to_addFragment)
        }


        //добавить меню
        setHasOptionsMenu(true)

        //recyclerView
        val adapter = ListAdapter()
        val recyclerview = binding.recycler
        recyclerview.adapter = adapter
        recyclerview.layoutManager = LinearLayoutManager(requireContext())

        //SubViewModel
        mSubViewModel = ViewModelProvider(this).get(SubViewModel::class.java)
        mSubViewModel.readAllData.observe(viewLifecycleOwner, Observer { sub ->
            adapter.setData(sub)
        })


        return view


    }


}
class ListAdapter : RecyclerView.Adapter<ListAdapter.MyViewHolder>() {

    private var subList = emptyList<Subscription>()

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): MyViewHolder {
        val binding = ItemRvBinding
            .inflate(LayoutInflater.from(parent.context), parent, false)
        return MyViewHolder(binding)
    }

    override fun onBindViewHolder(holder: MyViewHolder, position: Int) {
        val currentItem = subList[position]
        with(holder) {
            with(binding) {
                nameTv.text = currentItem.nameSub
                descTv.text = currentItem.descSub
                priceTv.text = currentItem.priceSub
                rowLayout.setOnClickListener {
                    val action =
                        ListFragmentDirections.actionListFragmentToUpdateFragment(currentItem)
                    holder.itemView.findNavController().navigate(action)
                }
            }
        }
    }

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

    fun setData(subscription: List<Subscription>) {
        this.subList = subscription
        notifyDataSetChanged()
    }

    fun total(subscription: List<Subscription>) {
        for (index in subscription) {
            for (two in index.priceSub){

            }

        }

    }

    class MyViewHolder(var binding: ItemRvBinding) : RecyclerView.ViewHolder(binding.root) {

    }


}



CodePudding user response:

You can use for loop to iterate through the subscriptions and calculate total price:

fun total(subscriptions: List<Subscription>): Double {
    var total = 0.0
    for (subscription in subscriptions) {
        total  = subscription.price
    }
    return total
}

Or the more concise way is to use sumOf function for the subscriptions list:

val total = subscriptions.sumOf { it.price }
  • Related