我從其數據存儲在Firebase
數據庫中的對象填充RecyclerView
。 RecyclerView
顯示當前正在使用該應用程序的用戶。 雖然RecyclerView
在顯示屏關閉並再次打開之前不會顯示它們,但是所有數據都可以正確讀取。一旦顯示屏開啓,RecyclerView
就會填充其項目。我使用調試來找出問題。在調試時它可以很好地工作但是當涉及到運行應用程序時,問題會回來。如果有人能夠幫助我解決這個高度讚賞的問題。 我已經在下面發佈了我的代碼。RecyclerView被延遲顯示其項目
protected void onStart() {
super.onStart();
mClient.connect();
childEventListener = tempUserRef.addChildEventListener(new ChildEventListener() {
@Override
public void onChildAdded(DataSnapshot dataSnapshot, String s) {
if (dataSnapshot.exists()) {
UserDetailsClass userDetailsClass = dataSnapshot.getValue(UserDetailsClass.class);
if (!userDetailsClass.getUserID().equals(UserDetails.getString("UniqueID", "ID"))) {
myFriendsAdapter.refillAdapter(userDetailsClass);
}
if (userDetailsClass.getUserID().equals(UserDetails.getString("UniqueID", "ID"))) {
sender = userDetailsClass.getName();
}
}
}
@Override
public void onChildChanged(DataSnapshot dataSnapshot, String s) {
}
@Override
public void onChildRemoved(DataSnapshot dataSnapshot) {
}
@Override
public void onChildMoved(DataSnapshot dataSnapshot, String s) {
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
}
適配器類別
public class MyFriendsAdapter extends RecyclerView.Adapter<MyFriendsAdapter.MyFriendsViewHolder> {
Context context;
ClickListener clickListener;
List<UserDetailsClass> friendsList;
public MyFriendsAdapter(Context context, List<UserDetailsClass> friendsList) {
this.context = context;
this.friendsList = friendsList;
}
@Override
public MyFriendsViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
MyFriendsViewHolder viewHolder;
LayoutInflater inflater = LayoutInflater.from(parent.getContext());
View view = inflater.inflate(R.layout.friend_online, parent, false);
viewHolder = new MyFriendsViewHolder(view);
return viewHolder;
}
@Override
public void onBindViewHolder(MyFriendsViewHolder holder, int position) {
TextView name = holder.getName();
name.setText(friendsList.get(position).getName());
holder.getStateName().setText(friendsList.get(position).getStateName());
setOnlineStatus(holder, position);
}
private void setOnlineStatus(MyFriendsViewHolder holder, int position) {
if (friendsList.get(position).getOnline_offline_status() == 1) {
holder.getOnline_offline().setText("Online");
holder.getOnline_offline().setTextColor(Color.GREEN);
}
}
@Override
public int getItemCount() {
if (friendsList == null) {
return 0;
} else {
return friendsList.size();
}
}
public void refillAdapter(UserDetailsClass userDetailsClass) {
friendsList.add(userDetailsClass);
}
public void cleanUp() {
friendsList.clear();
}
public void setClickListener(ClickListener clickListener) {
this.clickListener = clickListener;
}
class MyFriendsViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
private TextView name;
private TextView online_offline;
private TextView stateName;
private TextView distance;
public MyFriendsViewHolder(View itemView) {
super(itemView);
name = (TextView) itemView.findViewById(R.id.textView3);
online_offline = (TextView) itemView.findViewById(R.id.online);
stateName = (TextView) itemView.findViewById(R.id.textView4);
distance = (TextView) itemView.findViewById(R.id.textView13);
itemView.setOnClickListener(this);
}
public TextView getName() {
return name;
}
public void setName(TextView name) {
this.name = name;
}
public TextView getOnline_offline() {
return online_offline;
}
public void setOnline_offline(TextView online_offline) {
this.online_offline = online_offline;
}
public TextView getStateName() {
return stateName;
}
public void setStateName(TextView stateName) {
this.stateName = stateName;
}
public TextView getDistance() {
return distance;
}
public void setDistance(TextView distance) {
this.distance = distance;
}
@Override
public void onClick(View view) {
if (clickListener != null) {
String name = friendsList.get(getPosition()).getName();
clickListener.itemClick(view, getPosition(), name);
//friendsList.get(getPosition()).getName();
}
}
}
public interface ClickListener {
void itemClick(View view, int position, String name);
}
}
你忘了打電話給notifydatasetchanged()之後添加 –
聽起來像一個答案@ColdFire :-) –
@FrankvanPuffelen啊這是一個 –