title: How to apply filter in recyclerview based on spinner item selection?
body: <p>Hi i got a situation that to load images in an activity based on <code>spinner</code> selection. Here i will get those images as an ArrayList and spinner values from api. I dont know how to apply filter here based on spinner selection. Kindly come up with a solution. Below s my code.</p>
  
  <p><strong>For Activity:</strong></p>
  
  <pre><code>public class MedicalRecordsActivity extends BaseActivity implements MedicalRecordsView {
  
  @Inject
  MedicalRecordsPresenter mPresenter;
  
  @Bind(R.id.back_img_rl)
  RelativeLayout backImage;
  
  @Bind(R.id.tv_toolbartitle)
  TextView header;
  
  @Bind(R.id.upload_btn_layout)
  RelativeLayout uploadBtnLayout;
  
  @Bind(R.id.spinner_industry)
  Spinner categorySpinner;
  
  @Bind(R.id.images_recycler_view)
  RecyclerView recyclerView;
  
  List&lt;MemberImageModel&gt; arrayList;
  String patientID, imageType, title;
  EditText fileName;
  ProgressDialog categoryProgress, uploadProgress;
  
  List&lt;String&gt; uploadTypesList;
  ArrayAdapter&lt;String&gt; categoryAdapter;
  static String selectedSpinnerItem;
  File imageProfileFile;
  
  List&lt;ProfileImagesModel&gt; membersList;
  MedicalRecordsAdapter dataAdapter = null;
  
  List&lt;String&gt; Prescription = new ArrayList&lt;&gt;();
  
  private static final int RC_CAMERA_PERM = 123;
  private static final int RC_GALLERY = 124;
  private static final int RC_SETTINGS_SCREEN = 125;
  private static final int RC_WRITE_EXTERNAL_STORAGE = 126;
  
  @Override
  protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_medical_records);
      ButterKnife.bind(this);
      activityComponent().inject(this);
      mPresenter.attachView(this);
  
      header.setText("Medical Records");
      uploadBtnLayout.setVisibility(View.VISIBLE);
      patientID = PrefUtils.getPatientID(this);
      categoryProgress = new ProgressDialog(this);
      categoryProgress.setMessage(getString(R.string.loading));
  
      uploadProgress = new ProgressDialog(this);
      uploadProgress.setMessage(getString(R.string.file_uploading));
  }
  
  @Override
  public void onResume() {
      super.onResume();
      callGetProfileApi();
      callUploadTypesApi();
  }
  
  @Override
  public void onBackPressed() {
      finish();
  }
  
  @OnClick(R.id.back_img_rl)
  public void onBackClicked() {
      finish();
  }
  
  @Override
  public void getRecordType(UploadTypes response) {
      if (response != null) {
          if (response.isSuccess()) {
              uploadTypesList = new ArrayList&lt;&gt;();
              for (UploadTypes.TypesInfo typesInfo : response.getResponseObject()) {
                  uploadTypesList.add(typesInfo.getDocumentname());
              }
              loadSpinner(uploadTypesList);
  
              categorySpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
                  @Override
                  public void onItemSelected(AdapterView&lt;?&gt; parent, View view, int position, long id) {
                      selectedSpinnerItem = uploadTypesList.get(position);
                      dataAdapter.getFilter().filter(selectedSpinnerItem);
                      //callGetProfileApi();
                  }
  
                  @Override
                  public void onNothingSelected(AdapterView&lt;?&gt; parent) {
  
                  }
              });
          }
      }
  }
  
  @Override
  public void showCategoryProgress() {
      categoryProgress.show();
  }
  
  @Override
  public void hideCategoryProgress() {
      categoryProgress.dismiss();
  }
  
  @Override
  public void showUploadProgress() {
      uploadProgress.show();
  }
  
  @Override
  public void hideUploadProgress() {
      uploadProgress.dismiss();
  }
  
  @Override
  public void showProfile(ProfileResponse response) {
      if (response != null) {
          if (response.isSuccess()) {
              membersList = response.getResponseObject().get(0).getImages();
              setAdapter();
          }
      }
  }
  
  @Override
  public void showProgress() {
  
  }
  
  @Override
  public void hideProgress() {
  
  }
  
  @Override
  public void showNoInternet() {
  
  }
  
  @Override
  public void showNoData() {
  
  }
  
  @Override
  public void showError() {
  
  }
  
  private void callUploadTypesApi() {
      mPresenter.getRecordType(ApiConstants.PLATFORM_VAL, patientID, ApiConstants.ROLE_ID_VAL);
  }
  
  private void callGetProfileApi() {
      mPresenter.getProfileDetails(patientID);
  }
  
  public static String getSelectedItem() {
      return selectedSpinnerItem;
  }
  
  private void loadSpinner(List&lt;String&gt; categoryList) {
      categoryAdapter = new ArrayAdapter&lt;&gt;(this, android.R.layout.simple_spinner_item, categoryList);
      categoryAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
      categorySpinner.setAdapter(categoryAdapter);
  }
  
  private void setAdapter() {
      try {
          RecyclerView.LayoutManager layoutManager = new GridLayoutManager(this, 2);
          recyclerView.setLayoutManager(layoutManager);
          MedicalRecordsAdapter dataAdapter = new MedicalRecordsAdapter(this, membersList);
          recyclerView.setAdapter(dataAdapter);
         /* dataAdapter.getFilter().filter(selectedSpinnerItem);
          dataAdapter.notifyDataSetChanged();*/
      } catch (Exception e) {
          e.printStackTrace();
      }
  }}
  </code></pre>
  
  <p><strong>For Adapter:</strong></p>
  
  <pre><code>public class MedicalRecordsAdapter extends RecyclerView.Adapter&lt;MedicalRecordsAdapter.ViewHolder&gt; implements Filterable {
  
  Context context;
  List&lt;ProfileImagesModel&gt; membersList, filterList;
  //CustomFilter filter;
  
  public MedicalRecordsAdapter(Context context, List&lt;ProfileImagesModel&gt; membersList) {
      this.context = context;
      this.membersList = membersList;
      this.filterList = membersList;
  }
  
  @Override
  public MedicalRecordsAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
      View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.adapter_patient_record, parent, false);
      return new MedicalRecordsAdapter.ViewHolder(view);
  }
  
  @Override
  public void onBindViewHolder(MedicalRecordsAdapter.ViewHolder holder, int position) {
      holder.recordName.setText(filterList.get(position).getTitle());
      holder.recordDate.setText(filterList.get(position).getCreateddate());
      String thumbnail = filterList.get(position).getImagepath();
      Glide.with(context)
              .load(ApiEndpoint.IMAGE_URL + thumbnail)
              .into(holder.thumbnail);
  }
  
  @Override
  public int getItemCount() {
      return filterList.size();
  }
  
  @Override
  public Filter getFilter() {
      /*if (filter == null) {
          filter = new CustomFilter(filterList, this);
      }
      return filter;*/
      return new Filter() {
          @Override
          protected FilterResults performFiltering(CharSequence constraint) {
              String charString = constraint.toString();
  
              if (charString.isEmpty()) {
  
                  filterList = membersList;
              } else {
  
                  List&lt;ProfileImagesModel&gt; filteredList = new ArrayList&lt;&gt;();
  
                  for (ProfileImagesModel androidVersion : membersList) {
  
                      if (androidVersion.getType().contains(charString) /*|| androidVersion.getName().toLowerCase().contains(charString) || androidVersion.getVer().toLowerCase().contains(charString)*/) {
  
                          filteredList.add(androidVersion);
                      }
                  }
  
                  filterList = filteredList;
              }
  
              FilterResults filterResults = new FilterResults();
              filterResults.values = filterList;
              return filterResults;
          }
  
          @Override
          protected void publishResults(CharSequence constraint, FilterResults results) {
              filterList = (List&lt;ProfileImagesModel&gt;) results.values;
              notifyDataSetChanged();
          }
      };
  }
  
  public class ViewHolder extends RecyclerView.ViewHolder {
  
      private TextView recordName;
      private TextView recordDate;
      private ImageView thumbnail;
  
      public ViewHolder(View itemView) {
          super(itemView);
          recordName = (TextView) itemView.findViewById(R.id.small_card_record);
          recordDate = (TextView) itemView.findViewById(R.id.small_card_date);
          thumbnail = (ImageView) itemView.findViewById(R.id.imageview_small_card_thumbnail);
      }
  }}
  </code></pre>