2017-08-30 130 views
0

在我的应用程序中,我有两个活动。第一项活动将数据保存到Firebase,第二项活动将检索数据。我可以存储的数据没有问题,但是当试图检索数据不会出现在TextView从firebase检索到textview的数据

这是我的代码,当我运行的应用程序的TextView显示为空检索数据

public class DataOne extends NavigationApp { 
    public TextView nameOne, ageOne, addressOne, shcoolOne; 
    private DatabaseReference mDatabase; 
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 



    Firebase.setAndroidContext(this); 
    mDatabase = FirebaseDatabase.getInstance().getReference().child("/DetailOne/"); 

    TextView NameStudent = (TextView) findViewById(R.id.Name); 
    TextView agestudent = (TextView) findViewById(R.id.age); 
    TextView addressstudent = (TextView) findViewById(R.id.address); 
    TextView schoolstudent = (TextView) findViewById(R.id.school); 

    nameOne = (TextView) findViewById(R.id.WriteName); 
    ageOne = (TextView) findViewById(R.id.WriteAge); 
    addressOne = (TextView) findViewById(R.id.WriteAddress); 
    shcoolOne = (TextView) findViewById(R.id.WriteSchool); 

    Button map = (Button) findViewById(R.id.mapOne); 
    map.setOnClickListener(new View.OnClickListener() { 
     @Override 

     public void onClick(View view) { 
      Intent MapOne= new Intent(DataOne.this, DataOneMap.class); 

      startActivity(MapOne); 
     } 
    }); 

    mDatabase.addValueEventListener(new ValueEventListener() { 


     @Override 
     public void onDataChange(DataSnapshot dataSnapshot) { 
      for (DataSnapshot postSnapshot : dataSnapshot.getChildren()) { 
       Detail detail = dataSnapshot.getValue(Detail.class); 

       String name = detail.getName(); 
       String age = detail.getAge(); 
       String address = detail.getAddress(); 
       String school = detail.getSchool(); 

       nameOne.setText(name); 
       ageOne.setText(age); 
       addressOne.setText(address); 
       shcoolOne.setText(school); 


      } 
     } 

     @Override 
     public void onCancelled(DatabaseError databaseError) { 

      // [START_EXCLUDE] 
      System.out.println("The read failed: " + databaseError.getMessage()); 
     } 
    }); 
} 
} 

火力地堡结构:

firebase

+0

您可以从Firebase根共享您的数据库结构吗? –

+1

从'child(“/ DetailOne /”)中删除'/''这里不是sql或php,它会像: 'child(“DetailOne”)' – Ibrahim

回答

0

试图改变这一点:

mDatabase =FirebaseDatabase.getInstance().getReference().child("/DetailOne/"); 

要这样:

mDatabase = FirebaseDatabase.getInstance().getReference().child("DetailOne"); 

并告诉其是否与此

+0

它现在可以工作,谢谢 – Hanan

1

更改后的作品从该

Detail detail = dataSnapshot.getValue(Detail.class); 

这行代码为此

Detail detail = postSnapshot.getValue(Detail.class); 
0

请从

Detail detail = dataSnapShot.getValue(Detail.class); 

改变你的模型类
Detail detail = postSnapShot.getValue(Detail.class); 

在模型类Detail同时检查所有的方法和变量是否被赋予了public访问modifier..if是private将其更改为public

例如,如果你有

private String name; 

变化到

public String name;