두 명의 arraylist가 있고 같은 recyclerview의 두 목록에 데이터를 표시하려고합니다. 내 코드는 다음과 같습니다.하나의 사용자 지정 어댑터에 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를 사용하여 데이터를 검색하고 있습니다.
주요 활동 (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) {
}
});
}
내가 데이터를 채우기 위해 시도 할 때
이미 초대장 및 acceptedInvitations JSON 배열에서 데이터를 검색 할 수 있습니다,하지만 난 오류가 기본적으로 "addAcceptedInvitation (acceptedInvitation)"을 호출하면 acceptedInvitations에서 가져옵니다.
오류.
threadid = 1 : 캐치되지 않는 예외로 종료 스레드 (그룹 = 0x41a81c80)
무엇이 오류입니까? –
가장 위험한 오류는'user2 = acceptedInvitations.get (position) .getUserName();'이며이 줄은'acceptedInvitations'가'position'에 값을 가지기 전에 호출됩니다.'addNotification'과'addAcceptedInvitation'라고 불리는 코드에 대한 질문을 편집하고, Firebase의 데이터가 있다면 Firebase – koceeng
에 전화해서 코드를 입력하십시오. 답을 찾아 주셔서 감사합니다. 이미 내 질문을 편집했습니다. –