我正在尝试检索在firebase中上传的信息。一些上传的数据包含一个imgURl,一些不包含。
我的数据库如下所示:
这是我用来检索数据的代码:
public class ListRequests extends AppCompatActivity {
ListView listView;
FirebaseDatabase database;
DatabaseReference ref;
DatabaseReference Mref;
ArrayList<String>list;
ArrayAdapter<String>adapter;
Uri imgURI;
req req1;
Upload upload1;
TextView textView;
private static final String TAG = "ListRequests";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_list_requests);
// req1=new req();
upload1=new Upload();
listView=(ListView)findViewById(R.id.listView);
textView=findViewById(R.id.requestinfo);
database= FirebaseDatabase.getInstance();
ref=database.getReference("uploads");
list = new ArrayList<>();
adapter=new ArrayAdapter<String>(this,R.layout.requestinfo,R.id.requestinfo,list);
DatabaseReference database = FirebaseDatabase.getInstance().getReference();
DatabaseReference ref = database.child("uploads"); //ur child name of the database
ref.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
for(DataSnapshot singleSnapshot : dataSnapshot.getChildren()){
upload1=singleSnapshot.getValue(Upload.class);
if(upload1.getmImageUrl().toString()!=""){
list.add("\n"+" "+"Title: "+upload1.getTitle().toString()+"\n"
+" "+"Content: "+upload1.getContent().toString()+"\n"+" "+"Date: "+upload1.getDate().toString()+
"\n"+" "+"Time:"+upload1.getTime().toString()+"\n"+" "+"Location: "+upload1.getLocation().toString()+"\n"+" "+
"Type of notification: "+ upload1.getTypeof()+"\n"+" "+ "\n"+" "
+"Status: "+ upload1.getStatus().toString()+"\n"+" "+upload1.getmImageUrl().toString());
} else {
list.add("\n"+" "+"Title: "+upload1.getTitle().toString()+"\n"
+" "+"Content: "+upload1.getContent().toString()+"\n"+" "+"Date: "+upload1.getDate().toString()+
"\n"+" "+"Time:"+upload1.getTime().toString()+"\n"+" "+"Location: "+upload1.getLocation().toString()+"\n"+" "+
"Type of notification: "+ upload1.getTypeof()+"\n"+" "+ "\n"+" "
+"Status: "+ upload1.getStatus().toString()+"\n"+" ");
}
}
listView.setAdapter(adapter);
}
@Override
public void onCancelled(DatabaseError databaseError) {
Log.e(TAG, "onCancelled", databaseError.toException());
}
});
}
}
由于upload1.getmImageUrl().toString()
,某些数据未检索到ImgURl并导致程序崩溃。
如果它包含ImgURl,我想要检索所有的childern。 有没有人有解决方案,我如何检索所有数据?
以下是我在日志中遇到的错误:
04-10 13:11:35.282 17684-17684/com.example.hp.upload E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.hp.upload, PID: 17684
java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String java.lang.String.toString()' on a null object reference
at com.example.hp.upload.ListRequests$1.onDataChange(ListRequests.java:60)
at com.google.firebase.database.zzp.onDataChange(Unknown Source)
at com.google.android.gms.internal.to.zza(Unknown Source)
at com.google.android.gms.internal.vj.zzHX(Unknown Source)
at com.google.android.gms.internal.vp.run(Unknown Source)
at android.os.Handler.handleCallback(Handler.java:751)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6692)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1468)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1358)
答案 0 :(得分:0)
我已更新答案,试一试
DatabaseReference database = FirebaseDatabase.getInstance().getReference();
DatabaseReference ref = database.child(""); //ur child name of the database
ref.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
for(DataSnapshot singleSnapshot : dataSnapshot.getChildren()){
upload1=singleSnapshot.getValue(Upload.class);
if(upload1.getmImageUrl().toString()!=""){
System.out.println("Location: "+upload1.getLocation().toString()+"\n"+" "+" "+upload1.getmImageUrl().toString());
list.add(upload1.getmImageUrl().toString());
} else {
list.add(upload1.getLocation().toString());
}
}
listView.setAdapter(adapter);
}
@Override
public void onCancelled(DatabaseError databaseError) {
Log.e(TAG, "onCancelled", databaseError.toException());
}
});