请帮助我,我试图在./node_modules/.bin/parcel index.html
和child("complaints")
中获取数据。收集的数据必须存储在List中。儿童名单(“投诉”)和child("complainee")
list2
。但是,当我显示它时,child("complainee")
为null。先感谢您。
list2
这是Personal_RecyclerAdapter,我在其中显示列表和 list2 。 列表中的数据已完美显示,但 list2 中的数据未显示/ null。
database = FirebaseDatabase.getInstance();
myRef = database.getReference("complaints");
list = new ArrayList<Blog>();
list2 = new ArrayList<Complainee>();
myRef.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
try{
for(DataSnapshot dataSnapshot1 :dataSnapshot.getChildren()){
final Blog value = dataSnapshot1.getValue(Blog.class);
Boolean Posted = value.getPosted();
String complaint_type = value.getComplaintType();
if(complaint_type.equals("Personal Complaint")) {
final int ComplaintNo = value.getComplaintNo();
String Description = value.getDescription();
String Date = value.getDate();
String MediaURL = value.getMediaURL();
String Time = value.getTime();
String Address = value.getAddress();
final Blog fire = new Blog();
fire.setComplaintNo(ComplaintNo);
fire.setDescription(Description);
fire.setDate(Date);
fire.setMediaURL(MediaURL);
fire.setTime(Time);
fire.setAddress(Address);
fire.setComplainee_Name(value.getComplainee_Name());
// Read from the database for COMPLAINEE
FirebaseDatabase database2 = FirebaseDatabase.getInstance();
DatabaseReference myRef2 = database2.getReference("complainee").child(String.valueOf(ComplaintNo));
myRef2.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnap) {
Complainee cm = dataSnap.getValue(Complainee.class);
String n = cm.getName();
final Complainee fire2 = new Complainee();
fire2.setName(n);
fire2.setAddress("Address");
list2.add(fire2);
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
list.add(fire);
}
}
Personal_RecyclerAdapter recyclerAdapter = new Personal_RecyclerAdapter(list,list2, getContext());
RecyclerView.LayoutManager recyce = new GridLayoutManager(getContext(),1);
recyclerView.setLayoutManager(recyce);
recyclerView.setItemAnimator( new DefaultItemAnimator());
recyclerView.setAdapter(recyclerAdapter);
}catch (Exception e){
Toast.makeText(getContext(), "::ERROR " + e.toString(), Toast.LENGTH_LONG).show();
}
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
答案 0 :(得分:0)
您正在初始化儿童和投诉的数据库参考&#34;写myRef = database.getReference("complaints");
这就是为什么在#34;抱怨&#34;儿童。
改变你的参考 -
myRef = database.getReference();
然后使用
获取值myRef.child("complainee").addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
............
和另一个
myRef.child("complaints").addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
............
希望你的问题能够得到解决。
答案 1 :(得分:0)
这是由于firebase侦听器的异步性质,您还没有通知适配器更改。在您的情况下,您要将项目添加到列表list.add(fire)
,然后您已定义了回收站视图。但是您还没有等待将list2的数据添加到list2.add(fire2)
中。
以下是您的代码的外观:
database = FirebaseDatabase.getInstance();
myRef = database.getReference("complaints");
Personal_RecyclerAdapter recyclerAdapter = new Personal_RecyclerAdapter(list,list2, getContext());
RecyclerView.LayoutManager recyce = new GridLayoutManager(getContext(),1);
recyclerView.setLayoutManager(recyce);
recyclerView.setItemAnimator( new DefaultItemAnimator());
recyclerView.setAdapter(recyclerAdapter);
list = new ArrayList<Blog>();
list2 = new ArrayList<Complainee>();
myRef.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
try{
for(DataSnapshot dataSnapshot1 :dataSnapshot.getChildren()){
final Blog value = dataSnapshot1.getValue(Blog.class);
Boolean Posted = value.getPosted();
String complaint_type = value.getComplaintType();
if(complaint_type.equals("Personal Complaint")) {
final int ComplaintNo = value.getComplaintNo();
String Description = value.getDescription();
String Date = value.getDate();
String MediaURL = value.getMediaURL();
String Time = value.getTime();
String Address = value.getAddress();
final Blog fire = new Blog();
fire.setComplaintNo(ComplaintNo);
fire.setDescription(Description);
fire.setDate(Date);
fire.setMediaURL(MediaURL);
fire.setTime(Time);
fire.setAddress(Address);
fire.setComplainee_Name(value.getComplainee_Name());
// Read from the database for COMPLAINEE
DatabaseReference myRef2 = database.getReference("complainee").child(String.valueOf(ComplaintNo));
myRef2.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnap) {
Complainee cm = dataSnap.getValue(Complainee.class);
String n = cm.getName();
final Complainee fire2 = new Complainee();
fire2.setName(n);
fire2.setAddress("Address");
list2.add(fire2);
// Notify to your recyclerview that for the new data
recyclerAdapter.notifyDataSetChanged();
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
list.add(fire);
// Notify to your recyclerview that for the new data
recyclerAdapter.notifyDataSetChanged();
}
}
}catch (Exception e){
Toast.makeText(getContext(), "::ERROR " + e.toString(), Toast.LENGTH_LONG).show();
}
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
一旦获得数据,您必须通知recyclerview适配器数据集中存在一些更改。
希望它有所帮助!
答案 2 :(得分:0)
您只需要像这样查询。
FirebaseDatabase mDatabase;
DatabaseReference mDatabaseReference;
List<Complainee> complaineeList=new ArrayList();
mDatabase = FirebaseDatabase.getInstance();
mDatabaseReference = mDatabase.getReference();
mdatabaseRef.child("complainee").addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
for (DataSnapshot childSnapshot: dataSnapshot.getChildren()){
if(complaineeList.size>0){
complaineeList.clear();
}
Complainee complainee = childSnapshot.getValue(Complainee.class );
complaineeList.add(complainee);
}
Adapter recyclerAdapter = new Adapter(complaineeList, getContext());
}