Hello friends Today I will write a code for list view with search step by step :
STEP 1: Write this code in MainActivity
package com.ritesh.listviewsearch;
import java.util.ArrayList;
import java.util.HashMap;
import android.os.Bundle;
import android.app.Activity;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.Toast;
public class MainActivity extends Activity implements TextWatcher, OnItemClickListener {
private ArrayList<HashMap<String,String>> listhash;
private ArrayList<HashMap<String,String>> listhash_second;
private ListView listset;
private EditText edittext;
private ListViewAdapter adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
edittext=(EditText)findViewById(R.id.editText_search);
listset=(ListView)findViewById(R.id.list);
listhash=new ArrayList<HashMap<String,String>>();
listhash_second=new ArrayList<HashMap<String,String>>();
//Insert Data
for (int i = 0; i < 20; i++) {
HashMap<String, String> map=new HashMap<String, String>();
map.put("name", i+"ritesh");
map.put("phone", "99999999:"+i);
listhash.add(map);
listhash_second.add(map);
}
for (int i = 0; i < 20; i++) {
HashMap<String, String> map=new HashMap<String, String>();
map.put("name", i+"android");
map.put("phone", "9656599:"+i);
listhash.add(map);
listhash_second.add(map);
}
adapter=new ListViewAdapter(MainActivity.this,listhash);
listset.setAdapter(adapter);
listset.setOnItemClickListener(this);
edittext.addTextChangedListener(this);
}
@Override
public void afterTextChanged(Editable arg0) {
// TODO Auto-generated method stub
}
@Override
public void beforeTextChanged(CharSequence arg0, int arg1, int arg2,
int arg3) {
// TODO Auto-generated method stub
}
@Override
public void onTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {
// TODO Auto-generated method stub
int textlength = arg3+1;
String textstring =String.valueOf(arg0);
Log.d("textlength", ""+textlength);
Log.d("textstring", ""+textstring);
listhash.clear();
for (int i = 0; i < listhash_second.size(); i++) {
String name = listhash_second.get(i).get("name");
if (textlength <= name.length()) {
if(name.contains(edittext.getText().toString()))
{
listhash.add(listhash_second.get(i));
} }
}
adapter.notifyDataSetChanged();
}
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
// TODO Auto-generated method stub
Toast.makeText(MainActivity.this, listhash.get(arg2).get("name"), Toast.LENGTH_LONG).show();
}
}
STEP 2: Write this code in ListViewAdapter
package com.ritesh.listviewsearch;
import java.util.ArrayList;
import java.util.HashMap;
import android.app.Activity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;
public class ListViewAdapter extends BaseAdapter{
LayoutInflater layoutinflater;
ArrayList<HashMap<String, String>> listhash;
Activity activity;
private ViewHolder holder;
public ListViewAdapter(Activity activity,
ArrayList<HashMap<String, String>> listhash) {
// TODO Auto-generated constructor stub
this.listhash=listhash;
this.activity=activity;
this.layoutinflater=LayoutInflater.from(activity);
}
private class ViewHolder {
public TextView tvname;
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return listhash.size();
}
@Override
public Object getItem(int arg0) {
// TODO Auto-generated method stub
return null;
}
@Override
public long getItemId(int arg0) {
// TODO Auto-generated method stub
return 0;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
if (convertView == null) {
convertView = layoutinflater.inflate(R.layout.list_item,
null);
holder = new ViewHolder();
holder.tvname = (TextView) convertView
.findViewById(R.id.text_name);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.tvname.setText(listhash.get(position).get("name"));
return convertView;
}
}
STEP 3 : Write this code in activity_main.xml
<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" >
<EditText
android:id="@+id/editText_search"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="25dp"
android:layout_marginRight="25dp"
android:layout_alignParentTop="true"
android:hint="Search Name"
android:layout_centerHorizontal="true"
android:ems="10" >
</EditText>
<ListView
android:id="@+id/list"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/editText_search"
android:layout_centerHorizontal="true"
android:cacheColorHint="@android:color/transparent"
android:layout_marginTop="4dp" >
</ListView>
</RelativeLayout>
STEP 4 : Write this code in list_item.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="50dp"
android:orientation="vertical" >
<TextView
android:id="@+id/text_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:layout_marginLeft="56dp"
android:text="Name"
android:textAppearance="?android:attr/textAppearanceMedium" />
</RelativeLayout>
STEP 5 : Write this code in manifest file
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.ritesh.listviewsearch"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="8" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.ritesh.listviewsearch.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
Run the Project....
No comments:
Post a Comment