title: Filter a custom Listview using Buttons (Android)
body: <p>I have a <code>Fragment</code> containing a custom <code>ListView</code> with custom <code>Adapter</code>. What I display in this list are some doctors which attributes are: <code>picture</code>, <code>name</code>, <code>age</code> and <code>gender</code>. </p>
  
  <p><em>What I would like to do is filter the doctors by <code>gender</code> when a button is pressed.</em> So that the default would display all doctors, a male button will display all males, and a female button all females. (as shown in the image below)</p>
  
  <p><img src="https://i.stack.imgur.com/YeCOz.jpg" alt="Example in this image"></p>
  
  <p>Here is my fragment code:</p>
  
  <pre><code>public class Doctors extends Fragment {
  
  
  String[] names =  {"Richard Johnson", "William Perez", "Jennifer Fields", "Caroline Marin", "John Smith"};
  String[] ages =  {"25", "32", "41", "31", "29"};
  String[] gender = {"Male", "Male", "Female", "Female", "Male"};
  int[] images = { R.drawable.delete_doctora,R.drawable.delete_doctora,R.drawable.delete_doctora,R.drawable.delete_doctora,R.drawable.delete_doctora,};
  
  ListView list;
  ArrayAdapter&lt;DocItem&gt; adapter;
  ArrayList&lt;DocItem&gt; arrayList;
  
  
  @Override
  public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState){
      View view = inflater.inflate(R.layout.activity_doctors, container, false);
  
      arrayList = new ArrayList&lt;DocItem&gt;();
  
      for(int i = 0; i &lt; 5; i++){
          arrayList.add(i,new DocItem(names[i], ages[i], gender[i], images[i]));
      }
  
      adapter = new DocAdapter(getActivity(), arrayList);
      list = (ListView) view.findViewById(R.id.ListDocs);
  
      list.setAdapter(adapter);
      list.setTextFilterEnabled(true);
  
  
      return view;
  }
  
  }
  </code></pre>
  
  <p>My custom Adapter:</p>
  
  <pre><code>public class DocAdapter extends ArrayAdapter&lt;DocItem&gt; {
  
  private Context context;
  private ArrayList&lt;DocItem&gt; _values;
  
  public DocAdapter(Context context, ArrayList&lt;DocItem&gt; values) {
      super(context, 0, values);
      this.context = context;
      _values = new ArrayList&lt;DocItem&gt;();
      _values.addAll(values);
  }
  
  @Override
  public View getView(int position, View convertView, ViewGroup parent) {
      LayoutInflater inflater = (LayoutInflater) context
              .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
      View rowView = inflater.inflate(R.layout.doc_list_row, parent, false);
      TextView txtName = (TextView) rowView.findViewById(R.id.Name);
      TextView txtAge = (TextView) rowView.findViewById(R.id.Age);
  
  
      ImageView imageView = (ImageView) rowView.findViewById(R.id.Pic);
      txtName.setText(_values.get(position)._name);
      txtAge.setText(_values.get(position)._age);
      imageView.setImageResource(_values.get(position)._picture);
  
      return rowView;
  }
  }
  </code></pre>
  
  <p>And the Item:</p>
  
  <pre><code>public class DocItem {
  
  public String _name, _age, _gender;
  int _picture;
  
  public DocItem(String name, String age, String gender, int picture){
      _name = name;
      _age = age;
      _picture= picture;
      _gender = gender;
  
  }
  
  }
  </code></pre>