2017-05-03 125 views
0

显示空我想读的火力点数据库中的数据,但每次DataSnapshot是给空值,我是能够更新数据,但无法读取它。DataSnapshot数据在安卓

任何一个可以帮助我

private void showData(DataSnapshot dataSnapshot) { 
    for(DataSnapshot ds : dataSnapshot.getChildren()){ 
     UserInformation uInfo = new UserInformation(); 
     uInfo.setName(ds.child(userID).getValue(UserInformation.class).getName()); //set the name 
     uInfo.setEmail(ds.child(userID).getValue(UserInformation.class).getEmail()); //set the email 
     uInfo.setPhone_num(ds.child(userID).getValue(UserInformation.class).getPhone_num()); //set the phone_num 

     //display all the information 
     Log.d(TAG, "showData: name: " + uInfo.getName()); 
     Log.d(TAG, "showData: email: " + uInfo.getEmail()); 
     Log.d(TAG, "showData: phone_num: " + uInfo.getPhone_num()); 
     Toast.makeText(this,uInfo.getName().toString(),Toast.LENGTH_SHORT).show(); 

     ArrayList<String> array = new ArrayList<>(); 
     array.add(uInfo.getName()); 
     array.add(uInfo.getEmail()); 
     array.add(uInfo.getPhone_num()); 
     ArrayAdapter adapter = new ArrayAdapter(this,android.R.layout.simple_list_item_1,array); 
     mListView.setAdapter(adapter); 
    } 
} 

我调用此函数从

myRef.addValueEventListener(new ValueEventListener() { 
     @Override 
     public void onDataChange(DataSnapshot dataSnapshot) { 
      // This method is called once with the initial value and again 
      // whenever data at this location is updated. 
      try { 
       showData(dataSnapshot); 
      }catch (Exception e) 
      { 
       toastMessage("datasnapshow Exception"+e); 

      } 
     } 

     @Override 
     public void onCancelled(DatabaseError databaseError) { 

     } 
    }); 

这会给我空例外。我坚持这个问题,任何人都可以帮助我。数据库结构:

{ 
    myfirebase284 
     m0p7r9ryj5Mr22NVWUt1DcYTjqG2 
      email: "[email protected]" 
      name: "pema" 
      phone_num: 9990480514 
} 

enter image description here

+0

你可以发布你的数据库结构的图片吗? – wnieves19

+0

你可以请使用json文件吗?或者取而代之,一张照片? –

+0

任何好的教程,展示如何从firebased读取数据@Alex Mamo –

回答

0

当你要显示的数据,你并不需要创建UserInformation类的新对象。您需要从dataSnapshot对象中获取UserInformation对象。所以为了达到这个目的,请进行以下更改。改变这一行:

UserInformation uInfo = new UserInformation(); 

UserInformation uInfo = dataSnapshot.getValue(UserInformation.class); 
String name = uInfo.getName(); 
String email = uInfo.getEmail(); 
String phone_num = uInfo.getPhone_num(); 

考虑到getName()getEmail()getPhone_num()是从你的模型类的公共干将。

也得到了ArrayList<String> array = new ArrayList<>();出从for循环。

根据你的数据库结构使用String类来读取数据,请使用此代码:

DatabaseReference yourRef = FirebaseDatabase.getInstance().getReference().child(userId); 
ValueEventListener eventListener = new ValueEventListener() { 
    @Override 
    public void onDataChange(DataSnapshot dataSnapshot) { 
     String email = (String) dataSnapshot.child("email").getValue(); 
     String name = (String) dataSnapshot.child("name").getValue(); 
     String phone_num = (String) dataSnapshot.child("phone_num").getValue(); 
    } 

    @Override 
    public void onCancelled(DatabaseError databaseError) {} 
}; 
yourRef.addListenerForSingleValueEvent(eventListener); 

在这userId是由push()方法的独特生成的ID。

如果您想了解更多关于读取和写入到数据库火力地堡,请chceck官方doc

希望它有帮助。

+0

雅浦岛问题是与retriving如我能够获得数据的System.out.println(“数据库是----->” + ds.getValue());其中ds是数据快照,但如何逐一获取并显示它...任何帮助 –

+0

请编辑您的文章与您的数据库结构。 –

+0

我刚刚编辑亩后... @ ALex Mamo –