我正在构建一个活动应用程序,这是我每个活动的卡片模型:
现在这是我数据库中的事件数据。我有一个“events”节点(包含每个事件项)和一个“events-users”节点(其中包含参加该事件的每个用户项)。< / p>
要获取基本事件项(图像和名称),我有一个FirebaseRecyclerAdapter,它运行良好:
/**** EVENTS EVENTS EVENTS EVENTS ****/
// Inflates events
RecyclerView mEventsRecyclerView = (RecyclerView) findViewById(R.id.activity_zone_events_recyclerview);
mEventsRecyclerView.setLayoutManager(new GridLayoutManager(ZoneActivity.this, 1));
DatabaseReference events = fDatabaseRoot.child("events");
mEventsAdapter = new FirebaseRecyclerAdapter<EventItem, EventItemHolder>(EventItem.class, R.layout.item_event, EventItemHolder.class, events) {
@Override
protected void populateViewHolder(final EventItemHolder eventItemHolder, final EventItem event, final int position) {
// Sets event image
eventItemHolder.setEventImage(event.getEventImage());
// Sets event name
eventItemHolder.setEventName(event.getEventName());
// Sets event OnClickListener
eventItemHolder.eventCard.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// Opens EventActivity
Intent eventActivity = new Intent(ZoneActivity.this, EventActivity.class);
startActivity(eventActivity);
}
}
});
};
mEventsRecyclerView.setAdapter(mEventsAdapter);
问题是我不知道如何在每张卡内获取用户项目(与会者)。 我尝试在我创建的FirebaseRecyclerAdapter中初始化另一个FirebaseRecyclerAdapter但是当我打开相应的活动时应用程序中断...这就是我尝试过的:
/**** EVENTS EVENTS EVENTS EVENTS ****/
// Inflates events
RecyclerView mEventsRecyclerView = (RecyclerView) findViewById(R.id.activity_zone_events_recyclerview);
mEventsRecyclerView.setLayoutManager(new GridLayoutManager(ZoneActivity.this, 1));
DatabaseReference events = fDatabaseRoot.child("events");
mEventsAdapter = new FirebaseRecyclerAdapter<EventItem, EventItemHolder>(EventItem.class, R.layout.item_event, EventItemHolder.class, events) {
@Override
protected void populateViewHolder(final EventItemHolder eventItemHolder, final EventItem event, final int position) {
// Sets event image
eventItemHolder.setEventImage(event.getEventImage());
// Sets event name
eventItemHolder.setEventName(event.getEventName());
// Sets event OnClickListener
eventItemHolder.eventCard.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// Opens EventActivity
Intent eventActivity = new Intent(ZoneActivity.this, EventActivity.class);
startActivity(eventActivity);
}
/**** EVENT-ATTENDEES EVENT-ATTENDEES EVENT-ATTENDEES EVENT-ATTENDEES ****/
// Inflates event's attendees
RecyclerView mAttendeesRecyclerView = (RecyclerView) findViewById(R.id.item_event_attendees_recyclerview);
mAttendeesRecyclerView.setLayoutManager(new GridLayoutManager(ZoneActivity.this, 1));
mAttendeesAdapter = new FirebaseRecyclerAdapter<UserItem, UserItemHolder>(UserItem.class, R.layout.item_user, UserItemHolder.class, fDatabaseRoot.child("zones-events").child(zoneKey)) {
@Override
protected void populateViewHolder(final UserItemHolder userItemHolder, final UserItem user, final int position) {
// Sets attendee image
userItemHolder.setUserImage(user.getUserImage());
}
};
mAttendeesRecyclerView.setAdapter(mAttendeesAdapter);
}
};
mEventsRecyclerView.setAdapter(mEventsAdapter);
这是错误:
02-27 19:18:53.783 1050-1050/? E/UncaughtException: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.support.v7.widget.RecyclerView.setLayoutManager(android.support.v7.widget.RecyclerView$LayoutManager)' on a null object reference
at com.whatudu.whatudu.ZoneActivity$2$1$1.populateViewHolder(ZoneActivity.java:208)
at com.whatudu.whatudu.ZoneActivity$2$1$1.populateViewHolder(ZoneActivity.java:141)
at com.firebase.ui.database.FirebaseRecyclerAdapter.onBindViewHolder(FirebaseRecyclerAdapter.java:184)
at android.support.v7.widget.RecyclerView$Adapter.onBindViewHolder(RecyclerView.java:5825)
at android.support.v7.widget.RecyclerView$Adapter.bindViewHolder(RecyclerView.java:5858)
at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:5094)
at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:4970)
at android.support.v7.widget.LinearLayoutManager$LayoutState.next(LinearLayoutManager.java:2029)
at android.support.v7.widget.GridLayoutManager.layoutChunk(GridLayoutManager.java:541)
at android.support.v7.widget.LinearLayoutManager.fill(LinearLayoutManager.java:1377)
at android.support.v7.widget.LinearLayoutManager.onLayoutChildren(LinearLayoutManager.java:578)
at android.support.v7.widget.GridLayoutManager.onLayoutChildren(GridLayoutManager.java:170)
at android.support.v7.widget.RecyclerView.dispatchLayoutStep2(RecyclerView.java:3315)
at android.support.v7.widget.RecyclerView.onMeasure(RecyclerView.java:2843)
任何形式的帮助或提示(如果之前有人这样做过)将非常感谢!我知道这是可能的,我只是不知道该怎么做! :(
非常感谢你。
P.S。选择GridLayoutManager作为布局样式,因为这是我之前所做的。