У меня есть фрагмент, названный сегодня, в котором я получаю все задачи за текущий день. Но когда я добавляю элемент в пожарный магазин с текущим днем, и это первая задача, которая добавляется в пожарный магазин, задача теперь отображается во фрагменте, когда я добавляю, отображается вторая задача, а третья - одни и те же. Но первое задание никогда не показывается, знаете ли вы, почему делаете это?
Это фрагмент сегодня:
public class TodayFragment extends Fragment {
private View rootView;
private TodayAdaptor todayAdaptor;
private ColorDrawable swipeBackgroundRight = new ColorDrawable(Color.parseColor("#FF0000"));
private Drawable iconDelete;
private FirebaseFirestore firestore = FirebaseFirestore.getInstance();
private CollectionReference collectionReference;
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
if (getActivity() != null) {
getActivity().setTitle(R.string.today);
}
collectionReference = firestore.collection("Users")
.document(FirebaseAuth.getInstance().getCurrentUser().getUid())
.collection("Tasks");
iconDelete = ContextCompat.getDrawable(getActivity(), R.drawable.ic_delete);
rootView = LayoutInflater.from(getActivity()).inflate(R.layout.fragment_today, container, false);
RecyclerView recyclerViewToday = rootView.findViewById(R.id.recycler_view_today);
/* create the options for query */
Calendar calendar = Calendar.getInstance();
String dateFormatted = java.text.DateFormat.getDateInstance(java.text.DateFormat.FULL).format(calendar.getTime());
Query query = collectionReference.whereEqualTo("date", dateFormatted);
FirestoreRecyclerOptions<Task> options =
new FirestoreRecyclerOptions.Builder<Task>()
.setQuery(query, Task.class)
.build();
todayAdaptor = new TodayAdaptor(options);
recyclerViewToday.setHasFixedSize(true);
recyclerViewToday.setLayoutManager(new LinearLayoutManager(getActivity()));
recyclerViewToday.setAdapter(todayAdaptor);
new ItemTouchHelper(new ItemTouchHelper.SimpleCallback(0, ItemTouchHelper.RIGHT) {
@Override
public boolean onMove(@NonNull RecyclerView recyclerView, @NonNull RecyclerView.ViewHolder viewHolder, @NonNull RecyclerView.ViewHolder target) {
return false;
}
@Override
public void onSwiped(@NonNull RecyclerView.ViewHolder viewHolder, int direction) {
if (direction == ItemTouchHelper.RIGHT) {
todayAdaptor.deleteTask(viewHolder.getAdapterPosition());
Toast.makeText(getActivity(), "Task deleted", Toast.LENGTH_SHORT).show();
}
}
@Override
public void onChildDraw(@NonNull Canvas c, @NonNull RecyclerView recyclerView, @NonNull RecyclerView.ViewHolder viewHolder, float dX, float dY, int actionState, boolean isCurrentlyActive) {
View itemView = viewHolder.itemView;
int iconMargin = (itemView.getHeight() - iconDelete.getIntrinsicHeight()) / 2;
if (dX > 0) {
swipeBackgroundRight.setBounds(itemView.getLeft(), itemView.getTop(), (int) dX, itemView.getBottom());
iconDelete.setBounds(itemView.getLeft() + iconMargin, itemView.getTop() + iconMargin, itemView.getLeft() + iconMargin + iconDelete.getIntrinsicWidth(), itemView.getBottom() - iconMargin);
} else {
swipeBackgroundRight.setBounds(0, 0, 0, 0);
}
c.save();
swipeBackgroundRight.draw(c);
if (dX > 0) {
c.clipRect(itemView.getLeft(), itemView.getTop(), (int) dX, itemView.getBottom());
} else {
c.clipRect(itemView.getRight() + (int) dX, itemView.getTop(), itemView.getRight(), itemView.getBottom());
}
iconDelete.draw(c);
c.restore();
super.onChildDraw(c, recyclerView, viewHolder, dX, dY, actionState, isCurrentlyActive);
}
}).attachToRecyclerView(recyclerViewToday);
return rootView;
}
@Override
public void onStart() {
super.onStart();
todayAdaptor.startListening();
}
@Override
public void onStop() {
super.onStop();
todayAdaptor.stopListening();
}
}
Это мой адаптер:
public class TodayAdaptor extends FirestoreRecyclerAdapter<Task, TodayAdaptor.TodayViewHolder> {
public TodayAdaptor(@NonNull FirestoreRecyclerOptions<Task> options) {
super(options);
}
@Override
protected void onBindViewHolder(@NonNull TodayViewHolder holder, int position, @NonNull Task model) {
holder.todayCheckBox.setChecked(model.isCompleted());
holder.taskDescription.setText(model.getName());
}
@NonNull
@Override
public TodayViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.today_item, parent, false);
return new TodayViewHolder(view);
}
public void deleteTask(int position){
/* save the item to be
restored if the user want's so
*/
getSnapshots().getSnapshot(position).getReference().delete();
}
class TodayViewHolder extends RecyclerView.ViewHolder {
private CheckBox todayCheckBox;
private TextView taskDescription;
public TodayViewHolder(@NonNull View itemView) {
super(itemView);
todayCheckBox = itemView.findViewById(R.id.today_item_check_box);
taskDescription = itemView.findViewById(R.id.today_item_task_description);
}
}
}