简体   繁体   English

更改 RecyclerView 中显示的 ArrayList Android Java

[英]Changing ArrayList shown in RecyclerView Android Java

I have multiple array lists inside an arraylist:我在 arraylist 中有多个数组列表:

[[Object1, Object2],[Object3, Object4],[Object5, Object6]]

I display the first array list in a recyclerview that displays one arraylist at a time:我在 recyclerview 中显示第一个数组列表,一次显示一个 arraylist:

myViewHolder.bindTo(cities.get(0).get(i));

I want to click a button in another class that would show change the array list shown.我想单击另一个 class 中的按钮,该按钮将显示更改显示的数组列表。 How would I achieve this?我将如何实现这一目标?

Recycler View Adapter Class:回收商查看适配器 Class:

    private List<List<Country>> cities;

    @NonNull
    @Override
    public MyViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {

        PlannerItemBinding plannerItemBinding =
                DataBindingUtil.inflate(LayoutInflater.from(viewGroup.getContext()), R.layout.planner_item, viewGroup, false);

        return new MyViewHolder(plannerItemBinding);
    }

    @Override
    public void onBindViewHolder(@NonNull MyViewHolder myViewHolder, int i) {
        myViewHolder.bindTo(cities.get(0).get(i));

    }

    @Override
    public int getItemCount() {
        if (cities != null) {
            return cities.size();
        } else {
            return 0;
        }
    }

    public void setCityList(List<List<Country>> cities) {
        this.cities = cities;
        notifyDataSetChanged();
    }

    class MyViewHolder extends RecyclerView.ViewHolder {

        private PlannerItemBinding plannerItemBinding;

        public MyViewHolder(@NonNull PlannerItemBinding plannerItemBinding) {
            super(plannerItemBinding.getRoot());

            this.plannerItemBinding = plannerItemBinding;
        }

        void bindTo(Country country) {
            plannerItemBinding.setVariable(com.example.planner.BR.city, country);
            plannerItemBinding.setVariable(com.example.planner.BR.adapterPosition, getLayoutPosition());
            plannerItemBinding.setVariable(com.example.planner.BR.countryImageMedium, country.getImages().get(0).getSizes().getMedium());
            plannerItemBinding.executePendingBindings();

        }
    }

}

Set a setter in your RecyclerView.Adapter:在 RecyclerView.Adapter 中设置一个设置器:

public updateCities(List<List<Coutry>> cities) {
    this.cities = cities;
    notifydatasetchanged();
}

so that you can call it in onClick() of that button with the new data.这样您就可以使用新数据在该按钮的 onClick() 中调用它。

This will update the model of your adapter with the passed data and notify the recyclerview that its data has changed.这将使用传递的数据更新适配器的 model,并通知 recyclerview 其数据已更改。

You create an onClickListener on the object of your choice, and then when it is clicked you just setCityList to the new data and use notifydatasetchanged on your adapter您在您选择的 object 上创建一个onClickListener ,然后单击它时,您只需setCityList设置为新数据并在适配器上使用notifydatasetchanged

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM