Implementing ListView With Search

Today I will be going to explain how to add search functionality in list view. The android ListView contains many features that make ListView a powerful tool.

Let’s see the steps on how to perform search operation on Array and redraw the ListView.

Step 1) Create a new project in android IDE.

Write the following code into your User interface i.e. main activity layout file:

 

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent" >

<RelativeLayout
android:id="@+id/top"
android:layout_width="match_parent"
android:layout_height="40dp"
android:layout_margin="5dp"
android:background="@drawable/search_input1" >

<Button
android:id="@+id/searchButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:layout_marginRight="2dp"
android:background="@drawable/cancel_search" />

<Button
android:id="@+id/LeftButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:layout_marginLeft="2dp"
android:background="@drawable/icon_search" />

<EditText
android:id="@+id/SearchEditText"
android:layout_width="match_parent"
android:layout_height="40dp"
android:layout_centerVertical="true"
android:layout_toLeftOf="@id/searchButton"
android:layout_toRightOf="@id/LeftButton"
android:background="@null"
android:hint="Search"
android:imeOptions="actionSearch"
android:singleLine="true" />
</RelativeLayout>

<ListView
android:id="@+id/mListView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@id/top"
android:layout_marginBottom="5dp"
android:layout_marginTop="5dp"
android:cacheColorHint="@android:color/transparent"
android:divider="@android:color/white"
android:dividerHeight="2dp" >
</ListView>

</RelativeLayout>

 

 

Step 2) Create One BaseAdapter to bind Data witha ListView

 

package com.example.listviewsearch;

import java.util.ArrayList;
import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

public class SearchAdapter extends BaseAdapter {

private ArrayList<String> mData = new ArrayList<String>();
private LayoutInflater mInflater;

public SearchAdapter(Activity activity) {
mInflater = (LayoutInflater) activity
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}

public void addItem(String item) {
mData.add(item);
notifyDataSetChanged();
}

public int getCount() {
return mData.size();
}

public String getItem(int position) {
return mData.get(position);
}

public long getItemId(int position) {
return position;
}

public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder = null;
if (convertView == null) {
holder = new ViewHolder();
convertView = mInflater.inflate(R.layout.item_one, null);
holder.textView = (TextView) convertView.findViewById(R.id.text);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
String str = mData.get(position);
holder.textView.setText(str);
return convertView;
}

public class ViewHolder {
public TextView textView;
}

}

 

 

 

Step 3) Now write the Search Logic inside your activity


 

</code>

package com.example.listviewsearch;

import java.util.ArrayList;

import android.app.Activity;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.TextView.OnEditorActionListener;
import android.widget.Toast;

public class MainActivity extends Activity implements OnClickListener,
OnEditorActionListener, OnItemClickListener {
ListView mListView;
SearchAdapter mAdapter;
Button btnSearch, btnLeft;
EditText mtxt;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mListView = (ListView) findViewById(R.id.mListView);
mAdapter = new SearchAdapter(this);
btnSearch = (Button) findViewById(R.id.searchButton);
btnLeft = (Button) findViewById(R.id.LeftButton);
mtxt = (EditText) findViewById(R.id.SearchEditText);
mtxt.addTextChangedListener(new TextWatcher() {

@Override
public void onTextChanged(CharSequence s, int start, int before,
int count) {

}

@Override
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {

}

@Override
public void afterTextChanged(Editable s) {
if (0 != mtxt.getText().length()) {
String spnId = mtxt.getText().toString();
setSearchResult(spnId);
} else {
setData();
}
}
});
btnLeft.setOnClickListener(this);
btnSearch.setOnClickListener(this);
setData();
}

ArrayList<String> mAllData;

String[] str = { "India", "USA", "UK", "Ireland",
"Iceland", "Canada", "Pakistan", "China",
"Nepal", "Israel", "Iran" };

public void setData() {
mAllData = new ArrayList<String>();
mAdapter = new SearchAdapter(this);
for (int i = 0; i < str.length; i++) {
mAdapter.addItem(str[i]);
mAllData.add(str[i]);
}
mListView.setOnItemClickListener(this);
mListView.setAdapter(mAdapter);
}

@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.searchButton:
mtxt.setText("");
setData();
break;
case R.id.LeftButton:

break;
}
}

public void setSearchResult(String str) {
mAdapter = new SearchAdapter(this);
for (String temp : mAllData) {
if (temp.toLowerCase().contains(str.toLowerCase())) {
mAdapter.addItem(temp);
}
}
mListView.setAdapter(mAdapter);
}

@Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
return false;
}

@Override
public void onBackPressed() {
setResult(Activity.RESULT_CANCELED);
finish();
}

@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int position,
long arg3) {
String str = mAdapter.getItem(position);
Toast.makeText(this, str, Toast.LENGTH_LONG).show();
}
}

 

 

 

Step4) Write following into item_one.xml:

 

</code>

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >

<TextView
android:id="@+id/text"
android:layout_width="fill_parent"
android:layout_height="40dp"
android:gravity="center_vertical"
android:paddingLeft="10dp"
android:textColor="#FF0000"
android:textSize="17sp"
android:visibility="visible" />

</LinearLayout>

<code>

 

 

 

OutPut

listview1

listview2

Hope this tutorial helped you in your coding and you have a good coding experience.

Leave a Comment:

1 comment
karthi says June 24, 2015

Hi,
Good Afternoon, have a nice day.
As i am developing a shopping cart application using android i have some doubts how to use back end mysql, using my website or API
can you please send me the shopping cart source it is possible?

Reply
Add Your Reply