从数据库中获取详细信息之前,正在加载Android适配器

时间:2018-11-22 10:50:54

标签: java android

// here is my java code
private void loadData() {
    try {
        FirebaseFirestore db = FirebaseFirestore.getInstance();
        db.collection("stylists").get()
                .addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
                    @Override
                    public void onComplete(@NonNull Task<QuerySnapshot> task) {
                        if (task.isSuccessful()) {
                            try {
                                for(DocumentSnapshot documentSnapshot:task.getResult()){
                                    String stylistid=documentSnapshot.getId();
                                    String firstName=documentSnapshot.getString("firstName");
                                    String lastName=documentSnapshot.getString("lastName");
                                    String imageUrl=documentSnapshot.getString("imageUrl");
                                    serviceArray=(ArrayList<String>)documentSnapshot.get("services");
                                    stylistCollection=new StylistCollection(stylistid,firstName,lastName,imageUrl,serviceArray);
                                    stylistDetailsList.add(stylistCollection);
                                }
                                downloadImageUrl();
                            }catch (Exception e){
                                e.printStackTrace();
                            }
                            sharedPreferences.edit().clear().apply();
                        } else {
                            Log.d(TAG, "Error getting documents: ", task.getException());
                        }
                    }
                }).addOnSuccessListener(new OnSuccessListener<QuerySnapshot>() {
            @Override
            public void onSuccess(QuerySnapshot queryDocumentSnapshots) {
                setAdapter();

            }
        });
    }catch (Exception e){
        e.printStackTrace();
    }

}

private void downloadImageUrl() {
    try{
        for (int i=0;i<stylistDetailsList.size();i++){
            position=i;
            FirebaseStorage firebaseStorage = FirebaseStorage.getInstance();
            StorageReference storageReference= firebaseStorage.getReference();
            storageReference.child(stylistDetailsList.get(i).getImageUrl()).getDownloadUrl().addOnSuccessListener(new OnSuccessListener<Uri>() {
                @Override
                public void onSuccess(Uri uri) {
                    Log.e("imageurl", "uri: " + uri.toString());
                    String strUrl=uri.toString();
                    stylistDetailsList.get(position).setImageUrl(strUrl);
                }
            });
        }

    }catch (Exception e){
        e.printStackTrace();
    }

}

public void setAdapter(){
    StylistAdapter listAdapter = new StylistAdapter(documentId,stylistDetailsList);
    RecyclerView.ItemDecoration dividerItemDecoration = new DividerItemDecorator(ContextCompat.getDrawable(getContext(), R.drawable.divider));
    recyclerView.addItemDecoration(dividerItemDecoration);
    recyclerView.setAdapter(listAdapter);
    RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(getActivity());
    recyclerView.setLayoutManager(layoutManager);
    listAdapter.notifyDataSetChanged();

}

我正在从Firestore数据库中获取数据,并且我有一个名为imageurl的属性,该位置现在存储图像的文件夹名称,我想通过使用Firebase存储中的文件夹名称来下载特定图像的url。要在recyclerview中填充所有数据。这里的问题是在获取适配器正在执行的数据之前,我要在获取所有详细信息之后执行适配器。

1 个答案:

答案 0 :(得分:0)

你为什么不尝试这个

if(Yourasyctask.isFinshing){
set your recycler view code here also 
}

注意-:设置适配器之前先设置布局管理器
像这样

  RecyclerView rv_following_me = (RecyclerView) view.findViewById(R.id.rv_following);
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getActivity());
        rv_following_me.setLayoutManager(linearLayoutManager);

        followingAdapter = new FollowingAdapter(getActivity(), alMeModel);
        rv_following_me.setAdapter(followingAdapter);

谢谢