将对象从一个recyclerview移动到另一个recyclerview

mznpcxlj  于 2021-08-25  发布在  Java
关注(0)|答案(1)|浏览(433)

我目前正在为一些足球场制作一个预订应用程序。我有两个回收者视图:活动预订和取消预订。我还有一个自定义的回收器视图适配器。两个回收器视图使用相同的适配器。
我想做的是:如果我点击我的活动预订回收器视图中的按钮,我想从活动预订中删除预订并将其放入我的取消预订中。我将留下我的自定义适配器,并提到我使用firebase以获得我的预订状态(活动或取消)。

public class RecyclerViewAdapterRezervare extends RecyclerView.Adapter<RecyclerViewAdapterRezervare.RecycleViewHolder> {
    private List<Rezervare> rezervari;
    private Context context;
    private LayoutInflater inflater;
    private String numeUtilizator;
    private String status;

    private Rezervare rezervare = new Rezervare();

    private DatabaseReference reff;

    public RecyclerViewAdapterRezervare(List<Rezervare> rezervari, Context context, String numeUtilizator) {
        this.rezervari = rezervari;
        this.context = context;
        this.numeUtilizator = numeUtilizator;
        this.inflater = LayoutInflater.from(context);
    }

    @NonNull
    @Override
    public RecycleViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = inflater.inflate(R.layout.rezervari_layout, parent, false);
        return new RecycleViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull RecycleViewHolder holder, int position) {
        rezervare = rezervari.get(position);

        reff = FirebaseDatabase.getInstance().getReference();

        List<String> listaOre = rezervare.getOre();
        Map<String, Object> mapOre = new HashMap<>();
        for(String ora : listaOre){
            mapOre.put(ora, false);
        }

        // this will return the status ("activa" is the booking is active, or "anulata" is it's cancelled)
        getStatus(rezervare, new StatusCallback() {
            @Override
            public void onCallback(String value) {
                status = value;

                holder.btn_anuleaza.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        Log.v("status", status);
                        new AlertDialog.Builder(view.getContext()).setTitle("Anulare rezervare").setMessage("Sunteti sigur ca doriti anularea rezervarii?")
                            .setPositiveButton(R.string.da, new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialogInterface, int i) {

// if the cancel button is clicked, i update my database so the status of the booking will be "anulata" - cancelled                                reff.child("Users").child(numeUtilizator).child("rezervari").child(rezervare.getData()).child(rezervare.getNumeTeren()).child("status").setValue("anulata");
                                reff.child("Rezervari").child(rezervare.getData()).child("Fotbal").child(rezervare.getNumeTeren()).updateChildren(mapOre);
                                rezervare.setEsteAnulata(false);
                                Toast.makeText(view.getContext(), "Anulare cu succes!", Toast.LENGTH_LONG).show();

                            }
                        }).setNegativeButton(R.string.nu, null).show();

                    }
                });

            }
        });

    }

    @Override
    public int getItemCount() {
        return rezervari.size();
    }

    // i have updated my code with the delete and insert a booking
    public void stergeRezervare(int position){
        rezervari.remove(position);
        notifyDataSetChanged();
    }

    public void adaugaRezervare(Rezervare rezervare){
        rezervari.add(rezervare);
        notifyDataSetChanged();
    }

这是从firebase获取预订信息的功能。我有3个单独的列表,分别用于活动预订、取消预订和过去预订。

public interface RezervariListCallback {
        void onCallback(List<Rezervare> rezervariActuale, List<Rezervare> rezervariTrecute, List<Rezervare> rezervariAnulate);
    }

    public void getRezervari(String nume, RezervariListCallback myCallback){
        reff.child("Users").child(nume).child("rezervari").addListenerForSingleValueEvent(new ValueEventListener() {
            @Override
            public void onDataChange(@NonNull DataSnapshot snapshot) {
                for(DataSnapshot data : snapshot.getChildren()){
                    String dataRezervare = data.getKey();
                    dateRezervari.add(dataRezervare);

                    reff.child("Users").child(nume).child("rezervari").child(dataRezervare).addListenerForSingleValueEvent(new ValueEventListener() {
                       @Override
                       public void onDataChange(@NonNull DataSnapshot snapshot) {
                            for(DataSnapshot data : snapshot.getChildren()){
                                String numeTeren = data.getKey();

                                reff.child("Users").child(nume).child("rezervari").child(dataRezervare).child(numeTeren).child("ore").addValueEventListener(new ValueEventListener() {
                                    @Override
                                    public void onDataChange(@NonNull DataSnapshot snapshot) {
                                        List<String> ore = new ArrayList<>();
                                        for(DataSnapshot data : snapshot.getChildren()){
                                            String ora = data.getKey();
                                            ore.add(ora);
                                        }

                                        getAdresa(nume, dataRezervare, numeTeren, new AdresaCallback() {
                                            @Override
                                            public void onCallbackAdresa(String value) {
                                                Rezervare rezervare = new Rezervare(numeTeren, ore, dataRezervare, value);
                                                String dataAzi = getDataAzi();

                                                reff.child("Users").child(nume).child("rezervari").child(dataRezervare).child(numeTeren).child("status").addValueEventListener(new ValueEventListener() {
                                                    @Override
                                                    public void onDataChange(@NonNull DataSnapshot snapshot) {
                                                        String status = (String) snapshot.getValue();

                                                        if(comparaData(dataAzi, rezervare.getData())){
                                                            if(status.equals("activa")){
                                                                rezervariActive.add(rezervare);
                                                            } else {
                                                                rezervariAnulate.add(rezervare);
                                                            }
                                                        } else {
                                                            rezervariTrecute.add(rezervare);
                                                        }

                                                        myCallback.onCallback(rezervariActive, rezervariTrecute, rezervariAnulate);
                                                    }

                                                    @Override
                                                    public void onCancelled(@NonNull DatabaseError error) {

                                                    }
                                                });
                                            }
                                        });

                                    }

                                    @Override
                                    public void onCancelled(@NonNull DatabaseError error) {

                                    }
                                });
                            }
                       }

                       @Override
                       public void onCancelled(@NonNull DatabaseError error) {
                       }
                    });
                }
            }

            @Override
            public void onCancelled(@NonNull DatabaseError error) {

            }
        });
    }

这就是我设置适配器的地方

public void updateRezervari(View view, RecyclerView activeBookingsRecycler, RecyclerView pastBookingsRecycler, RecyclerView cancelledBookingsRecycler){
        AsyncTask.execute(new Runnable() {
            @Override
            public void run() {
                getRezervari(numeUtilizator, new RezervariListCallback() {
                    @Override
                    public void onCallback(List<Rezervare> rezervariActuale, List<Rezervare> rezervariTrecute, List<Rezervare> rezervariAnulate) {
                        if(rezervariActuale.size()>0){
                            tvFaraRezervariActive.setEnabled(false);
                            tvFaraRezervariAnulate.setEnabled(false);
                            tvFaraRezervariTrecute.setEnabled(false);
                        }

                        Log.v("rezAc", rezervariActuale.toString());
                        Log.v("rezTr", rezervariTrecute.toString());

                        RecyclerViewAdapterRezervare adapter = new RecyclerViewAdapterRezervare(rezervariActuale, view.getContext(), numeUtilizator);
                        activeBookingsRecycler.setAdapter(adapter);
                        adapter = new RecyclerViewAdapterRezervare(rezervariTrecute, view.getContext(), numeUtilizator);
                        pastBookingsRecycler.setAdapter(adapter);
                        adapter = new RecyclerViewAdapterRezervare(rezervariAnulate, view.getContext(), numeUtilizator);
                        cancelledBookingsRecycler.setAdapter(adapter);
                    }
                });
            }
        });
    }

你能给我一些关于如何从一个回收者的观点转移到另一个的建议吗?非常感谢。

5lhxktic

5lhxktic1#

由于您正在使用firebase获取预订的状态,您可以在firebase数据库中将状态更新为取消,然后您将从firebase侦听器获得一个回调,您可以在其中更新ArrayList并调用notifydatasetchanged()。

相关问题