ashish2py

Android search in Listview

Dec 14th, 2011
216
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.56 KB | None | 0 0
  1. package com.crumbin.tabs;
  2.  
  3. //import android.app.TabActivity;
  4. import java.util.ArrayList;
  5. import android.app.Activity;
  6. import android.os.Bundle;
  7. import android.text.Editable;
  8. import android.widget.ArrayAdapter;
  9. import android.widget.EditText;
  10. import android.widget.ListView;
  11. import android.text.TextWatcher;
  12. import com.crumbin.main.R;
  13.  
  14. public class ExploreActivity extends Activity{
  15.     private ListView lv;
  16.         private EditText et;
  17.         private String listview_array[] = { "Amit","Ashish","Guruji","Pandey gopal","Anirudh Khusape","Siddharth Menon","Alaskar","Summy Kapoor","Pardus",
  18.                                             "Amit Savartkar","Shushil","Eldhose","Gopal"};
  19.                                        
  20.        
  21.         private ArrayList<String> array_sort= new ArrayList<String>();
  22.         int textlength=0;
  23.  
  24.         public void onCreate(Bundle savedInstanceState){
  25.        
  26.             super.onCreate(savedInstanceState);
  27.             setContentView(R.layout.explore_tab);
  28.  
  29.        
  30.             lv = (ListView) findViewById(R.id.ListView01);
  31.             et = (EditText) findViewById(R.id.EditText01);
  32.             lv.setAdapter(new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1, listview_array));
  33.             et.addTextChangedListener(new TextWatcher()
  34.  
  35.             {
  36.        
  37.    
  38.                 public void afterTextChanged(Editable s){
  39.                     // Abstract Method of TextWatcher Interface.
  40.                 }
  41.    
  42.                 public void beforeTextChanged(CharSequence s,int start, int count, int after){
  43.                     // Abstract Method of TextWatcher Interface.
  44.                 }
  45.                 public void onTextChanged(CharSequence s,int start, int before, int count){
  46.                     textlength = et.getText().length();
  47.                     array_sort.clear();
  48.                     for (int i = 0; i < listview_array.length; i++){
  49.                         if (textlength <= listview_array[i].length()){
  50.                             if(et.getText().toString().equalsIgnoreCase((String)listview_array[i].subSequence(0,textlength)))
  51.                             {
  52.                                 array_sort.add(listview_array[i]);
  53.                             }
  54.                         }
  55.                     }
  56.                     lv.setAdapter(new ArrayAdapter<String>(ExploreActivity.this,android.R.layout.simple_list_item_1, array_sort));
  57.                 }
  58.             });
  59.         }
  60. }
  61.  
  62. /*
  63.  
  64. XML > explore_tab.xml
  65.  
  66. <?xml version="1.0" encoding="utf-8"?>
  67. <LinearLayout android:id="@+id/LinearLayout01"
  68.         android:layout_width="fill_parent"
  69.         android:layout_height="fill_parent"
  70.         xmlns:android="http://schemas.android.com/apk/res/android"
  71.         android:orientation="vertical">
  72.    
  73.     <EditText android:id="@+id/EditText01"
  74.     android:layout_height="wrap_content"
  75.     android:layout_width="fill_parent"
  76.     android:hint="Search">
  77.     </EditText>
  78.    
  79.     <ListView android:id="@+id/ListView01"
  80.     android:layout_width="wrap_content"
  81.     android:layout_height="wrap_content" >
  82.     </ListView>
  83. </LinearLayout>
  84.  
  85. */
Advertisement
Add Comment
Please, Sign In to add comment