The concept covered here is search and filter but with a listview. Here are the components used:
- ListView
- SearchView.
Feel free to post an example you’ve written.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Oclemy
Android Custom ListView Search/Filter and ItemClickListener
Android Custom ListView Search/Filter and ItemClickListener tutorial.
In this piece we see how to search/filter a ListView then handle the itemClick events for the searched cardviews.
Many a times you want to search data. Searching data in a fundamntal thing in working with computers.
We human beings can only work with a small subset of data at a given time.
Searching helps us in this as we filter only the required data and work with it.
In this tutorial we see how to search/filter a custom listview with images and text and handle onItemClickListener after searching.
Let’s go.
1. Create Basic Activity Project
Basic activity will have a toolbar and floating action button already added in the layout
Normally two layouts get generated with this option:
In this example I used a basic activity.
The activity will automatically be registered in the android_manifest.xml. Android Activities are components and normally need to be registered as an application component.
If you’ve created yours manually then register it inside the
<application>...<application>
as following, replacing theMainActivity
with your activity name:You can see that one action and category are specified as intent filters. The category makes our MainActivity as launcher activity. Launcher activities get executed first when th android app is run.
Advantage of Creating Basic Activity project
You can optionally choose empty activity over basic activity for this project.
However basic activity has the following advantages:
Generated Project Structure
AndroidStudio will generate for you a project with default configurations via a set of files and directories.
Here are the most important of them:
build/
R.java
file.R.java
file normally holds the references to application resources.libs/
src/main/
src/main/java/
src/main/res/
src/main/res/drawable/
src/main/res/layout/
src/main/res/menu/
src/main/res/values/
AndroidManifest.xml
build.gradle
3. Create User Interface
User interfaces are typically created in android using XML layouts as opposed by direct java coding.
This is an example fo declarative programming.
######### Advantages of Using XML over Java
Here are our layouts for this project:
(a). activity_main.xml
Here are some of the widgets, views and viewgroups that get employed”
android.support.design.widget
android.support.design.widget
<android.support.v7.widget
android.support.design.widget
(b). content_main.xml
This layout gets included in your activity_main.xml.
We are going to add a ListView and SearchView here. We’ll arrange them in a vertical linear layout with searchview on top of ListView.
Users will use the SearchView to search/filter the ListView.
(c). model.xml
This layout will define our custom rows in our ListView.
This a custom listview we are searching so we need a model layout to model each row.
In this case we have image and text.
Let’s now jump to our Java classes.
4. Movie.java
This is basically our POJO class.
We will be showing lists of movies in our ListView.
5. ItemClickListener.java
This is an interface that defines us our event handler signature.
6. MyViewHolder.java
This is our view holder class.
The class will hold our imageview and textview so that they can be reused by the adapter.
6. MyAdapter.java
This is our adapter class. It derives from
android.widget.BaseAdapter
.This class will bind our data to our inflated
model.xml
view object. It will also inflate that layout in the first place.This class will also implement
Filterable
interface and override thegetFilter()
method.7. CustomFilter.java
This is the class that will implement our actual search. We will be searching with the
contains()
method of a String class.This class derives from
android.widget.Filter
.8. MainActivity.java
This is our main activity class. It derives from
android.app.AppCompatActivity
class.Download