0

enter image description here> I have 3 classes.

  • DriverDatabase.class
  • AddInfoDriver.class
  • DriverProfile.class

In DriverDatabase.class I have set(fName, lName .... and other fields.). Also, put {get; and set;} for those attribtues.

In AddInfoDriver.class I have referenced FirebaseDatabase and DriverDatabase.class so that, whenever a user fills those edittexts, it saves to Firebase Database.

The Problem is: I cannot retrieve those 5 Edittext values in DriverProfile.class below is the piece of code for retrieving.

public class DriverProfile extends Fragment {

@BindView(R.id.btn_save) Button btnsave;
@BindView(R.id.edtx_DoB) EditText edtxdob;
@BindView(R.id.edtx_fname) EditText edtx_fname;
@BindView(R.id.edtx_lname) EditText edtx_lname;
@BindView(R.id.spinner_type) Spinner spn_type;
@BindView(R.id.edtx_number) EditText edtx_number;

private DatabaseReference driverfname, driverlname;


public DriverProfile() {
//Constructed is required

}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {

    View view = inflater.inflate(R.layout.fragment_my_profile, container, false);
    ButterKnife.bind(this, view);

    driverfname = FirebaseDatabase.getInstance().getReference().child("Drivers").child("fname");
    driverlname  = FirebaseDatabase.getInstance().getReference().child("Drivers").child("lname");

    driverfname.addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            edtx_fname.setText(dataSnapshot.getValue(String.class));
        }

        @Override
        public void onCancelled(DatabaseError databaseError) {

        }
    });

Didnt write all edittext codes, just see my ValueEventListener for fname of Driver. it is not displaying data when saved by driver in a child("Drivers")

Zafar Kurbonov
  • 2,077
  • 4
  • 19
  • 42

1 Answers1

1

To get the values from all drivers please use the following code:

DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
DatabaseReference driversRef = rootRef.child("Drivers");
ValueEventListener eventListener = new ValueEventListener() {
    @Override
    public void onDataChange(DataSnapshot dataSnapshot) {
        for(DataSnapshot ds : dataSnapshot.getChildren()) {
            String cardNumber = ds.child("cardNumber").getValue(String.class);
            String cardType = ds.child("cardType").getValue(String.class);
            String dateOfBirth = ds.child("dateOfBirth").getValue(String.class);
            String name = ds.child("name").getValue(String.class);
            String surname = ds.child("surname").getValue(String.class);
            Log.d("TAG", cardNumber + " / " + cardType + " / " + dateOfBirth + " / " + name + " / " + surname);
        }
    }

    @Override
    public void onCancelled(DatabaseError databaseError) {}
};
driversRef.addListenerForSingleValueEvent(eventListener);
Alex Mamo
  • 130,605
  • 17
  • 163
  • 193
  • Perfect solution bro!! thank you. Can I ask one more thing @Alex? – Zafar Kurbonov Sep 29 '17 at 08:33
  • 1
    in one project, I have to apps. How to connect two apps together to share data. Client app and Driver app. client orders hotdog and other snacks, driver should see those orders. I have done all tasks with recyclerview and adapters. the only thing left is to connect two apps together. read documentations, not clear for me really – Zafar Kurbonov Sep 29 '17 at 08:38
  • 3
    I think that you may be interested in [this](https://stackoverflow.com/questions/37874920/can-multiple-android-application-access-same-firebase-database). – Alex Mamo Sep 29 '17 at 08:44
  • great. thanx bro – Zafar Kurbonov Sep 29 '17 at 08:51
  • This is basically another question. In order to follow the rules of this comunity, please post another fresh question, so me and other users can help you. – Alex Mamo Sep 29 '17 at 09:50
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/155582/discussion-between-zoffa-and-alex-mamo). – Zafar Kurbonov Sep 29 '17 at 10:38
  • Just answered your last question. – Alex Mamo Sep 29 '17 at 10:45