刷卡时如何从Android Studio中的卡片视图回收视图中获取特定数据

时间:2020-06-28 14:24:49

标签: android android-recyclerview swap

我使用回收视图,并且在刷卡删除时需要从卡视图中获取特定数据。就像在刷卡进行删除时,如果我的卡具有名称电子邮件或电话..然后它给我电话号码..我做出一些处理的逻辑,但是我每次都失败。我的逻辑是,如果有人交换了卡,则在适配器类中,调用了delete方法,他们选择交换卡的ID并对此进行了快照。但是它不起作用,并且在快照中为null,因为如果( a == i)首先运行并删除数据形式firebase 我要他先拍摄快照然后删除

我在适配器类中的逻辑

 public void deleteItem(int position) {

    for (int i = 0 ;i <= 1 ; i++) {

      if (a > i) {
        String id = getSnapshots().getSnapshot(condation).getId();

        DocumentReference documentReference = firebaseFirestore.
                collection("Saloon Appointment History")
                .document(FirebaseAuth.getInstance().getCurrentUser().getUid())
                .collection("My Appointment").document(id);

        documentReference.get().addOnCompleteListener(new OnCompleteListener<DocumentSnapshot>() {
          @Override
          public void onComplete(@NonNull Task<DocumentSnapshot> task) {
            if (task.isSuccessful()) {

              DocumentSnapshot document = task.getResult();
              assert document != null;
              Log.d(TAG, "onComplete: " + "hi2");
              phone = (String) document.getString("customerPhone");
              Log.d(TAG, "onComplete: " + phone);

            } else {
              Log.d(TAG, "get failed with ", task.getException());
            }
          }
        });
      }
      if (a == i)
      {
        Log.d(TAG, "onComplete: " + "hi3");
        getSnapshots().getSnapshot(position).getReference().delete();
    
      }
    
      }
    }

AdvancedAppointment

import androidx.annotation.NonNull;
import androidx.appcompat.app.ActionBar;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
import androidx.coordinatorlayout.widget.CoordinatorLayout;
import androidx.core.content.res.ResourcesCompat;
import androidx.recyclerview.widget.ItemTouchHelper;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;

import com.firebase.ui.firestore.FirestoreRecyclerOptions;
import com.google.android.gms.tasks.OnCompleteListener;
import com.google.android.gms.tasks.Task;
import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.firestore.CollectionReference;
import com.google.firebase.firestore.FirebaseFirestore;
import com.google.firebase.firestore.Query;
import com.google.firebase.firestore.QueryDocumentSnapshot;
import com.google.firebase.firestore.QuerySnapshot;

import www.sanju.motiontoast.MotionToast;

public class AdvancedAppointment extends AppCompatActivity {




    AlertDialog.Builder builder;
    private CoordinatorLayout coordinatorLayout;
    private FirebaseAuth firebaseAuth = FirebaseAuth.getInstance();
    FirebaseFirestore db = FirebaseFirestore.getInstance();
    CollectionReference noteRef =  db.collection("Saloon Advanced Appointment ")
            .document(FirebaseAuth.getInstance()
                    .getCurrentUser().getUid()).collection("Appointment");

    private AdvancedAppointmentAdapter advancedAppointmentAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_advanced_appointment);

        Toolbar toolbar = (Toolbar)findViewById(R.id.toolbar1);
        coordinatorLayout = (CoordinatorLayout)findViewById(R.id.coordinatorLayout);
        builder = new AlertDialog.Builder(this);
        setSupportActionBar(toolbar);
        ActionBar actionBar = getSupportActionBar();
        actionBar.setTitle("Appointment Detail ");

        setUpRecyclerView();
    }
    private void setUpRecyclerView() {


        Query query = noteRef.orderBy("date", Query.Direction.DESCENDING);

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

        advancedAppointmentAdapter = new AdvancedAppointmentAdapter(options);

        RecyclerView recyclerView = findViewById(R.id.recycler_view1);
        recyclerView.setHasFixedSize(true);
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        recyclerView.setAdapter(advancedAppointmentAdapter);

        new ItemTouchHelper(new ItemTouchHelper.SimpleCallback(0,
                ItemTouchHelper.LEFT | 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)
            {
                builder.setMessage("Are you sure you want to cancel this appointment").setCancelable(false)
                        .setPositiveButton("Yes", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which)
                            {

                                advancedAppointmentAdapter.deleteItem(viewHolder.getAdapterPosition());


                                MotionToast.Companion.darkToast(AdvancedAppointment.this,"Appointment is Cancel",
                                        MotionToast.TOAST_SUCCESS,
                                        MotionToast.GRAVITY_BOTTOM,
                                        MotionToast.LONG_DURATION,
                                        ResourcesCompat.getFont(AdvancedAppointment.this,R.font.helvetica_regular));
                                CollectionReference collectionOfProducts = db.collection("Saloon Send Message")
                                        .document(FirebaseAuth.getInstance().getCurrentUser().getUid()).collection("My Number");
                                collectionOfProducts

                                        .whereEqualTo("customerPhone","03351306390" )
                                        .get().addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
                                    @Override
                                    public void onComplete(@NonNull Task<QuerySnapshot> task) {

                                        if (task.isSuccessful()) {
                                            for (QueryDocumentSnapshot document : task.getResult()) {
                                                document.getReference().delete();
                                            }
                                            Intent intent = new Intent(AdvancedAppointment.this,SendMessage.class);
                                            finish();
                                            startActivity(intent);


                                        }
                                    }
                                });

                            }
                        })
                        .setNegativeButton("No", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                dialog.cancel();
                                Intent intent = new Intent(AdvancedAppointment.this,Appointment.class);
                                startActivity(intent);
                                finish();
                            }
                        });
                AlertDialog dialog =builder.create();
                dialog.setTitle("Confirmation");
                dialog.show();

            }

        }).attachToRecyclerView(recyclerView);


    }
    @Override
    protected void onStart()
    {
        super.onStart();
        advancedAppointmentAdapter.startListening();
    }

    @Override
    protected void onStop()
    {
        super.onStop();
        advancedAppointmentAdapter.stopListening();
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu)
    {
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.menu_option,menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(@NonNull MenuItem item) {
        switch (item.getItemId())
        {
            case R.id.LogoutOption:
                firebaseAuth.signOut();
                startActivity(new Intent(AdvancedAppointment.this, BeautySalonLogin.class));
                finish();
                return  true;
            case R.id.HistoryOption:
                startActivity(new Intent(AdvancedAppointment.this, History.class));
                finish();
                return  true;
            case R.id.ClearToken:

                builder.setMessage("You want to reset the token").setCancelable(false)
                        .setPositiveButton("Yes", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which)
                            {
                                clearToken();

                            }
                        })
                        .setNegativeButton("No", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                dialog.cancel();
                            }
                        });
                AlertDialog dialog =builder.create();
                dialog.setTitle("Confirmation");
                dialog.show();
                return  true;

            case R.id.ProfileOption:
                startActivity(new Intent(AdvancedAppointment.this, PersonlInfo.class));
                finish();
                return true;

        }
        return super.onOptionsItemSelected(item);
    }
    public void clearToken()
    {
        CollectionReference collectionReference = db.collection("Saloon Token")
                .document(FirebaseAuth.getInstance().getCurrentUser().getUid()).collection("Tokens");
        collectionReference.get().addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
            @Override
            public void onComplete(@NonNull Task<QuerySnapshot> task) {
                if (task.isSuccessful()) {
                    for (QueryDocumentSnapshot document : task.getResult())
                    {
                        document.getReference().delete();
                    }

                    MotionToast.Companion.createToast(AdvancedAppointment.this, "Token is clear",
                            MotionToast.TOAST_SUCCESS,
                            MotionToast.GRAVITY_BOTTOM,
                            MotionToast.LONG_DURATION,
                            ResourcesCompat.getFont(AdvancedAppointment.this, R.font.helvetica_regular));
                    // Snackbar.make(coordinatorLayout,"All token is clear" ,Snackbar.LENGTH_LONG).show();
                    CollectionReference collectionReference = db
                            .collection("Saloon Estimated Time")
                            .document(FirebaseAuth.getInstance().getCurrentUser().getUid())
                            .collection("Time");
                    collectionReference.get().addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
                        @Override
                        public void onComplete(@NonNull Task<QuerySnapshot> task)
                        {
                            for (QueryDocumentSnapshot documentSnapshot:task.getResult())
                            {
                                documentSnapshot.getReference().delete();
                            }
                        }
                    });

                } else {
                }
            }
        });
    }

}

AdvancedAppointmentAdapter

import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.firebase.ui.firestore.FirestoreRecyclerAdapter;
import com.firebase.ui.firestore.FirestoreRecyclerOptions;
import com.google.android.gms.tasks.OnCompleteListener;
import com.google.android.gms.tasks.OnSuccessListener;
import com.google.android.gms.tasks.Task;
import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.firestore.DocumentReference;
import com.google.firebase.firestore.DocumentSnapshot;
import com.google.firebase.firestore.FirebaseFirestore;

public class AppointmentAdapter extends FirestoreRecyclerAdapter<AppointmentClass, AppointmentAdapter.AppointmentHolder> {

  public static  String phone;
  FirebaseFirestore firebaseFirestore = FirebaseFirestore.getInstance();
  private static final String TAG = "Dashboard";
  int condation ;
  int a = 1;

  public AppointmentAdapter(@NonNull FirestoreRecyclerOptions<AppointmentClass> options)
  {
    super(options);
  }

  @Override
  protected void onBindViewHolder(@NonNull AppointmentHolder holder, int i, @NonNull AppointmentClass model) {


    holder.textViewToken.setText(model.getToken());
    holder.textViewTime.setText(model.getTime());
    holder.textViewDate.setText(model.getDate());
    holder.textViewCustomerName.setText(model.getCustomerName());
    holder.textViewCustomerPhone.setText(model.getCustomerPhone());


    for (String service :model.getService())
    {
      holder.textViewService.append(service);
      holder.textViewService.append("\n");
    }

  }

  @NonNull
  @Override
  public AppointmentHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
    View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.history_item_cards,
            parent,false);
    return new AppointmentHolder(v);
  }

  public void deleteItem(int position) {
    
      String id = getSnapshots().getSnapshot(condation).getId();

      DocumentReference documentReference = firebaseFirestore.
              collection("Saloon Appointment History")
              .document(FirebaseAuth.getInstance().getCurrentUser().getUid())
              .collection("My Appointment").document(id);

      documentReference.get().addOnCompleteListener(new OnCompleteListener<DocumentSnapshot>() {
        @Override
        public void onComplete(@NonNull Task<DocumentSnapshot> task) {
          if (task.isSuccessful())
          {
         
            DocumentSnapshot document = task.getResult();
            assert document != null;
            
            phone = (String) document.getString("customerPhone");
            Log.d(TAG, "onComplete: " + phone);
            getSnapshots().getSnapshot(position).getReference().delete();
        



          } else {
            Log.d(TAG, "get failed with ", task.getException());
          }
        }
      });
    }

  class AppointmentHolder extends RecyclerView.ViewHolder {


    TextView textViewToken;
    TextView textViewTime;
    TextView textViewDate;
    TextView textViewCustomerName;
    TextView textViewCustomerPhone;
    TextView textViewService;

    public AppointmentHolder(View itemView)
    {
      super(itemView);
      textViewToken    = itemView.findViewById(R.id.text_view_Token);
      textViewTime     = itemView.findViewById(R.id.text_view_Time);
      textViewDate     = itemView.findViewById(R.id.text_view_Date);
      textViewCustomerName = itemView.findViewById(R.id.text_view_CustomerName);
      textViewCustomerPhone= itemView.findViewById(R.id.text_view_CustomerPhone);
      textViewService  = itemView.findViewById(R.id.text_view_Service);
    }
  }
 
}

AdvancedClass

public class AdvancedClass {

    private String time;
    private String date;
    private String customerName;
    private String customerPhone;

    public  AdvancedClass()
    {

    }

    public String getTime() {
        return time;
    }

    public String getDate() {
        return date;
    }

    public String getCustomerName() {
        return customerName;
    }

    public String getCustomerPhone() {
        return customerPhone;
    }
}

0 个答案:

没有答案