android recyclerview刷新问题(firebase firestore)

f5emj3cl  于 2021-06-29  发布在  Java
关注(0)|答案(1)|浏览(481)

我正在从firebase cloud firestore向android recyclerview获取数据。。当我向右滑动项目以删除项目时,它会从数据库中删除项目,但不会刷新recyclerview!也同样在左刷卡。。另外,当我偶然发现数据库中的数据recyclerview不刷新数据时。。。
就这样。。
编辑解决方案(&S)
删除。whereequalto(“tamamlandimi”,false)修复了所有问题。。所以我在firebase中添加了数组,问题就解决了!!
hatirlatmalar.java文件

public class Hatirlatmalar extends Fragment {

    RecyclerView hRVT;
    RCV_HatirlatmalarAdapter hatirlatmalarAdapter;
    FloatingActionButton yEG;
    Button hatirlatmaKaydet;

    TextInputLayout hatirlatmaTarihi, yapilacakIsTipi, kimeYapilacak, yapilacakOlan;
    TextInputEditText hT, yIT;
    Spinner yapilacakIsTipiSpinner;

    ConstraintLayout pBC;

    String hTarihi;

    private DatePickerDialog.OnDateSetListener hatirlatmaTarihiSecici;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View h = inflater.inflate(R.layout.hatirlatmalar, container, false);

        hRVT = h.findViewById(R.id.yapilacaklarTumu);

        hatirlatmalarRefC = fs.collection("Kullanıcılar/" + kullanici.getEmail() + "/Hatırlatmalar");

      hatirlatmalarGetir();

        yEG = h.findViewById(R.id.yEG);
        yEG.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                dialog.setContentView(R.layout.hatirlatma_ekle);

                Objects.requireNonNull(dialog.getWindow()).setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));

                hatirlatmaKaydet = dialog.findViewById(R.id.yapilacakKaydet);
                hatirlatmaTarihi = dialog.findViewById(R.id.hatirlatmaTarihi);
                yapilacakIsTipi = dialog.findViewById(R.id.yapilacakIsTipi);
                kimeYapilacak = dialog.findViewById(R.id.kimeYapilacak);
                yapilacakOlan = dialog.findViewById(R.id.yapilacakOlan);

                hatirlatmaTarihi.getEditText().setText(bugun);

                hT = dialog.findViewById(R.id.hT);
                yIT = dialog.findViewById(R.id.yIT);
                pBC = dialog.findViewById(R.id.pBC);

                yapilacakIsTipiSpinner = dialog.findViewById(R.id.yapilacakIsTipiSpinner);

                hT.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {

                        DatePickerDialog tS = new DatePickerDialog(
                                Objects.requireNonNull(getContext()),
                                android.R.style.Theme_Holo_Dialog_MinWidth,
                                hatirlatmaTarihiSecici,
                                yil, ay, gun);

                        tS.getDatePicker().setMinDate(System.currentTimeMillis() - 1000);
                        Objects.requireNonNull(tS.getWindow()).setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));

                        tS.show();

                    }
                });

                hatirlatmaTarihiSecici = new DatePickerDialog.OnDateSetListener() {

                    @Override
                    public void onDateSet(DatePicker view, int year, int month, int day) {

                        month += 1;

                        String gun, ay;

                        if (month < 10) {

                            ay = "0" + month;

                        } else {

                            ay = String.valueOf(month);
                        }

                        if (day < 10) {

                            gun = "0" + day;

                        } else {

                            gun = String.valueOf(day);

                        }

                        hTarihi = gun + "." + ay + "." + year;

                        hatirlatmaTarihi.getEditText().setText(hTarihi);

                    }

                };

                ArrayAdapter<CharSequence> b = ArrayAdapter.createFromResource(Objects.requireNonNull(getContext()),
                        R.array.yapilacakIsTipi,
                        R.layout.saga_dayali);
                yapilacakIsTipiSpinner.setAdapter(b);

                yIT.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {

                        yapilacakIsTipiSpinner.performClick();

                    }
                });

                yapilacakIsTipiSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
                    @Override
                    public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {

                        yapilacakIsTipi.getEditText().setText(yapilacakIsTipiSpinner.getSelectedItem().toString());

                    }

                    @Override
                    public void onNothingSelected(AdapterView<?> parent) {
                    }
                });

                hatirlatmaKaydet.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {

                        try {
                            yukleniyorAc();

                            hTarihi = hatirlatmaTarihi.getEditText().getText().toString();

                            Date ht = tarihTipi.parse(hTarihi);
                            Date kt = Calendar.getInstance().getTime();
                            String yo = yapilacakOlan.getEditText().getText().toString().trim();
                            String yit = yapilacakIsTipi.getEditText().getText().toString().trim();
                            String ky = kimeYapilacak.getEditText().getText().toString().trim();

                            if (!yo.trim().isEmpty()) {
                                hatirlatmalarRefC.add(new RCV_Hatirlatmalar(false, kt, ht, yo, yit, ky))
                                        .addOnSuccessListener(new OnSuccessListener<DocumentReference>() {
                                            @Override
                                            public void onSuccess(DocumentReference documentReference) {

                                                StyleableToast.makeText(getContext(), getString(R.string.hatirlatma_kaydildi), R.style.onayKisaAlt).show();
                                                dialog.dismiss();

                                            // hatirlatmalarGetir(); I'm DOING THIS TO RELOAD RCV!!!

                                            }
                                        }).addOnFailureListener(new OnFailureListener() {
                                    @Override
                                    public void onFailure(@NonNull Exception e) {

                                        StyleableToast.makeText(getContext(), getString(R.string.hatirlatma_kaydlemedi), R.style.hataKisa).show();

                                    }
                                }).addOnCompleteListener(new OnCompleteListener<DocumentReference>() {
                                    @Override
                                    public void onComplete(@NonNull Task<DocumentReference> task) {
                                        yukleniyorKapa();
                                    }
                                });
                            }

                        } catch (ParseException e) {
                            yukleniyorKapa();
                            e.printStackTrace();
                            StyleableToast.makeText(getContext(), getString(R.string.kayityok_tryc), R.style.hataKisa).show();
                        }

                    }
                });

                dialog.show();
            }
        });

        return h;
    }

    private void hatirlatmalarGetir() {

        Query query = hatirlatmalarRefC.whereEqualTo("tamamlandimi", false).orderBy("hatirlatmaTarihi", Query.Direction.DESCENDING);

        FirestoreRecyclerOptions<RCV_Hatirlatmalar> options = new FirestoreRecyclerOptions.Builder<RCV_Hatirlatmalar>()
                .setQuery(query, RCV_Hatirlatmalar.class)
                .build();

        hatirlatmalarAdapter = new RCV_HatirlatmalarAdapter(options);

        hRVT.setHasFixedSize(true);
        hRVT.setLayoutManager(new LinearLayoutManager(getContext()));
        hRVT.setAdapter(hatirlatmalarAdapter);
        hatirlatmalarAdapter.startListening();

        new ItemTouchHelper(new ItemTouchHelper.SimpleCallback(0, ItemTouchHelper.RIGHT | ItemTouchHelper.LEFT) {
            @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.LEFT) {

                    hatirlatmalarAdapter.hatirlatmaTamamla(viewHolder.getAdapterPosition());
                    hatirlatmalarGetir();

                } else {

                    hatirlatmalarAdapter.hatirlatmaSil(viewHolder.getAdapterPosition());
                    hatirlatmalarGetir();

                }

            }

            @Override
            public void onChildDraw(@NonNull Canvas c, @NonNull RecyclerView recyclerView, @NonNull RecyclerView.ViewHolder viewHolder, float dX, float dY, int actionState, boolean isCurrentlyActive) {

                new RecyclerViewSwipeDecorator.Builder(c, recyclerView, viewHolder, dX, dY, actionState, isCurrentlyActive)
                        .addSwipeRightActionIcon(R.drawable.ic_sil)
                        .addSwipeLeftActionIcon(R.drawable.ic_tamamlandi)
                        .addSwipeRightBackgroundColor(ContextCompat.getColor(getContext(), R.color.kirmizi))
                        .addSwipeLeftBackgroundColor(ContextCompat.getColor(getContext(), R.color.yesil))
                        .addSwipeRightLabel(getString(R.string.hatirlatma_sil))
                        .addSwipeLeftLabel(getString(R.string.hatirlatma_tamamla))
                        .setSwipeRightLabelColor(ContextCompat.getColor(getContext(), R.color.ak))
                        .setSwipeLeftLabelColor(ContextCompat.getColor(getContext(), R.color.ak))
                        .create()
                        .decorate();

                super.onChildDraw(c, recyclerView, viewHolder, dX, dY, actionState, isCurrentlyActive);
            }

        }).attachToRecyclerView(hRVT);

    }

    public void yukleniyorAc() {
        pBC.setVisibility(View.VISIBLE);
    }

    public void yukleniyorKapa() {
        pBC.setVisibility(View.GONE);
    }

    @Override
    public void onStop() {
        super.onStop();
        hatirlatmalarAdapter.stopListening();
    }

    @Override
    public void onStart() {
        super.onStart();
        hatirlatmalarAdapter.startListening();
    }

}

rcvèu hatirlatmalaradapter.java文件

public class RCV_HatirlatmalarAdapter extends FirestoreRecyclerAdapter<RCV_Hatirlatmalar, RCV_HatirlatmalarAdapter.RCV_Hatirlatmalar_Holder> {

    Context context;

    public RCV_HatirlatmalarAdapter(@NonNull FirestoreRecyclerOptions<RCV_Hatirlatmalar> options) {
        super(options);
    }

   /* @Override
    public int getItemCount() {
        return super.getItemCount();
    }
    */

    @Override
    protected void onBindViewHolder(@NonNull RCV_Hatirlatmalar_Holder holder, int position, @NonNull RCV_Hatirlatmalar model) {

        holder.nezaman.setText(tarihTipi.format(model.getHatirlatmaTarihi()));
        holder.ne.setText(model.getYapilacakOlan());
        holder.kime.setText(model.getKimeYapilacak());
        holder.tip.setText(model.getYapilacakIsTipi());

    }

    @NonNull
    @Override
    public RCV_Hatirlatmalar_Holder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.rcv_hatirlatmalar,
                parent, false);

        context = parent.getContext();

        return new RCV_Hatirlatmalar_Holder(v);
    }

    public void hatirlatmaSil(int adapterPosition) {

        getSnapshots().getSnapshot(adapterPosition).getReference().delete();

        StyleableToast.makeText(context, "Silinidi!", R.style.hataKisaAlt).show();

    }

    public void hatirlatmaTamamla(int adapterPosition) {

        final Map<String, Object> h = new HashMap<>();
        h.put("tamamlandimi", true);

        getSnapshots().getSnapshot(adapterPosition).getReference().set(h, SetOptions.merge()).addOnSuccessListener(new OnSuccessListener<Void>() {
            @Override
            public void onSuccess(Void aVoid) {
                StyleableToast.makeText(context, "Tamamlandı!", R.style.onayKisaAlt).show();
            }
        });

    }

    class RCV_Hatirlatmalar_Holder extends RecyclerView.ViewHolder {

        TextView nezaman, ne, kime, tip;

        public RCV_Hatirlatmalar_Holder(@NonNull View itemView) {
            super(itemView);
            nezaman = itemView.findViewById(R.id.nezaman);
            ne = itemView.findViewById(R.id.ne);
            kime = itemView.findViewById(R.id.kime);
            tip = itemView.findViewById(R.id.tip);

        }
    }
}
3yhwsihp

3yhwsihp1#

您没有将数据更改通知适配器。你得打个电话 notifyDataSetChanged() 在适配器内部,当任何数据正在更改时。
或者像这样的特殊情况 notifyItemChanged(position) 或者 notifyItemDeleted(position) .
有关这些方法的更多信息,请向下滚动以根据场景查看完整的方法列表。
在你的情况下,你需要在,

public void hatirlatmaSil(int adapterPosition) {

    getSnapshots().getSnapshot(adapterPosition).getReference().delete();
    notifyItemRemoved(adapterPosition)  ----> Add This 

    StyleableToast.makeText(context, "Silinidi!", R.style.hataKisaAlt).show();

}

相关问题