【问题标题】:I want my recyclerview to show limited cards through condition but all items keep getting shown我希望我的 recyclerview 通过条件显示有限的卡片,但所有项目都继续显示
【发布时间】:2022-01-11 00:25:40
【问题描述】:

我正在尝试显示 Recyclerview 未完成的作业列表 (!isJob_finished()),但每个项目似乎都显示在 recyclerview 卡片列表中。我如何使它在作业未完成时不会显示在 recyclerview 列表中?

我的适配器 适配器

public class MyAdapter extends RecyclerView.Adapter<MyAdapter.MyViewHolder>{
    //Make a context and iterable datalist variable
    Context context;
    ArrayList<JobModel> userArrayList;

    //Constructor
    public MyAdapter(Context context, ArrayList<JobModel> userArrayList) {
        this.context = context;
        this.userArrayList = userArrayList;
    }

    @NonNull
    @Override
    public MyAdapter.MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        //Here is where you attach your item layout ie: job_openings_list
        View v = LayoutInflater.from(context).inflate(R.layout.job_openings_list,parent,false);
        return new MyViewHolder(v);
    }

    @Override
    public void onBindViewHolder(@NonNull MyAdapter.MyViewHolder holder, int position) {
        //Instantiate your model class to hold the data of the model ur simulating
        JobModel job = userArrayList.get(position);

        //Based on the widgets u used on myViewHolder
        if(job.isJob_finished()==false){
            holder.tv_provided_title.setText(job.getJob_title());
            holder.tv_provided_payout.setText(job.getJob_payout());
            holder.tv_provided_id.setText(job.getJob_ID());

            //OnClickers redirect to intent
            holder.tv_provided_title.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) { ToDescription(v, job); }
            });
            holder.tv_provided_payout.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) { ToDescription(v, job); }
            });
            holder.tv_provided_id.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) { ToDescription(v, job); }
            });
        }else{

        }

    }

    private void ToDescription(View v, JobModel job) {
        Intent i = new Intent(v.getContext(), JobDescription.class);
        i.putExtra("job_id",job.getJob_ID());
        context.startActivity(i);
    }

    @Override
    public int getItemCount() {
        return userArrayList.size();
    }

    public static class MyViewHolder extends RecyclerView.ViewHolder{
        //Here in scope instantiate the widgets used based on your item layout
        TextView tv_provided_title,tv_provided_payout, tv_provided_id;

        public MyViewHolder(@NonNull @NotNull View itemView) {
            super(itemView);
            //find the widgets
            tv_provided_title = itemView.findViewById(R.id.tv_provided_title);
            tv_provided_payout = itemView.findViewById(R.id.tv_provided_payout);
            tv_provided_id = itemView.findViewById(R.id.tv_provided_id);
        }
    }
}

我的模特 我与适配器一起使用的模型

public class JobModel {
    private String Job_title;
    private String Job_ID;
    private String Job_payout;
    private Boolean Job_finished = false;

    private JobModel(){
    }

    public JobModel(String job_title, String job_ID, String job_payout) {
        Job_title = job_title;
        Job_ID = job_ID;
        Job_payout = job_payout;
    }

    public Boolean isJob_finished() { return Job_finished; }

    public void setFinished(Boolean finished) { this.Job_finished = finished; }

    public String getJob_title() {
        return Job_title;
    }

    public void setJob_title(String job_title) {
        Job_title = job_title;
    }

    public String getJob_ID() {
        return Job_ID;
    }

    public void setJob_ID(String job_ID) {
        Job_ID = job_ID;
    }

    public String getJob_payout() {
        return Job_payout;
    }

    public void setJob_payout(String job_payout) {
        Job_payout = job_payout;
    }
}

我使用的代码是JAVA

【问题讨论】:

    标签: java android android-studio android-recyclerview android-adapter


    【解决方案1】:

    RecyclerView 是一个View,旨在显示传递的数据(在这种情况下:使用适配器)。您应该将已过滤的列表传递给适配器,而不是所有作业的整个数组。这是最简单的方法

    另一种更灵活的方法是implement Filterable in adapter 并使用过滤,如果在开始时应该只有未完成的作业,那么只需在适配器构造函数的开头设置过滤器。您还可以引入一些开关/切换 GUI 元素,这些元素将隐藏/显示已完成的工作(例如在 ActionBar/Toolbar 上)

    【讨论】:

    • 我觉得Filterable是个不错的选择,但是使用switch的时候会隐藏视图?
    • 你可以实现任何你想要的过滤。您可以只传递boolean,也可以传递String 进行过滤。那是您的选择(作为您在适配器中的实现)。 switch case 会做一些有限的选择,看看链接教程是如何实现的
    • 我没有使用 filterable 但是当你提到 recyclerview 只是一个视图时,你给了我一个类似于 filterable 的想法。我编辑了文档快照以在活动类中包含一个条件并且它有效。未显示已完成的作业。
    • 如果有帮助,请考虑支持/接受答案 :) 祝你好运!
    猜你喜欢
    • 2018-04-04
    • 2021-05-30
    • 1970-01-01
    • 2022-09-26
    • 1970-01-01
    • 2020-02-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多