0

I am creating an application for android and I need to find a user in the database by nickname and after it is found I need to get his id. The problem is that the database does not find a user with this nickname, although there is such a user in the database and I do not understand the problem, maybe someone can tell me.

here is my code:

private SpannableString makeSpannable(String comment) //comment = "Hahdgd @aimness2"
{
    
    Pattern pattern = Pattern.compile("[@][a-zA-Z0-9-.]+");
    SpannableString spannableString = new SpannableString(comment);
    Matcher matcher = pattern.matcher(spannableString);


    while (matcher.find())
    {
        int start = matcher.start();
        int end = matcher.end();

        ClickableSpan span = new ClickableSpan() {

            String email;
            @Override
            public void onClick(@NonNull View widget) {
                
                String substr = comment.substring(start+1, end);

                findUserUid(substr);
                

                SomeUserProfileFragment someUserProfileFragment = new SomeUserProfileFragment();

                Bundle args = new Bundle();
                args.putString("UserData", uid);
                someUserProfileFragment.setArguments(args);

                getActivity().getSupportFragmentManager().beginTransaction()
                        .add(R.id.frame_container, someUserProfileFragment)
                        .addToBackStack(null)
                        .commit();

            }
        };

        spannableString.setSpan(span, matcher.start(), matcher.end(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
    }
    return  spannableString;
}

private String findUserUid(String nick)
{
    DatabaseReference db =  FirebaseDatabase.getInstance().getReference("UserData");
    Query query = db.orderByChild("nickname").equalTo(nick);

    query.addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(@NonNull DataSnapshot snapshot) {
           if(snapshot.exists()) {
               for (DataSnapshot dataSnapshot : snapshot.getChildren()) {
                   uid = "" + dataSnapshot.child("uid").getValue(String.class);
               }
           }else
           {
               Log.i("hahahha", "loch");
           }
        }

        @Override
        public void onCancelled(@NonNull DatabaseError error) {

        }

    });

    return uid;
}

database screen

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
lin
  • 1
  • 1
  • 3
  • The first step is to stop ignoring potential errors and implement `onCancelled`. At its minimum this should be `public void onCancelled(@NonNull DatabaseError databaseError) { throw databaseError.toException(); }` – Frank van Puffelen Nov 04 '21 at 19:07
  • @FrankvanPuffelen tahnk you. I've add code to onCancelled(). But I still have a problem – lin Nov 04 '21 at 19:22
  • If no error raised through `onCancelled`, try reproducing the problem with a hard-coded value: `Query query = db.orderByChild("nickname").equalTo("aimness2");` – Frank van Puffelen Nov 04 '21 at 20:05
  • @FrankvanPuffelen uid is still null – lin Nov 04 '21 at 21:58
  • Oh yeah, I now see what you're trying to do. Data is loaded from Firebase (and most modern cloud APIs) asynchronously, and you can't return something now that has not been loaded yet. If you run the code in a debugger, you'll see that `return uid` runs before `uid = "" + dataSnapshot.child("uid").getValue(String.class)` ever executes. The solution is (always) that all code that needs the data fro the database must be inside `onDataChange` or be called from there. I linked a few other questions on the topic, as this is incredibly common – Frank van Puffelen Nov 04 '21 at 22:07
  • 1
    @FrankvanPuffelen I've solved it. The problem was that it first invoked another fragments and only looked for its uid – lin Nov 04 '21 at 22:13

0 Answers0