Hanan
Hanan

Reputation: 526

retrieve data from firebase to textview

In my app I have two activities. The first activity saves data to Firebase, the second activity retrieves data. I can store data without problem but when try retrieve data it does not appear in the TextView

This my code to retrieve the data

 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());
        }
    });
}
}

When I run app the TextView appears empty.

Firebase structure:

firebase

Upvotes: 1

Views: 10047

Answers (3)

Uncle Sam
Uncle Sam

Reputation: 61

Kindly change your model class from

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

to

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

Also check in your model class Detail whether all the methods and variables were given a public access modifier..if it is private change it to public

for instance if you have

private String name;

change to

public String name;

Upvotes: 0

alsabsab
alsabsab

Reputation: 1311

Change this line of code from this

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

To this

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

Upvotes: 2

Razvan
Razvan

Reputation: 292

Try to change this:

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

To this:

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

And tell if it works after this

Upvotes: 0

Related Questions