0

I would like to have users enter a last name into a form and automatically provide the user with the last name's account id. The account id is store in a Firebase table. But onEditorAction won't allow me to paste in the onDataChange function. The @Override triggers "Annotations are not allowed here." Any idea how to make onEditorAction work with onDataChange?

Here is my failing code:

    DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
    DatabaseReference ehrRef = rootRef.child("ehr");

    mFirstName.setOnEditorActionListener(new TextView.OnEditorActionListener() {
        @Override
        public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
            boolean handled = false;
            if (actionId == EditorInfo.IME_ACTION_DONE) {
                @Override
                public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
                    Boolean found;
                    for(DataSnapshot ds : dataSnapshot.getChildren()) {
                        String ehrLastName = ds.child("lastNameEHR").getValue(String.class);
                        found = ehrLastName.contains(mLastName.getText().toString());
                        if (found) {
                            mUserIdBackPainHistory.setText(ds.child("userIdEHR").getValue(String.class));
                        }
                    }
                }
                handled = true;
            }
            return handled;
        }
    });
Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
rachelvsamuel
  • 1,551
  • 2
  • 10
  • 21

1 Answers1

0

The onDataChange method is part of a ValueEventListener interface. You can't simply add that method, without declaring it as part of the interface.

mFirstName.setOnEditorActionListener(new TextView.OnEditorActionListener() {
    @Override
    public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
        boolean handled = false;
        if (actionId == EditorInfo.IME_ACTION_DONE) {
            ehrRef.addListenerForSingleValueEvent(new ValueEventListener() {
              @Override
              public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
                Boolean found;
                for(DataSnapshot ds : dataSnapshot.getChildren()) {
                    String ehrLastName = ds.child("lastNameEHR").getValue(String.class);
                    found = ehrLastName.contains(mLastName.getText().toString());
                    if (found) {
                        mUserIdBackPainHistory.setText(ds.child("userIdEHR").getValue(String.class));
                    }
                  }
                  public void onCanceled(DatabaseError error) {
                    throw error.toException();
                  }
                }
            }
            handled = true;
        }
        return handled;
    }
});

The key here is ehrRef.addListenerForSingleValueEvent(new ValueEventListener() {, which adds a listener to your reference. This starts the loading of the data, and calls your onDataChange when the data has been loaded.

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
  • I added your helpful code, but it doesn't seem to execute. I fill in the `mFirstName` field and my Logcat shows no activity. The code is in `onCreateView`. – rachelvsamuel Jun 18 '18 at 20:45
  • If `ehrRef.addListenerForSingleValueEvent` is called, either `onDataChange` or `onCanceled` should fire soon after. If that doesn't happen, the only things I can imagine is that you're not connected to the network (in which case the client has no idea whether the node exists, or you have access to it). check your logcat for relevant messages. – Frank van Puffelen Jun 18 '18 at 20:53
  • I put a `Log.v` in first line of `onEditorAction` and `onCancelled` and Logcat didn't show anything. Argh. – rachelvsamuel Jun 18 '18 at 21:03
  • Ah, I see what happened. It works if I press return on my keyboard, but not if I just move the another field with my finger. Is there a way to make it register if I just move out of the field? – rachelvsamuel Jun 18 '18 at 21:35
  • That seems related to your `if (actionId == EditorInfo.IME_ACTION_DONE)`. To detect when it loses focus, see https://stackoverflow.com/questions/10627137/how-can-i-know-when-an-edittext-loses-focus – Frank van Puffelen Jun 18 '18 at 21:54