RecyclerView在刷新时多次上传项目。

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

recyclerview uploads items multiple time on refresh

问题

我已经实现了一个RecyclerView,在其中我想展示应用程序中创建的群组。目前我正在使用swipeRefreshLayout刷新RecyclerView,但每次刷新RecyclerView时,它会根据刷新的次数多次打印出相同的群组。

有谁可以帮助我解决这个问题?

以下是代码部分。

onCreateView 方法:

  1. @Nullable
  2. @Override
  3. public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
  4. // ... (你的其他代码)
  5. swipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
  6. @Override
  7. public void onRefresh() {
  8. recyclerUpdater();
  9. swipeRefreshLayout.setRefreshing(false);
  10. }
  11. });
  12. return view;
  13. }

recyclerUpdate 函数,用于向 RecyclerView 适配器中添加字符串:

  1. private void recyclerUpdate() {
  2. if(user != null) {
  3. firebaseFirestore.collection("users").document(user.getUid()).collection("Groups").get()
  4. .addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
  5. @Override
  6. public void onComplete(@NonNull Task<QuerySnapshot> task) {
  7. if (task.isSuccessful()) {
  8. mProjects.clear(); // 清空数据列表
  9. mProjectsId.clear(); // 清空 ID 列表
  10. for (QueryDocumentSnapshot document : task.getResult()) {
  11. String title = document.getString(TITLE);
  12. String groupId = document.getString(GROUP_ID);
  13. mProjects.add(title);
  14. mProjectsId.add(groupId);
  15. }
  16. projectAdapter.setItems(mProjects, mProjectsId, getActivity());
  17. projectAdapter.notifyDataSetChanged();
  18. Log.d(TAG, "onComplete: list: " + mProjects.toString());
  19. } else {
  20. Log.d(TAG, "onComplete: error getting documents", task.getException());
  21. }
  22. }
  23. });
  24. }
  25. }

这是 RecyclerView 适配器部分:

  1. public class groupView extends RecyclerView.Adapter<groupView.ViewHolder> {
  2. // ... (你的其他代码)
  3. public void setItems(List<String> items, List<String> ids, Context context) {
  4. mItems = items;
  5. mIds = ids;
  6. mContext = context;
  7. notifyDataSetChanged(); // 更新数据后通知适配器更新UI
  8. }
  9. // ... (你的其他代码)
  10. }

提前感谢你的帮助!

英文:

I have implemented a recyclerview, where I want to show the groups that have been created in the app. Right now I am refreshing the recyclerview using the swipeRefreshLayout, but everytime I refresh the recyclerview, it prints out the same group multiple times according to how many times I refresh it.

Can anyone help me solve this problem?

Below is the code.

onCreateView method:

  1. @Nullable
  2. @Override
  3. public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
  4. Log.d(TAG, &quot;onCreateView: started&quot;);
  5. View view = inflater.inflate(R.layout.fragment_projects, container, false); //This inflates the project_fragment layout to this java class.
  6. swipeRefreshLayout = view.findViewById(R.id.swipeRefresh);
  7. add_btn = view.findViewById(R.id.add_project_btn);
  8. add_btn.setOnClickListener(new View.OnClickListener() {
  9. @Override
  10. public void onClick(View view) {
  11. requestNewGroup();
  12. }
  13. });
  14. initRecyclerView(view);
  15. recyclerUpdater();
  16. swipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
  17. @Override
  18. public void onRefresh() {
  19. recyclerUpdater();
  20. swipeRefreshLayout.setRefreshing(false);
  21. }
  22. });
  23. return view;
  24. }

recyclerUpdate function, where Strings are added to the recyclerView adapter:

  1. private void recyclerUpdate() {
  2. if(user != null) {
  3. firebaseFirestore.collection(&quot;users&quot;).document(user.getUid()).collection(&quot;Groups&quot;).get()
  4. .addOnCompleteListener(new OnCompleteListener&lt;QuerySnapshot&gt;() {
  5. @Override
  6. public void onComplete(@NonNull Task&lt;QuerySnapshot&gt; task) {
  7. if (task.isSuccessful()) {
  8. for (QueryDocumentSnapshot document : task.getResult()) {
  9. String title = document.getString(TITLE);
  10. String groupId = document.getString(GROUP_ID);
  11. mProjects.add(title);
  12. mProjectsId.add(groupId);
  13. }
  14. projectAdapter.setItems(mProjects, mProjectsId, getActivity());
  15. projectAdapter.notifyDataSetChanged();
  16. Log.d(TAG, &quot;onComplete: list: &quot; + mProjects.toString());
  17. } else {
  18. Log.d(TAG, &quot;onComplete: error getting documents&quot;, task.getException());
  19. }
  20. }
  21. });
  22. }
  23. }

recyclerViewAdapter:

  1. public class groupView extends RecyclerView.Adapter&lt;groupView.ViewHolder&gt;{
  2. private static final String TAG = groupView.class.getSimpleName();
  3. private List&lt;String&gt; mItems = new ArrayList&lt;&gt;();
  4. private List&lt;String&gt; mIds = new ArrayList&lt;&gt;();
  5. private Context mContext;
  6. public void setItems(List&lt;String&gt; items, List&lt;String&gt; ids, Context context) {
  7. mItems = items;
  8. mIds = ids;
  9. mContext = context;
  10. }
  11. @NonNull
  12. @Override
  13. public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
  14. if(viewType == 0){
  15. return ViewHolder.inflate(parent);
  16. }else{
  17. return null;
  18. }
  19. }
  20. @Override
  21. public void onBindViewHolder(@NonNull ViewHolder holder, final int position) {
  22. Log.d(TAG, &quot;onBindViewHolder: called&quot;);
  23. if (holder instanceof ViewHolder) {
  24. ((ViewHolder) holder).bind(mItems.get(position));
  25. ((ViewHolder) holder).mLinearLayout.setOnClickListener(new View.OnClickListener() {
  26. @Override
  27. public void onClick(View view) {
  28. Log.d(TAG, &quot;onClick: clicked on: &quot; + mItems.get(position));
  29. //Sending ID as an intent, to be used in the process
  30. Intent intent = new Intent(mContext, projectClicked.class);
  31. intent.putExtra(&quot;itemPos&quot;, mItems.get(position));
  32. intent.putExtra(&quot;itemID&quot;, mIds.get(position));
  33. //intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); //close activitys before calling a new
  34. mContext.startActivity(intent);
  35. }
  36. });
  37. }
  38. }
  39. @Override
  40. public int getItemCount() {
  41. return mItems.size();
  42. }
  43. @Override
  44. public int getItemViewType(int position) {
  45. return 0;
  46. }
  47. static class ViewHolder extends RecyclerView.ViewHolder{
  48. private TextView mTextView;
  49. private LinearLayout mLinearLayout;
  50. public static ViewHolder inflate(ViewGroup parent) {
  51. View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.layout_listitem_group, parent,false);
  52. return new ViewHolder(view);
  53. }
  54. public ViewHolder(@NonNull View itemView) {
  55. super(itemView);
  56. mTextView = itemView.findViewById(R.id.list_groups_title);
  57. mLinearLayout = itemView.findViewById(R.id.listitem_groups);
  58. }
  59. public void bind(String text){
  60. mTextView.setText(text);
  61. }
  62. }

}

Thank you in advance!

答案1

得分: 0

你正在向 mProjectsmProjectsId 添加新项,而没有清空这些列表。
for 循环之前添加 clear()

  1. if (task.isSuccessful()) {
  2. mProjects.clear();
  3. mProjectsId.clear();
  4. for (QueryDocumentSnapshot document : task.getResult()) {
  5. ...
英文:

You are adding new items to mProjects and mProjectsId not clearing these lists.
Add clear() before for loop:

  1. if (task.isSuccessful()) {
  2. mProjects.clear();
  3. mProjectsId.clear();
  4. for (QueryDocumentSnapshot document : task.getResult()) {
  5. ...

huangapple
  • 本文由 发表于 2020年10月17日 00:12:06
  • 转载请务必保留本文链接:https://go.coder-hub.com/64392776.html
匿名

发表评论

匿名网友

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

确定