我正在使用Firebase
并使用android.support.v7.widget.RecyclerView
。问题是onStart
视图加载为空,数据仅在滚动后填充。知道为什么吗?
代码:
RecyclerView (我正在设置一个空的适配器onStart以避免“RecyclerView:没有连接适配器;跳过布局”):
mLayoutManager = new LinearLayoutManager(getActivity());
mLayoutManager.setReverseLayout(true);
mLayoutManager.setStackFromEnd(true);
mRecyclerView.setLayoutManager(mLayoutManager);
mRecyclerView.setHasFixedSize(true);
mRecyclerView.getRecycledViewPool().setMaxRecycledViews(1,0);
mRecyclerView.setAdapter(new RecyclerView.Adapter() {
@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
return null;
}
@Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
}
@Override
public int getItemCount() {
return 0;
}
});
Firebase数据提取:
databaseReference.child("/News").orderByChild("createdAt").addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(final DataSnapshot dataSnapshot) {
for (DataSnapshot postSnapshot : dataSnapshot.getChildren()) {
final News news = postSnapshot.getValue(News.class);
final PostListClass postListClass = new PostListClass();
//add fac id admin
if (news.getFaculteId().equals(userFaculteId) || news.getFaculteId().equals("Admin")) {
postListClass.setPostId(postSnapshot.getKey());
postListClass.setPostTitle(news.getHeadlines());
postListClass.setPostDescription(news.getDetails());
databaseReference.child("/users").child(news.getAuthor()).addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot usersSnapShot) {
User user = usersSnapShot.getValue(User.class);
postListClass.setPostOwnerId(usersSnapShot.getKey());
postListClass.setPostOwnerName(user.getPrenom() + " " + user.getNom());
postListClass.setPostOwnerPromo(user.getPromotion());
try {
Calendar cal = Calendar.getInstance(Locale.ENGLISH);
cal.setTimeInMillis(news.getCreatedAt() * 1000);
SimpleDateFormat sdf = new SimpleDateFormat("dd-MM-yyyy");
Date date = sdf.parse(DateFormat.format("dd-MM-yyyy", cal).toString());
String day = (String) android.text.format.DateFormat.format("dd", date);
String month = (String) android.text.format.DateFormat.format("MMM", date);
postListClass.setPostDate(day + " " + month);
} catch (java.text.ParseException e) {
e.printStackTrace();
}
storageRef.child("users/" + news.getAuthor() + ".jpg").getDownloadUrl().addOnSuccessListener(new OnSuccessListener<Uri>() {
@Override
public void onSuccess(Uri uri) {
postListClass.setPostOwnerImageUrl(String.valueOf(uri));
}
}).addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception exception) {
// Handle any errors
if (exception.getClass().isInstance(StorageException.class)) {
postListClass.setPostOwnerImageUrl("NullImage");
}
}
});
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
databaseReference.child("/News").child(postSnapshot.getKey()).child("/Comments").addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
postListClass.setPostComments(String.valueOf(dataSnapshot.getChildrenCount()));
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
storageRef.child("News/" + postSnapshot.getKey() + ".png").getDownloadUrl().addOnSuccessListener(new OnSuccessListener<Uri>() {
@Override
public void onSuccess(Uri uri) {
postListClass.setPostImageUrl(String.valueOf(uri));
}
}).addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception exception) {
postListClass.setPostImageUrl("NullImage");
}
});
postListClasses.add(postListClass);
}
}
postsAdapter = new PostsAdapter(getContext(), postListClasses);
mRecyclerView.setAdapter(postsAdapter);
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
所以这是应用启动时的结果(三个字段缺少数据):
滚动后(缺少数据填写):
答案 0 :(得分:2)
数据是从Firebase异步加载的。因此,当您将适配器附加到列表时,还没有数据。
一旦数据从服务器进入,您需要提醒其列表。您可以致电adapter.notifyDataSetChanged()
。