【发布时间】:2017-01-10 03:02:07
【问题描述】:
我有两个 arraylist,我想在同一个 recyclerview 中显示两个列表中的数据。我的代码如下所示。
我的适配器
class NotificationAdapter extends RecyclerView.Adapter<NotificationViewHolder> {
//..
List<Notification> notifList = new ArrayList<>();
List<AcceptedInvitation> acceptedInvitations = new ArrayList<>();
Context context;
LayoutInflater inflater;
public NotificationAdapter(Context context){
this.context = context;
inflater = LayoutInflater.from(context);
}
public void addNotification(Notification notification){
notifList.add(notification);
notifyDataSetChanged();
notifyItemInserted(notifList.size());
}
public void addAcceptedInvitation(AcceptedInvitation invitation){
acceptedInvitations.add(invitation);
notifyDataSetChanged();
notifyItemInserted(acceptedInvitations.size());
}
@Override
public NotificationViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = inflater.inflate(R.layout.rv_notifications, parent, false);
NotificationViewHolder holder = new NotificationViewHolder(view);
return holder;
}
@Override
public void onBindViewHolder(final NotificationViewHolder holder, int position) {
String user1= null;
String user2= null;
if(position < notifList.size()){
user1= notifList.get(position).getUserName();
}else {
user2= acceptedInvitations.get(position).getUserName();
}
@Override
public int getItemCount() {
return notifList.size() + acceptedInvitations.size();
}
}
RecyclerView ViewHolder
public class NotificationViewHolder extends RecyclerView.ViewHolder {
//..
TextView userName;
public NotificationViewHolder(View itemView) {
super(itemView);
userName = (TextView) itemView.findViewById(R.id.display_name);
}
}
我在 onBindViewHolder() 部分有错误。顺便说一下,我正在使用 Firebase 检索数据。
Main Activity(在Activity启动时调用loadData())
public void loadData(){
//..
final FirebaseDatabase database = FirebaseDatabase.getInstance();
DatabaseReference ref = database.getReference().child("users").child(key).child("invitations");
ref.addChildEventListener(new ChildEventListener() {
@Override
public void onChildAdded(DataSnapshot dataSnapshot, String s) {
Notification notification = dataSnapshot.getValue(Notification.class);
adapter.addNotification(notification);
}
@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) {
}
});
DatabaseReference invitationRef = database.getReference().child("users").child(key).child("acceptedInvitations");
invitationRef.addChildEventListener(new ChildEventListener() {
@Override
public void onChildAdded(DataSnapshot dataSnapshot, String s) {
AcceptedInvitation acceptedInvitation = dataSnapshot.getValue(AcceptedInvitation.class);
Toast.makeText(getActivity(), dataSnapshot.getValue().toString(), Toast.LENGTH_LONG).show();
adapter.addAcceptedInvitation(acceptedInvitation);
}
@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) {
}
});
}
我已经可以从invitations 和acceptedInvitations json-array 中检索数据,但是当我尝试从acceptedInvitations 填充数据时出现错误,基本上是在我调用“addAcceptedInvitation(acceptedInvitation)”时。
错误。
threadid=1:线程以未捕获的异常退出(组=0x41a81c80)
【问题讨论】:
-
错误是什么?
-
最大的错误风险是在
user2= acceptedInvitations.get(position).getUserName();这一行中,并且在您的acceptedInvitations对position具有价值之前,可能会调用此行。请编辑您的问题,提及您调用addNotification和addAcceptedInvitation的代码,如果它们的数据来自 Firebase,请在您调用 Firebase 的位置提供编码 -
感谢您的回答。我已经编辑了我的问题。
-
尝试将
if(position < notifList.size()){ ... } else { .. }中的else替换为.. } else if (position < acceptedInvitations.size()) { ... }。它应该使您的错误消失。 (并在您回复我时提及我:D) -
错误消失了,但是recyclerview中没有填充数据。 @koceengspan>
标签: android firebase firebase-realtime-database