无法使用recyclerView检索Firestore数据库

时间:2018-10-13 18:18:52

标签: android firebase google-cloud-firestore

我正在使用recyclerView和Adapter在profileActivity中获取数据

这是我的

public class studentDetailsRecyclerActivity extends AppCompatActivity {
  //recyclerview  to set the details for UI in the student profile activity

    private RecyclerView mRecyclerView;
    private storeDetailsAdapter mStoreDetailsAdapter;

    private List<storeStudentDetails> studentDetailsList;

    private FirebaseFirestore dbReference;

    private ProgressBar mProgressBar;

    private String TAG = studentDetailsRecyclerActivity.class.getSimpleName();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        dbReference = FirebaseFirestore.getInstance();
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_recycler_details);

        mProgressBar = findViewById(R.id.progressbar);

        mRecyclerView = (RecyclerView)findViewById(R.id.recyclerView_products);
        mRecyclerView.setHasFixedSize(true);
        mRecyclerView.setLayoutManager(new LinearLayoutManager(this));

        studentDetailsList = new ArrayList<>();

        mStoreDetailsAdapter = new storeDetailsAdapter(this,studentDetailsList);

        mRecyclerView.setAdapter(mStoreDetailsAdapter);
        //to get the "details" this is our collection from firestore so we must fetch them
        //by calling the addOnSuccessListener
        dbReference.collection("details").get()
                .addOnSuccessListener(new OnSuccessListener<QuerySnapshot>() {
                    @Override
                    public void onSuccess(QuerySnapshot queryDocumentSnapshots) { //we must have to hide the progress bar when the data gets loaded

                        //here queryDocumentsSnapshot will hold all the "details" which is your collection in firestore

                        if(!queryDocumentSnapshots.isEmpty()){

                            //we must have to create empty list so that to store all
                            //details from DocumentsSnapshots
                            List<DocumentSnapshot>  list =  queryDocumentSnapshots.getDocuments();



                            //enhanced for loop because we have to give every index documentSnapShot

                            for(DocumentSnapshot d: list){

                                storeStudentDetails sd = d.toObject(storeStudentDetails.class);
                                studentDetailsList.add(sd);


                                Log.d(TAG, "onSuccess: " + sd.toString());




                            }

                            //to refresh and sync we must have to use notifyDataSetChanged

                            mStoreDetailsAdapter.notifyDataSetChanged();
                        }
                    }
                })  .addOnFailureListener(new OnFailureListener() {
            @Override
            public void onFailure(@NonNull Exception e) {
                Toast.makeText(getApplicationContext(), "Error getting data!!!", Toast.LENGTH_LONG).show();
            }
        });
    }
}

这是我的storeDetailsAdapter

import java.util.List;

public class storeDetailsAdapter extends RecyclerView.Adapter<storeDetailsAdapter.StudentViewHolder>{

    private Context context;
    private List<storeStudentDetails> studentDetailsList;

    public storeDetailsAdapter(Context context, List<storeStudentDetails> studentDetailsList) {
            this.context = context;
            this.studentDetailsList = studentDetailsList;
    }

    @NonNull
    @Override
    public StudentViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        return new StudentViewHolder(
                LayoutInflater.from(context).inflate(R.layout.profile_activity, parent, false)
        );
    }



    @Override
    public void onBindViewHolder(@NonNull StudentViewHolder holder, int position) {
        storeStudentDetails mStoreDetails = studentDetailsList.get(position);

        holder.studName.setText(mStoreDetails.getStudentName());
        holder.rollNum.setText(mStoreDetails.getRollNo());
        holder.bookName.setText( mStoreDetails.getBook());
        holder.fine.setText("Fine:" + mStoreDetails.getFine());
        holder.dept.setText(mStoreDetails.getDept());

    }

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

    class StudentViewHolder extends RecyclerView.ViewHolder {

        TextView studName,rollNum,bookName,dept,fine;

        public StudentViewHolder(View itemView) {
            super(itemView);

            studName=itemView.findViewById(R.id.studentName_prof);
            rollNum = itemView.findViewById(R.id.rollNumber_prof);
            bookName = itemView.findViewById(R.id.bookName_prof);
            fine = itemView.findViewById(R.id.fineAmt_prof);
            dept = itemView.findViewById(R.id.department_prof);
        }
    }
}

这是我的StoreStudentDetails类:

public class storeStudentDetails implements Serializable {


    private String studentName;
    private  String rollNo;
    private  String book;
    private Double fine;
    private String dept;

    @Exclude private String id;

    public storeStudentDetails() {
    }

    public storeStudentDetails(String studentName, String rollNo,String book, double fine ,String dept) {

        this.studentName = studentName;
        this.rollNo = rollNo;
        this.book = book;
        this.fine = fine;
        this.dept = dept;


    }

    public void setId(String id) {
        this.id = id;
    }

    public String getStudentName() {
        return studentName;
    }

    public String getRollNo() {
        return rollNo;
    }

    public String getBook() {
        return book;
    }

    public Double getFine() {
        return fine;
    }

    public String getDept() {
        return dept;
    }

    public String getId() {
        return id;
    }
}

1 个答案:

答案 0 :(得分:0)

要解决此问题,请移动以下代码行:

mStoreDetailsAdapter = new storeDetailsAdapter(this,studentDetailsList);
mRecyclerView.setAdapter(mStoreDetailsAdapter);

在下面的代码行之前:

mStoreDetailsAdapter.notifyDataSetChanged();

这是因为onSuccess()方法具有异步行为,并且当您在回调外部设置适配器时,列表为空。

如您所见,解决此问题的最简单方法是将这些代码行移到回调中。但是如果您想在studentDetailsList方法之外使用onSuccess()的值,我建议您从此 post 中查看anwser的最后一部分,其中我已经解释了如何使用自定义回调来完成。您也可以查看此 video 以获得更好的理解。