如何根据微调项目选择在recyclerview中应用过滤器?

时间:2017-06-30 08:53:56

标签: java android filter android-recyclerview spinner

嗨,我遇到了根据spinner选择在活动中加载图片的情况。在这里,我将从Api获取这些图像作为ArrayList和spinner值。我不知道如何根据微调器选择应用过滤器。请提出解决方案。在我的代码下面。

活动:

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<MemberImageModel> arrayList;
String patientID, imageType, title;
EditText fileName;
ProgressDialog categoryProgress, uploadProgress;

List<String> uploadTypesList;
ArrayAdapter<String> categoryAdapter;
static String selectedSpinnerItem;
File imageProfileFile;

List<ProfileImagesModel> membersList;
MedicalRecordsAdapter dataAdapter = null;

List<String> Prescription = new ArrayList<>();

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<>();
            for (UploadTypes.TypesInfo typesInfo : response.getResponseObject()) {
                uploadTypesList.add(typesInfo.getDocumentname());
            }
            loadSpinner(uploadTypesList);

            categorySpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
                @Override
                public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                    selectedSpinnerItem = uploadTypesList.get(position);
                    dataAdapter.getFilter().filter(selectedSpinnerItem);
                    //callGetProfileApi();
                }

                @Override
                public void onNothingSelected(AdapterView<?> 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<String> categoryList) {
    categoryAdapter = new ArrayAdapter<>(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();
    }
}}

适用于适配器:

public class MedicalRecordsAdapter extends RecyclerView.Adapter<MedicalRecordsAdapter.ViewHolder> implements Filterable {

Context context;
List<ProfileImagesModel> membersList, filterList;
//CustomFilter filter;

public MedicalRecordsAdapter(Context context, List<ProfileImagesModel> 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<ProfileImagesModel> filteredList = new ArrayList<>();

                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<ProfileImagesModel>) 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);
    }
}}

1 个答案:

答案 0 :(得分:0)

从您的Adpater中移除getFilter()方法并添加以下方法:

 public void filter(String charText) {
    charText = charText.toLowerCase(Locale.getDefault());
    membersList.clear();

    if (charText.length() == 0) {

        membersList.addAll(filterList);

    } else {
        for (int i=0; i<filterList.size(); i++) {
            ProfileImagesModel androidVersion = filterList.get(i);
            if (androidVersion.getType().toLowerCase(Locale.getDefault()).contains(charText)) {
                membersList.add(androidVersion);
            }
        }
    }
    notifyDataSetChanged();
}

并在setOnItemSelectedListener()

中调用以下方法
dataAdapter.filter(selectedSpinnerItem);