Skip to content Skip to sidebar Skip to footer

How To Implement A Search For A Listview That Uses Listadapater, Arraylist And Hashmap

I have a list view that I populate from a JSON array. First the values are extracted from the JSON array, then put into a HashMap. Then this HashMap is added to an ArrayList. From

Solution 1:

As per you description I believe you have custom adapter implementation for your list view. In case of custom adapters yon need to create your own filters by implementing http://developer.android.com/reference/android/widget/Filterable.html

or

http://developer.android.com/reference/android/widget/Filter.html

Below is the simple tutorial to take inspiration

http://www.survivingwithandroid.com/2012/10/android-listview-custom-filter-and.html

========== Example for simple search ===

You need to create an Edit text on top of list view

<EditText
         android:id="@+id/editTxt"
         android:layout_width="match_parent"
         android:layout_height="wrap_content"
         android:layout_marginTop="5dp"
         android:maxLines="1" />

Then apply text change listener to this edit text

editTxt.addTextChangedListener(newTextWatcher() {

    @OverridepublicvoidonTextChanged(CharSequence s, int start, int before, int count) {
        adapter.getFilter().filter(s.toString());                           
    }

    @OverridepublicvoidbeforeTextChanged(CharSequence s, int start, int count,
            int after) {

    }

    @OverridepublicvoidafterTextChanged(Editable s) {
    }
});

and you are done.

Post a Comment for "How To Implement A Search For A Listview That Uses Listadapater, Arraylist And Hashmap"