使用4个不同的微调器

时间:2018-02-13 11:46:05

标签: android android-recyclerview android-spinner

我目前正在尝试为RecyclerView实施一个搜索功能,其中包含CardViews形式的自定义项目,其中包含4个不同的TextViews。 我想使用4个不同的微调元素,每个元素覆盖一个textView

现在我只用一个微调器尝试搜索,但它不起作用。

自定义RecyclerView适配器:

public class WorkersAdapter extends RecyclerView.Adapter<WorkersAdapter.ViewHolder> {

private List<Worker> workers, filtered;
private Context c;
private int lastPosition = -1;

public class ViewHolder extends RecyclerView.ViewHolder {
    public TextView levelracein, nodein, cityin;
    public ImageView iv;

    public ViewHolder(View view) {
        super(view);
        levelracein = (TextView) view.findViewById(R.id.levelracein);
        cityin = (TextView) view.findViewById(R.id.cityin);
        nodein = (TextView) view.findViewById(R.id.nodein);
    }
}


public WorkersAdapter(Context c, List<Worker> workers) {
    this.c = c;
    this.workers = workers;
    this.filtered = new ArrayList<>();
    this.filtered.addAll(this.workers);
}

@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    View itemView = LayoutInflater.from(parent.getContext())
            .inflate(R.layout.worker_list_item, parent, false);

    return new ViewHolder(itemView);
}

@Override
public void onBindViewHolder(ViewHolder holder, int position) {
    int pos = position;
        Worker worker = filtered.get(position);
        holder.levelracein.setText(worker.getLevel() + " " + worker.getRace());
        holder.cityin.setText(worker.getCity());
        holder.nodein.setText(worker.getNode());

    setAnimation(holder.itemView, position);
}

@Override
public int getItemCount() {
    return workers != null? workers.size() : 0;
}

private void setAnimation(View viewToAnimate, int position)
{
    if (position > lastPosition)
    {
        Animation animation = AnimationUtils.loadAnimation(c, android.R.anim.slide_in_left);
        viewToAnimate.startAnimation(animation);
        lastPosition = position;
    }
}

public void filter(final String level, final String race, final String city, final String node) {

    new Thread(new Runnable() {
        @Override
        public void run() {

            filtered.clear();
            if (level.equals("None")) {
                filtered.addAll(workers);

            } else {
                for (Worker worker : workers) {
                    if (worker.getLevel().toLowerCase().contains(level.toLowerCase())) {
                        filtered.add(worker);
                    }
                }
            }

                ((Activity) c).runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    // Notify the List that the DataSet has changed...
                    notifyDataSetChanged();
                }
            });

        }
    }).start();

}


}

Spinner听众:

levelspinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
        @Override
        public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
            onQueryTextChange(levelspinner.getSelectedItem().toString(),
                    racespinner.getSelectedItem().toString(),
                    cityspinner.getSelectedItem().toString(),
                    nodespinner.getSelectedItem().toString());
        }

        @Override
        public void onNothingSelected(AdapterView<?> adapterView) {

        }
    });

在RecyclerView中提供搜索的功能:

public boolean onQueryTextChange(String level, String race, String city, String node) {
    wa.filter(level, race, city, node); //wa -> WorkersAdapter
    return true;
}

问题是,

  1. 如何实现一个搜索,它将添加所有不同的微调器选择,并且对所选的每个微调器项目进行更精确的搜索?
  2. 为什么我的RecyclerView在选择后仍显示空的cardView?
  3. enter image description here

1 个答案:

答案 0 :(得分:1)

在Adapter类上将getItemCount()更改为以下代码,在搜索时使用的两个ArralList引用将显示filtered ArrayList引用的结果。所以你必须返回filtered.size()

   @Override
public int getItemCount() {
    return filtered!= null? filtered.size() : 0;
}

根据您的评论,您希望使用以下代码

在单个过滤器更改中搜索所有这些微调器
for (Worker worker : workers) {
                if (worker.getLevel().toLowerCase().contains(level.toLowerCase())  ||  2nd searchitem      ||   3rd searchitem     ||  4th searchitem  ) {
                    filtered.add(worker);
                }
            }