try this one,whenever u enter text in the edittext ,the list will show filtered result as i have shown the stuff in images
Initial:
Filtered:
this is main.xml
<?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="fill_parent"
android:orientation="vertical" >
<EditText
android:id="@+id/etSearchbox"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
<ListView
android:id="@+id/lvFirst"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
></ListView>
</LinearLayout>
this is FilterListActivity.java
package com.filterlist;
import android.app.Activity;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.ListView;
public class FilterListActivity extends Activity{
EditText etSearchbox;
ListView lvFirst;
ArrayAdapter<String> adapter1;
String[] data = {"mehul joisar","amit mishra","amitabh","Aamir khan","jesica","katrina"};
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
etSearchbox=(EditText)findViewById(R.id.etSearchbox);
lvFirst=(ListView)findViewById(R.id.lvFirst);
lvFirst.setTextFilterEnabled(true);
adapter1 = new ArrayAdapter<String>(FilterListActivity.this, android.R.layout.simple_list_item_1, data);
lvFirst.setAdapter(adapter1);
etSearchbox.addTextChangedListener(new TextWatcher() {
@Override
public void onTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {
// TODO Auto-generated method stub
FilterListActivity.this.adapter1.getFilter().filter(arg0);
}
@Override
public void beforeTextChanged(CharSequence arg0, int arg1, int arg2,
int arg3) {
// TODO Auto-generated method stub
}
@Override
public void afterTextChanged(Editable arg0) {
// TODO Auto-generated method stub
}
});
}
}
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…