| salam here a example public Filter getFilter() { return new Filter() { @Override protected FilterResults performFiltering(CharSequence constraint) { final FilterResults oReturn = new FilterResults(); final ArrayList results = new ArrayList (); if (orig == null) orig = items; if (constraint != null) { if (orig != null && orig.size() > 0) { for (final station g : orig) { if (g.getName().toLowerCase() .contains(constraint.toString())) results.add(g); } } oReturn.values = results; } return oReturn; } @SuppressWarnings("unchecked") @Override protected void publishResults(CharSequence constraint, FilterResults results) { items = (ArrayList ) results.values; notifyDataSetChanged(); } };}public void notifyDataSetChanged() { super.notifyDataSetChanged(); notifyChanged = true;} Add search function to Custom Listview [March 10, 2013 at 8:48 AM] 2,023 views 1. Introduce Adding search functionality to listview will filters the list data with a matching string, hence provides user an easy way to find the information he needs. In this simple tutorial i am discussing how to enable search filter to android custom ListView. In this tutorial,we will display list of picture and search picture by its name. 2. Filter Method in Adapter To filter the list data with a matching string from user,we use a Filter method in Adapter. | | public void filter(String charText) { charText = charText.toLowerCase(); if (charText.length() == 0 ) { picList.addAll(listpicOrigin); for (Picture pic : listpicOrigin) { if (pic.getPicName().toLowerCase().contains(charText)) { | | 3. Create XML layout activity_main.xml | | < LinearLayout xmlns:android = "" android:id = "@+id/LinearLayout1" android:layout_width = "match_parent" android:layout_height = "match_parent" android:orientation = "vertical" > android:id = "@+id/viewFlipper1" android:layout_width = "match_parent" android:layout_height = "fill_parent" > android:id = "@+id/listpic_layout" layout = "@layout/listpic_layout" /> android:id = "@+id/viewpic_layout" layout = "@layout/view_pic_layout" /> | | listpic_layout.xml | | < LinearLayout xmlns:android = "" android:id = "@+id/LinearLayout1" android:layout_width = "match_parent" android:layout_height = "match_parent" android:orientation = "vertical" > android:id = "@+id/search_edt" android:layout_width = "match_parent" android:layout_height = "wrap_content" android:drawableRight = "@drawable/search" android:hint = "Search pic name" > android:id = "@+id/listView1" android:layout_width = "match_parent" android:layout_height = "wrap_content" android:dividerHeight = "3dp" > | | list_item.xml | | <? xml version = "1.0" encoding = "utf-8" ?> < LinearLayout xmlns:android = "" android:id = "@+id/LinearLayout1" android:layout_width = "match_parent" android:layout_height = "wrap_content" android:orientation = "horizontal" > android:id = "@+id/pic_icon_img" android:layout_width = "120dp" android:layout_height = "120dp" android:scaleType = "fitXY" android:src = "@drawable/ic_launcher" /> android:layout_width = "wrap_content" android:layout_height = "match_parent" android:layout_weight = "1" android:orientation = "vertical" > android:id = "@+id/pic_name_txt" android:layout_width = "fill_parent" android:layout_height = "wrap_content" android:layout_gravity = "center" android:layout_marginTop = "10dp" android:layout_weight = "1" android:gravity = "center_vertical" android:text = "Pic Name" android:textAppearance = "?android:attr/textAppearanceLarge" android:textStyle = "bold" /> android:id = "@+id/pic_type_txt" android:layout_width = "fill_parent" android:layout_height = "wrap_content" android:layout_weight = "1" android:text = "Pic Type" android:textAppearance = "?android:attr/textAppearanceMedium" /> android:id = "@+id/imageView2" android:layout_width = "wrap_content" android:layout_height = "wrap_content" android:layout_gravity = "center_vertical" android:src = "@drawable/arrow" /> | | view_pic_layout.xml | | <? xml version = "1.0" encoding = "utf-8" ?> < LinearLayout xmlns:android = "" android:layout_width = "match_parent" android:layout_height = "match_parent" android:orientation = "vertical" > android:id = "@+id/viewpic_img" android:layout_width = "fill_parent" android:layout_height = "fill_parent" android:scaleType = "fitCenter" android:src = "@drawable/bikinigirl_3" /> | | 4. Code application PicListAdapter.java | | public class PicListAdapter extends BaseAdapter { private Context mContext; private LayoutInflater mInflater; private List<Picture> picList = null ; private ArrayList<Picture> listpicOrigin; public PicListAdapter(Context context, List<Picture> picList) { mInflater = LayoutInflater.from(mContext); this .listpicOrigin = new ArrayList<Picture>(); this .listpicOrigin.addAll(picList); public class ViewHolder { public View getView( int position, View view, ViewGroup parent) { holder = new ViewHolder(); view = mInflater.inflate(R.layout.list_item, null ); holder.picName = (TextView) view.findViewById(R.id.pic_name_txt); holder.picType = (TextView) view.findViewById(R.id.pic_type_txt); holder.picIcon = (ImageView) view.findViewById(R.id.pic_icon_img); holder = (ViewHolder) view.getTag(); holder.picName.setText(picList.get(position).getPicName()); holder.picType.setText(picList.get(position).getPicType()); holder.picIcon.setImageResource(picList.get(position).getPicSource()); public Picture getItem( int position) { return picList.get(position); public long getItemId( int position) { public void filter(String charText) { charText = charText.toLowerCase(); if (charText.length() == 0 ) { picList.addAll(listpicOrigin); for (Picture pic : listpicOrigin) { if (pic.getPicName().toLowerCase().contains(charText)) { | | MainActivity.java | | public class MainActivity extends Activity implements TextWatcher, private static final int LIST_PIC_SCREEN = 0 ; private static final int VIEW_PIC_SCREEN = 1 ; private String[] listPicName = { "androidgirl_1" , "androidgirl_2" , "androidgirl_3" , "beautifulgirl_1" , "beautifulgirl_2" , "bikinigirl_1" , "bikinigirl_2" , "bikinigirl_3" , "cutegirl_1" , "cutegirl_2" , "cutegirl_3" , "cutegirl_4" , "cutegirl_5" , "hotgirl_1" , "hotgirl_2" , "hotgirl_3" , "nudegirl_1" , "nudegirl_2" , "nudegirl_3" , "nudegirl_4" , "sexygirl_1" , "sexygirl_2" , "sexygirl_3" , "sportgirl_1" , "sportgirl_2" , "sportgirl_3" , private String[] listPicType = { "android" , "android" , "android" , "beautiful" , "beautiful" , "bikini" , "bikini" , "bikini" , "cutegirl" , "cutegirl" , "cutegirl" , "cutegirl" , "cutegirl" , "hotgirl" , "hotgirl" , "hotgirl" , "nude" , "nude" , "nude" , "nude" , "sexy" , "sexy" , "sexy" , "sport" , "sport" , "sport" , "sport" }; private int [] listPicDrawable = { R.drawable.androidgirl_1, R.drawable.androidgirl_2, R.drawable.androidgirl_3, R.drawable.beautifulgirl_1, R.drawable.beautifulgirl_2, R.drawable.bikinigirl_1, R.drawable.bikinigirl_2, R.drawable.bikinigirl_3, R.drawable.cutegirl_1, R.drawable.cutegirl_2, R.drawable.cutegirl_3, R.drawable.cutegirl_4, R.drawable.cutegirl_5, R.drawable.hotgirl_1, R.drawable.hotgirl_2, R.drawable.hotgirl_3, R.drawable.nudegirl_1, R.drawable.nudegirl_2, R.drawable.nudegirl_3, R.drawable.nudegirl_4, R.drawable.sexygirl_1, R.drawable.sexygirl_2, R.drawable.sexygirl_3, R.drawable.sportgirl_1, R.drawable.sportgirl_2, R.drawable.sportgirl_3, R.drawable.sportgirl_4 private ArrayList<Picture> listPic = new ArrayList<Picture>(); private ListView listview; private PicListAdapter adapter; private EditText searchEdt; private ViewFlipper fliper; private ImageView viewpic; public void onCreate(Bundle savedInstanceState) { super .onCreate(savedInstanceState); setContentView(R.layout.activity_main); fliper = (ViewFlipper) findViewById(R.id.viewFlipper1); listview = (ListView) findViewById(R.id.listView1); for ( int i = 0 ; i < listPicName.length; i++) { Picture pic = new Picture(listPicName[i], listPicType[i], adapter = new PicListAdapter( this , listPic); listview.setAdapter(adapter); listview.setOnItemClickListener( this ); searchEdt = (EditText) findViewById(R.id.search_edt); searchEdt.addTextChangedListener( this ); viewpic = (ImageView) findViewById(R.id.viewpic_img); public boolean onCreateOptionsMenu(Menu menu) { getMenuInflater().inflate(R.menu.activity_main, menu); public void beforeTextChanged(CharSequence s, int start, int count, public void onTextChanged(CharSequence s, int start, int before, int count) { public void afterTextChanged(Editable s) { String text = searchEdt.getText().toString().toLowerCase(); public void onItemClick(AdapterView<?> parent, View view, int position, Sliding.slideFromRightToLeft(VIEW_PIC_SCREEN, fliper); viewpic.setImageResource(listPic.get(position).getPicSource()); * @param : event, keycode of downed button * @Objective : Handle keyevent on main activity public boolean onKeyDown( int keyCode, KeyEvent event) { if (keyCode == KeyEvent.KEYCODE_BACK) { int screen = fliper.getDisplayedChild(); if (screen == VIEW_PIC_SCREEN) { Sliding.slideFromLeftToRight(LIST_PIC_SCREEN, fliper); return super .onKeyDown(keyCode, event); | | 5. Conclusion If you want to hide keyboard when load Activity,you put android:windowSoftInputMode=”stateHidden” into AndroidManifest.xml | | < manifest xmlns:android = "" package = "com.example.searchinginlistview" android:versionCode = "1" android:versionName = "1.0" > android:minSdkVersion = "8" android:targetSdkVersion = "15" /> android:icon = "@drawable/icon" android:label = "@string/app_name" android:theme = "@style/AppTheme" > android:name = ".MainActivity" android:label = "@string/title_activity_main" android:windowSoftInputMode = "stateHidden" < action android:name = "android.intent.action.MAIN" /> < category android:name = "android.intent.category.LAUNCHER" /> | | In this tutorial, we have known about how to enable search functionality to custom Listview. You can improve it to enable search functionality to custom Expandable Listview. |