2017-08-30 167 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;