Firestore回收站视图

时间:2020-06-12 06:04:06

标签: android google-cloud-firestore android-recyclerview

大家好,我已经使用RecyclerView适配器从Firestore提取数据。现在,我希望在最初获取10条记录之后,如果用户向下滑动到最后一条记录,则当我进一步向下移动时,它应该从FIrestore数据库中获取接下来的几条记录,例如2或3。 我不知道如何设置该属性以及在哪里设置该属性。 ViewHolder类和ViewAdapter类写在下面。最初,我在给定Collection下获取整个数据。 欢迎任何建议。

    package com.example.merchant;

    import android.view.LayoutInflater;
    import android.view.View;
    import android.view.ViewGroup;

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

    import java.util.ArrayList;

    public class RecyclerViewAdapter extends RecyclerView.Adapter<RecyclerViewHolder> {

        public RecyclerViewAdapter(FetchPastOrders fetchPastOrders, ArrayList<PastOrders> pastOrders) {
            this.fetchPastOrders = fetchPastOrders;
            this.pastOrders = pastOrders;
        }



        public FetchPastOrders getFetchPastOrders() {
            return fetchPastOrders;
        }

        public void setFetchPastOrders(FetchPastOrders fetchPastOrders) {
            this.fetchPastOrders = fetchPastOrders;
        }

        public ArrayList<PastOrders> getPastOrders() {
            return pastOrders;
        }

        public void setPastOrders(ArrayList<PastOrders> pastOrders) {
            this.pastOrders = pastOrders;
        }

        FetchPastOrders fetchPastOrders;
       ArrayList<PastOrders> pastOrders;

        @NonNull
        @Override
        public RecyclerViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
            LayoutInflater layoutInflater=LayoutInflater.from(fetchPastOrders.getBaseContext());
            View view=layoutInflater.inflate(R.layout.singleorderfetch,parent,false);


            return new RecyclerViewHolder(view);
        }

        @Override
        public void onBindViewHolder(@NonNull RecyclerViewHolder holder, int position) {

            holder.Name.setText("Name : "+pastOrders.get(position).getName());
            holder.Location.setText("Location : "+pastOrders.get(position).getLocation());
            holder.CID.setText("CID : "+pastOrders.get(position).getCID());
            holder.Amount.setText("Amount : "+pastOrders.get(position).getAmount());
            holder.Date.setText("Date : "+pastOrders.get(position).getDate());

        }

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






    package com.example.merchant;

    import androidx.annotation.NonNull;
    import androidx.appcompat.app.AppCompatActivity;
    import androidx.recyclerview.widget.LinearLayoutManager;
    import androidx.recyclerview.widget.RecyclerView;

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

    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.OnFailureListener;
    import com.google.android.gms.tasks.OnSuccessListener;
    import com.google.android.gms.tasks.Task;
    import com.google.firebase.firestore.DocumentReference;
    import com.google.firebase.firestore.DocumentSnapshot;
    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 java.util.ArrayList;

    public class FetchPastOrders extends AppCompatActivity {

        TextView Name;
        TextView Amount;
        TextView CID;
        TextView Location;
        TextView orderId;
        RecyclerView recyclerView;
        private RecyclerViewAdapter adapter;
        FirebaseFirestore firebaseFirestore;
        ArrayList<PastOrders> pastOrdersList = new ArrayList<>();


        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_fetch_past_orders);
            Name = findViewById(R.id.Name);
            Amount = findViewById(R.id.Amount);
            CID = findViewById(R.id.CID);
            orderId = findViewById(R.id.orderId);
            Location = findViewById(R.id.location);
            setFireStore();
            setUpRecycleView();
            loadDataFromFirebase();


        }


        private void setFireStore() {
            firebaseFirestore = FirebaseFirestore.getInstance();
        }

        private void setUpRecycleView() {
            recyclerView = findViewById(R.id.recyclerView);
            recyclerView.setHasFixedSize(true);
            recyclerView.setLayoutManager(new LinearLayoutManager(this));


        }

        private void loadDataFromFirebase() {

            firebaseFirestore.collection("Orders").orderBy("date", Query.Direction.DESCENDING)
                    .get()
                    .addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
                        @Override
                        public void onComplete(@NonNull Task<QuerySnapshot> task) {
                            for (DocumentSnapshot querySnapshots : task.getResult()) {
                                PastOrders pastOrders = new PastOrders(querySnapshots.getString("name"),
                                        querySnapshots.getString("location"),
                                        querySnapshots.getString("CID"),
                                        querySnapshots.getString("Amount"),
                                        querySnapshots.getDate("date")

                                );
                                pastOrdersList.add(pastOrders);
                            }
                            adapter = new RecyclerViewAdapter(FetchPastOrders.this, pastOrdersList);
                            recyclerView.setAdapter(adapter);
                        }
                    })
                    .addOnFailureListener(new OnFailureListener() {
                        @Override
                        public void onFailure(@NonNull Exception e) {
                            Toast.makeText(FetchPastOrders.this, "Problem", Toast.LENGTH_SHORT).show();
                            Log.e("Error", "Error");
                        }
                    });
        }

    }





    package com.example.merchant;

    import android.view.View;
    import android.widget.TextView;

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

    public class RecyclerViewHolder extends RecyclerView.ViewHolder {

       public  TextView Name;
        public TextView Amount;
        public  TextView CID;
        public  TextView Location;
        public TextView orderId;
        public TextView Date;
        public RecyclerViewHolder(@NonNull View itemView) {
            super(itemView);


            Name = itemView.findViewById(R.id.Name);
            Amount = itemView.findViewById(R.id.Amount);
            CID = itemView.findViewById(R.id.CID);
            orderId = itemView.findViewById(R.id.orderId);
            Location = itemView.findViewById(R.id.location);
            Date = itemView.findViewById(R.id.date);

        }
    }

0 个答案:

没有答案