我在onBindViewHolder中添加什么来绑定我的模型并填充我的RecyclerView?

时间:2018-04-03 17:23:13

标签: android firebase android-recyclerview firebaseui android-viewholder

现在我只是尝试使用Firebase UI 3.1.1来填充回收站视图。我看到的所有教程都已过时,而且过时的教程都不完整。这就是我到目前为止......

private void setUpFirebaseAdapter() {
    DatabaseReference reference = FirebaseDatabase.getInstance().getReference(Constants.LOBBIES);
    Query query = reference;

    mRecyclerView.setLayoutManager(new LinearLayoutManager(getApplicationContext()));

    FirebaseRecyclerOptions lobbyOptions = new FirebaseRecyclerOptions.Builder<Lobby>().setQuery(query, Lobby.class).build();

    mLobbyAdapter = new FirebaseRecyclerAdapter<Lobby, LobbyViewHolder>(lobbyOptions) {
        @Override
        protected void onBindViewHolder(@NonNull LobbyViewHolder holder, int position, @NonNull Lobby model) {
            holder.
        }

        @Override
        public LobbyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            return null;
        }
    };
}

我将什么放入持有人,以便我可以完成回收者视图?谢谢!

EDIT。这也是我的View holder类

public class LobbyViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {

    View mView;
    Context mContext;

    public LobbyViewHolder(View itemView) {
        super(itemView);
        mView = itemView;
        mContext = itemView.getContext();
        itemView.setOnClickListener(this);
    }
    public void bindLobby(Lobby lobby) {
    TextView nameTextView = (TextView)     mView.findViewById(R.id.lobby_name_text);
    TextView locationTextView = (TextView) mView.findViewById(R.id.lobby_location_text);
    TextView sizeTextView = (TextView) mView.findViewById(R.id.lobby_size_text);
    TextView priceTextView = (TextView) mView.findViewById(R.id.lobby_price_text);
    TextView dateTextView = (TextView) mView.findViewById(R.id.lobby_date_text);

    nameTextView.setText(lobby.getOwner());
    locationTextView.setText(lobby.getLocation());
    sizeTextView.setText(lobby.getTotalMembers());
    priceTextView.setText("Php " + lobby.getCost());
    dateTextView.setText(lobby.getDateText());
    }

@Override
public void onClick(View v) {
    final ArrayList<Lobby> lobbies = new ArrayList<>();
    DatabaseReference ref = FirebaseDatabase.getInstance().getReference(Constants.LOBBIES);
    ref.addListenerForSingleValueEvent(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            for(DataSnapshot snapshot : dataSnapshot.getChildren()){
                lobbies.add(snapshot.getValue(Lobby.class));
            }

            int itemPosition = getLayoutPosition();

            Intent intent = new Intent(mContext, LobbyDetailActivity.class);
            intent.putExtra("position", itemPosition  + "");
            intent.putExtra("lobbies", Parcels.wrap(lobbies));

            mContext.startActivity(intent);
        }

        @Override
        public void onCancelled(DatabaseError databaseError) {
        }
    });
}

}

1 个答案:

答案 0 :(得分:0)

从我看到你也没有给布局视图(onCreateViewHolder)膨胀,请这样做:

private void setUpFirebaseAdapter() {
    DatabaseReference reference = FirebaseDatabase.getInstance().getReference(Constants.LOBBIES);
    Query query = reference;

    mRecyclerView.setLayoutManager(new LinearLayoutManager(getApplicationContext()));

    FirebaseRecyclerOptions lobbyOptions = new FirebaseRecyclerOptions.Builder<Lobby>().setQuery(query, Lobby.class).build();

    mLobbyAdapter = new FirebaseRecyclerAdapter<Lobby, LobbyViewHolder>(lobbyOptions) {
        @Override
        protected void onBindViewHolder(@NonNull LobbyViewHolder holder, int position, @NonNull Lobby model) {
            holder.bindLobby(model);
            //here you associate the holder views with the model attributes
        }

        @Override
        public LobbyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

            View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.your_layout_file, parent, false);
        return new LobbyViewHolder(view);
        }
    };
}