title: How do i add search functionality in gridview in android?
body: <p>My android app consists of a gridview with an image and a text below it. I am passing a array of strings into the textview. I want the edittext field to act as a search bar and filter the results.I have seen other questions relating this but they seem to filter an arraylist which is passed into a Listview. How do i filter an array of strings in a gridview?</p>
  
  <p>I know i should add a a TextWatcher to listen for text changes on the EditText but after that how do i filter my data?</p>
  
  <p>Here is my code</p>
  
  <p><strong>Mainactivity.java</strong></p>
  
  <pre><code>public class MainActivity extends Activity {
  EditText inputSearch;
  GridView grid;
  
  public static String[] Sname = {
          "one",
          "two",
          "three",
          "four",
  
  };
  
  
  @Override
  protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);
  
      final CustomGrid adapter = new CustomGrid(MainActivity.this, Sname);
      grid = (GridView) findViewById(R.id.grid);
      grid.setAdapter(adapter);
      grid.setOnItemClickListener(new AdapterView.OnItemClickListener() {
  
          @Override
          public void onItemClick(AdapterView&lt;?&gt; parent, View view,
                                  int position, long id) {
              Toast.makeText(MainActivity.this, "You Clicked at " + Sname[+position], Toast.LENGTH_SHORT).show();
  
          }
      });
  
      inputSearch = (EditText) findViewById(R.id.editText);
  
  
      }
  }
  </code></pre>
  
  <p>CustomGrid.java</p>
  
  <pre><code>public class CustomGrid extends BaseAdapter {
  private Context mContext;
  private final String[] Sname;
  
  
  public CustomGrid(Context c,String[] Sname) {
      mContext = c;
  
      this.Sname = Sname;
  }
  
  @Override
  public int getCount() {
      // TODO Auto-generated method stub
      return Sname.length;
  }
  
  @Override
  public Object getItem(int position) {
      // TODO Auto-generated method stub
      return null;
  }
  
  @Override
  public long getItemId(int position) {
      // TODO Auto-generated method stub
      return 0;
  }
  
  
  
  @Override
  public View getView(int position, View view, ViewGroup parent) {
      // TODO Auto-generated method stub
      View grid;
      LayoutInflater inflater = (LayoutInflater) mContext
              .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
  
  
          grid = new View(mContext);
          grid = inflater.inflate(R.layout.grid_single, null);
          TextView textView = (TextView) grid.findViewById(R.id.grid_text);
          ImageView imageView = (ImageView)grid.findViewById(R.id.grid_image);
          textView.setText(Sname[position]);
          imageView.setImageResource(R.mipmap.image1);
  
      return grid; }}
  </code></pre>