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