如何更新/刷新 RecyclerView 中选定项的数据

huangapple go评论66阅读模式
英文:

How to update/refresh data of selected item from RecyclerView

问题

I have a recyclerview that its data come from viewModel and database using retrofit. I know how to update all data of recyclerview every time that I want. but when I go to the selected item page using navigation component with selected item data as its argument, I don't know how to update data of this page (using e.g. swiperefresh).

I can update all data, that its output is a List, but I don't know how to find the selected item among the list. It is easy in Array case using index but here it is a List. Any simple solution?

here are some related posts but I couldn't use them:

https://stackoverflow.com/q/9572795/21193451

https://stackoverflow.com/q/4042434/21193451

https://stackoverflow.com/q/32457406/21193451

英文:

I have a recyclerview that its data come from viewModel and database using retrofit. I know how to update all data of recyclerview every time that I want. but when I go to the selected item page using navigation component with selected item data as its argument, I don't know how to update data of this page (using e.g. swiperefresh).

I can update all data, that its output is a List, but I don't know how to find the selected item among the list. It is easy in Array case using index but here it is a List. Any simple solution?

here are some related posts but I couldn't use them:

https://stackoverflow.com/q/9572795/21193451

https://stackoverflow.com/q/4042434/21193451

https://stackoverflow.com/q/32457406/21193451

答案1

得分: 1

你可以使用:

  • For循环

    for(MyClass item: currentList){
      if(item.getId() == yourItem.getId()){
        return item;
      } 
      return null; //未找到项目。
    }
    
  • 或者如果你有RecyclerView中项目的位置,你可以使用: currentList.get(position)

英文:

You can use:

  • For loop

    for(MyClass item: currentList){
      if(item.getId() == yourItem.getId(){
        return item;
      } 
      return null; //Item Not found.
    }
    
  • Or if you have the item position in the recyclerview you can use : currentList.get(position)

huangapple
  • 本文由 发表于 2023年6月6日 15:23:45
  • 转载请务必保留本文链接:https://go.coder-hub.com/76412273.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定