Firebase Recycler View不显示任何内容

时间:2018-03-28 14:44:31

标签: java android firebase firebase-realtime-database firebaseui

您好! 一开始我必须告诉我,我已经展示了很多关于Firebase的文档,并花了很多时间在Stack上的现有主题上。

让我们开始吧。我正在为包含Firebase实施的聊天(被叫响应)的汽车用户创建应用。 现在,我正在努力使用recyclerView显示用户列表。 在firebase我有数据库"用户"。 使用我的代码后,我没有任何用户的空白活动。

在' com.firebaseui:firebase-ui-database:2.4.0' (使用populateViewHolder)我的活动正常。 有人知道我的代码有什么问题吗? 谢谢你的回复:)

这是我的活动:

public class ResponseAllUsersActivity extends AppCompatActivity {

private Toolbar mToolbar;
private RecyclerView allUsersList;
private DatabaseReference allDatabaseUserreference;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_response_all_users);

    mToolbar = (Toolbar) findViewById(R.id.all_users_app_bar);
    setSupportActionBar(mToolbar);
    getSupportActionBar().setTitle("All Users");
    getSupportActionBar().setDisplayHomeAsUpEnabled(true);

    allUsersList = (RecyclerView) findViewById(R.id.all_users_list);
    allUsersList.setHasFixedSize(true);
    allUsersList.setLayoutManager(new LinearLayoutManager(this));

    allDatabaseUserreference = FirebaseDatabase.getInstance().getReference().child("Users");

}

@Override
protected void onStart() {
    super.onStart();

    Query query = FirebaseDatabase.getInstance()
            .getReference()
            .child("Users");

    FirebaseRecyclerOptions<Response_AllUsers> options =
            new FirebaseRecyclerOptions.Builder<Response_AllUsers>()
                    .setQuery(query, Response_AllUsers.class)
                    .build();

    FirebaseRecyclerAdapter<Response_AllUsers, AllUsersViewHolder> firebaseRecyclerAdapter
            = new FirebaseRecyclerAdapter<Response_AllUsers, AllUsersViewHolder>
            (options)
    {
        @Override
        protected void onBindViewHolder(@NonNull AllUsersViewHolder holder, int position, @NonNull Response_AllUsers model)
        {
            holder.setUser_name(model.getUser_name());
            holder.setUser_status(model.getUser_status());
            holder.setUser_image(getApplicationContext(), model.getUser_image());
        }

        @NonNull
        @Override
        public AllUsersViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
            View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.response_users_display_layout,parent,false);
            return new AllUsersViewHolder(view);
        }


    };
    allUsersList.setAdapter(firebaseRecyclerAdapter);
}
public static class AllUsersViewHolder extends RecyclerView.ViewHolder
{
    View mView;
   public AllUsersViewHolder(View itemView)
   {
       super(itemView);

       mView = itemView;
   }
   public void setUser_name(String user_name)
   {
       TextView name = (TextView) mView.findViewById(R.id.all_users_username);
       name.setText(user_name);
   }

   public void setUser_status(String user_status)
   {
       TextView status = (TextView) mView.findViewById(R.id.all_users_status);
       status.setText(user_status);
   }
   public void setUser_image(Context ctx, String user_image)
   {
       CircleImageView image = (CircleImageView) mView.findViewById(R.id.all_users_profile_image);

       Picasso.with(ctx).load(user_image).into(image);
   }
}
}

Response_AllUsers:

public class Response_AllUsers {
public String user_name;
public String user_image;
public String user_status;

public Response_AllUsers()
{

}

public Response_AllUsers(String user_name, String user_image, String user_status) {
    this.user_name = user_name;
    this.user_image = user_image;
    this.user_status = user_status;
}

public String getUser_name() {
    return user_name;
}

public void setUser_name(String user_name) {
    this.user_name = user_name;
}

public String getUser_image() {
    return user_image;
}

public void setUser_image(String user_image) {
    this.user_image = user_image;
}

public String getUser_status() {
    return user_status;
}

public void setUser_status(String user_status) {
    this.user_status = user_status;
}
}

1 个答案:

答案 0 :(得分:1)

您使用的是新版本的Firebaseui,然后在onStart()内需要添加以下内容:

 firebaseRecyclerAdapter.startListening();

开始侦听数据

并在onStop()内添加:

firebaseRecyclerAdapter.stopListening();

更多信息:

Firebase ui lifecycle