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:
@Nullable @Override public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { Log.d(TAG, "onCreateView: started"); View view = inflater.inflate(R.layout.fragment_projects, container, false); //This inflates the project_fragment layout to this java class. swipeRefreshLayout = view.findViewById(R.id.swipeRefresh); add_btn = view.findViewById(R.id.add_project_btn); add_btn.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { requestNewGroup(); } }); initRecyclerView(view); recyclerUpdater(); swipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() { @Override public void onRefresh() { recyclerUpdater(); swipeRefreshLayout.setRefreshing(false); } }); return view; }
recyclerUpdate function, where Strings are added to the recyclerView adapter:
private void recyclerUpdate() { if(user != null) { firebaseFirestore.collection("users").document(user.getUid()).collection("Groups").get() .addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() { @Override public void onComplete(@NonNull Task<QuerySnapshot> task) { if (task.isSuccessful()) { for (QueryDocumentSnapshot document : task.getResult()) { String title = document.getString(TITLE); String groupId = document.getString(GROUP_ID); mProjects.add(title); mProjectsId.add(groupId); } projectAdapter.setItems(mProjects, mProjectsId, getActivity()); projectAdapter.notifyDataSetChanged(); Log.d(TAG, "onComplete: list: " + mProjects.toString()); } else { Log.d(TAG, "onComplete: error getting documents", task.getException()); } } }); } }
recyclerViewAdapter:
public class groupView extends RecyclerView.Adapter<groupView.ViewHolder>{ private static final String TAG = groupView.class.getSimpleName(); private List<String> mItems = new ArrayList<>(); private List<String> mIds = new ArrayList<>(); private Context mContext; public void setItems(List<String> items, List<String> ids, Context context) { mItems = items; mIds = ids; mContext = context; } @NonNull @Override public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) { if(viewType == 0){ return ViewHolder.inflate(parent); }else{ return null; } } @Override public void onBindViewHolder(@NonNull ViewHolder holder, final int position) { Log.d(TAG, "onBindViewHolder: called"); if (holder instanceof ViewHolder) { ((ViewHolder) holder).bind(mItems.get(position)); ((ViewHolder) holder).mLinearLayout.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { Log.d(TAG, "onClick: clicked on: " + mItems.get(position)); //Sending ID as an intent, to be used in the process Intent intent = new Intent(mContext, projectClicked.class); intent.putExtra("itemPos", mItems.get(position)); intent.putExtra("itemID", mIds.get(position)); //intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); //close activitys before calling a new mContext.startActivity(intent); } }); } } @Override public int getItemCount() { return mItems.size(); } @Override public int getItemViewType(int position) { return 0; } static class ViewHolder extends RecyclerView.ViewHolder{ private TextView mTextView; private LinearLayout mLinearLayout; public static ViewHolder inflate(ViewGroup parent) { View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.layout_listitem_group, parent,false); return new ViewHolder(view); } public ViewHolder(@NonNull View itemView) { super(itemView); mTextView = itemView.findViewById(R.id.list_groups_title); mLinearLayout = itemView.findViewById(R.id.listitem_groups); } public void bind(String text){ mTextView.setText(text); } }
}
Thank you in advance!
Advertisement
Answer
You are adding new items to mProjects
and mProjectsId
not clearing these lists.
Add clear()
before for
loop:
if (task.isSuccessful()) { mProjects.clear(); mProjectsId.clear(); for (QueryDocumentSnapshot document : task.getResult()) { ...