我正在开发一个应用程序,并且在数据库中有像医生和病人这样的用户。患者数据中可能有医生userId,医生数据中可能有患者userId
。因为在应用程序中,医生将看到有关其患者的一些信息。我开发了这段代码,但是在设置适配器之前无法将患者信息添加到Arraylist中。这是我的代码:
myRef.child("users")
.child(firebaseUser.getUid()).child("patients").addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
for(DataSnapshot ds: dataSnapshot.getChildren()){
AddingPatients addingpatient = ds.getValue(AddingPatients.class);
if(addingpatient.getConfirm().equals("yes")){
myref.child("users").child(addingpatient.getUserId()).addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
if (dataSnapshot.exists()) {
Users patient = dataSnapshot.getValue(Users.class);
name.add(patient.getName()+" "+patient.getSurname());
sicks.add(patient.getSicks());
patientuid.add(patient.getUserId());
}
}
@Override
public void onCancelled(DatabaseError error) {
}
});
}
}
PatientSelectionAdapter adapter= new PatientSelectionAdapter(getContext(),name,sicks,hastauid);
rv.setAdapter(adapter);
rv.setLayoutManager(new LinearLayoutManager(getContext()));
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
}
});
当我调试代码时,看到的是,设置适配器后,患者信息会添加到列表中。所以我的RecyclerView
是空的。我该如何解决这个问题?
答案 0 :(得分:2)
要解决此问题,请移动以下代码行:
*=
在第二个var search = function(current_page) {
var $links = getVimeoLinks();
//Click each link
$links.forEach(function(link){
//Add _blank target, to avoid stoping execution after clicking
$(link).attr('target', '_blank');
link.click();
});
var next_page = (current_page+1);
//Click next page
//considering that next page button always have aria-label="Pg X"
//and that it's the only link with that pattern on aria-label
var next_page_link = $("a[aria-label^='Pg "+next_page+"']");
if(next_page_link.length === 0){
alert('no more pages found after page'+current_page+' :(');
return;
} else {
next_page_link.click();
}
return search(next_page);
}
//Add event to start searching
$('#search').on('click', function() {search(1)})
//function to get the vimeo links
var getVimeoLinks = function(){
var links = $('a').toArray().filter(function(link){
$link = $(link);
href = $link.attr('href');
if(typeof href !== 'string')
return false;
return href.match(/vimeo\.com/) !== null;
});
return links;
}
方法中,如下所示:
PatientSelectionAdapter adapter= new PatientSelectionAdapter(getContext(),name,sicks,hastauid);
rv.setAdapter(adapter);
rv.setLayoutManager(new LinearLayoutManager(getContext()));
发生这种情况是因为onDataChange()
具有异步行为。请查看 here 模式的详细信息。
答案 1 :(得分:0)
您需要:
将以下行移动到顶部onDataChange()上。
PatientSelectionAdapter adapter= new PatientSelectionAdapter(getContext(),name,sicks,hastauid);
在从内部onDataChange()返回之前插入以下行
adapter.notifyDataSetChanged();