Ad Code

How to search in ArrayList by value (ArrayList<HashMap<String, Object>>) | Integrate smooth search by ArrayList Hashmap value without lagging in Android Studio | Search EditText searching progress listener

Hello developers, if you want to integrate a smooth search program into your project, then you are in the right place. In this Article we will show you how to setup a smooth search EditText for searching in a huge ArrayList without lagging. It allows us to quickly find the desired data without having to manually look through all the elements in the data structure.

 In this Article, we will explore how to search for values in ArrayList, HashMap, and String using an EditText in Android Studio. For smooth experience we have used Runnable interface in this project. We have also added onSearchingStarted and onSearchFinished function which helps you to integrate a progress while searching by the user. you can assume onSearchingStarted function as onSearchingProgressListener. You can easily integrate this searching program into your existing project or into a new project.

Arraylist hashmap string search


Integrate Search in ArrayList by value (ArrayList<HashMap<String, Object>>)


Searching for a specific element in a collection such as ArrayList, HashMap, or String is a common task in Android development. The search can be performed by user input in an EditText or by a predefined search query.

Setup searching will an Edittext and ArrayList<HashMap<String, Object>>. You can call this function after initialized the list where you integrate the searching program.

Setup Searching
  private void setSearchArrayList(ArrayList<HashMap<String , Object>> arrayList, EditText editText) {

        editText.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

                Handler handler = new Handler();
                onSearchingStarted();
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        final String _charSeq = charSequence.toString();
                        ArrayList<HashMap<String, Object>> tempList = new ArrayList<>();
                        String charSeq = _charSeq.trim();

                        try {

                            if (charSeq.length() > 0) {
                                for (int in = 0; in < arrayList.size(); in++) {
                                    if(arrayList.get(in).toString().contains(_charSeq)) {
                                        tempList.add(arrayList.get(in));
                                    }
                                }
                            } else {
                                tempList.addAll(arrayList);
                            }

                        } catch (Exception e) {}

                        handler.post(new Runnable() {
                            @Override
                            public void run() {
                                onSearchedListener(tempList);
                                onSearchFinished();
                            }
                        });


                    }
                }).start();

            }

            @Override
            public void afterTextChanged(Editable editable) {

            }

        });

    }

onSearchedListener will return an ArrayList<HashMap<String, Object>> which contains the content of the Edittext. If Srearch Edittext does not contains any text then it will return the entire ArayyList.

On Searched
 private void onSearchedListener(ArrayList<HashMap<String , Object>> arrayList) {

       // arrayList is the list which contains searched content
        
    }

onSearchingStarted() will call while the text of the EditText will change. You can use this function for integrating a progress while searching progress.

On Searching Started
 private void onSearchingStarted() {
        // you can set loading here while searching
        //this is useful while searching in a huge list
    }

onSearchFinished() will call after searching completed. You can disable your loading progress if you have integrated in the onSearchingStarted() function.

On Search Finished
  private void onSearchFinished() {
       //on Search finished
    }


Searching for elements in collections such as ArrayList, HashMap, or String using an EditText is a common task in Android development. By following these simple steps, you can easily implement a search feature in your app.

Searching for an item in an ArrayList, HashMap, or String in Android Studio is a simple process that can make working with large amounts of data more efficient and organized. By using an EditText to receive user input, you can make the search feature more user-friendly and customizable.

What is HashMap in programming?

HashMap is a data structure that stores key-value pairs. It is part of the Java Collections Framework and provides a way to map keys to values. We can add key-value pairs to a HashMap using the put() method and remove them using the remove() method. We can also access values using their associated key using the get() method.

What is ArrayList in programming?

ArrayList is a dynamic array implementation that can grow or shrink as needed. It is part of the Java Collections Framework and provides a convenient way to store a collection of elements that can be accessed and modified easily. We can add elements to an ArrayList using the add() method and remove them using the remove() method. We can also access elements using their index position using the get() method.

What is String in programming?

String is a built-in data type in Java that represents a sequence of characters. It is an immutable data type, which means that once a String object is created, its value cannot be changed. We can access individual characters in a String using the charAt() method and concatenate Strings using the + operator.

Thank you for visiting our website. If you have any question or facing any error, then you can contact us or comment in this article.

Post a Comment

0 Comments

Ad Code