0

When a user saves a String to the database it usually begins with a capital letter, and that is the way that I would like it to remain. But then in my SearchFragment, the user types in the name of a certain place, or activity or whatever, and for example the word could be Nature Hike, or something, and I want at THIS moment (the search) for the title of the event saved in the database to not matter if it is a capital letter or lowercase.

How can I achieve this? If the String is already saved to the database, and another user searches for the that String, but instead of Nature Hike they write nature... then I want it to still come up.

Can someone tell me how to do this?

SearchEventsFragment

public class SearchEventsFragment extends Fragment {

    RecyclerView mRecyclerView;
    SearchEventsAdapter mSearchEventsAdapter;
    List<Post> mPostList;

    private EditText mSearchBar;
    Activity mActivity;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_search_events, container, false);

        mRecyclerView = view.findViewById(R.id.recycler_view);
        mRecyclerView.setHasFixedSize(true);
        mRecyclerView.setLayoutManager(new LinearLayoutManager(getContext()));

        mPostList = new ArrayList<>();
        mSearchEventsAdapter = new SearchEventsAdapter(getContext(), mPostList);
        mRecyclerView.setAdapter(mSearchEventsAdapter);

        mSearchBar = mActivity.findViewById(R.id.search_bar);
        mSearchBar.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                searchEvents(s.toString());
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });

        readEvents();

        return view;
    }

    private void searchEvents(String s) {
        Query query = FirebaseDatabase.getInstance().getReference("Posts").orderByChild("text_event").startAt(s).endAt(s + "\uf8ff");
        query.addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
                mPostList.clear();
                for (DataSnapshot snapshot : dataSnapshot.getChildren()) {
                    Post post = snapshot.getValue(Post.class);
                    mPostList.add(post);
                }

                mSearchEventsAdapter.notifyDataSetChanged();
            }

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

            }
        });
    }

    private void readEvents() {
        DatabaseReference reference = FirebaseDatabase.getInstance().getReference("Posts");
        reference.addListenerForSingleValueEvent(new ValueEventListener() {
            @Override
            public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
                if (mSearchBar.getText().toString().equals("")) {
                    mPostList.clear();
                    for (DataSnapshot snapshot : dataSnapshot.getChildren()) {
                        Post post = snapshot.getValue(Post.class);
                        if (post != null) {
                            mPostList.add(post);
                        }
                    }

                    mSearchEventsAdapter.notifyDataSetChanged();
                }
            }

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

            }
        });
    }

    @Override
    public void onAttach(@NonNull Context context) {
        super.onAttach(context);

        if (context instanceof Activity) {
            mActivity = (Activity) context;
        }
    }
}
JMB
  • 313
  • 2
  • 9
  • Did you tried using [String.equalsIgnoreCase](https://docs.oracle.com/javase/7/docs/api/java/lang/String.html#equalsIgnoreCase(java.lang.String))? – AgentP Jun 14 '20 at 14:12
  • @PraveenSP where would you recommend to use that method within the backend firebase call? – luk2302 Jun 14 '20 at 14:17
  • I guess the startAt + endAt logic is taken from https://stackoverflow.com/questions/50812056/making-a-firebase-query-search-not-case-sensitive ? Have you taken a look at https://stackoverflow.com/questions/38590937/firebase-query-methods-startat-taking-case-sensitive-parameters ? – luk2302 Jun 14 '20 at 14:19
  • @PraveenSP I tried that one, but was unsuccessful. Most of the stuff that I have seen wants you to change the String string = s.toString().toLowercase(); or something like this, but I need for when a user types in the search for the String to come back even though it's uppercase and the user is typing in lowercase – JMB Jun 14 '20 at 14:20
  • @luk2302 it says there is no way to do so in the second post, and that you have to store the original String with the lowercase String... Is that right? I have to store it as lowercase in the database otherwise it won't work? – JMB Jun 14 '20 at 14:24
  • I do not know, but it looks like it. – luk2302 Jun 14 '20 at 14:25
  • @luk2302 well that's a bummer... So for example, if a user saves Nature Hike, and I save two Strings to the database Nature Hike and nature hike, and set the query for the lowercase letters, then even if the user starts typing Nature... it will come up? If the query is set on the lowercase letters? – JMB Jun 14 '20 at 14:27
  • 2
    I guess you have to create an additional field called something like "searchable" and then always query using that field. When querying you have to lowercase the string first of course. – luk2302 Jun 14 '20 at 14:28
  • @luk2302 can't believe there's not a simpler way to do this. Anyways, thanks man! – JMB Jun 14 '20 at 14:30
  • Suggest you to store the values in only small letters and implement the search, If you're using CSS to show it on UI you can use text-transform:capitalize property to show the name "nature hike" as "Nature Hike" – Nagesh Tripathi Jun 27 '20 at 09:38

0 Answers0