我试图从另一个构造函数获取数组列表。我尝试了很多方法,但没有成功。请在下面查看我的代码。如果无法做到这一点,请向我建议采用其他方法来实现此方法。该代码在我的适配器中

ArrayList<CuisineCategory> mydataList = new ArrayList<CuisineCategory>();

ArrayList<CuisineCategory> dataList;

public CuisineAdapter(Context context, ArrayList<CuisineCategory> mydataList) {
        this.context = context;
        this.mydataList = mydataList;

    }

    public CuisineAdapter(Context context, CuisineListToggleListener listToggleListener) {
        this.context = context;
        this.listToggleListener = listToggleListener;
        this.dataList = new ArrayList<>();

       this.dataList = mydataList;<- this is not working

    }


mydataList包含数组列表,我想在第二个构造函数中使用它

请参阅我的适配器的完整代码

public class CuisineAdapter extends RecyclerView.Adapter<CuisineAdapter.MyViewHolder> {
    private Context context;
    ArrayList<CuisineCategory> mydataList = new ArrayList<>();

    private ArrayList<CuisineCategory> dataList;

    private CuisineListToggleListener listToggleListener;

    public CuisineAdapter(Context context, ArrayList<CuisineCategory> mydataList) {
        this.context = context;
        //this.dataList = new ArrayList<>();
        this.mydataList = mydataList;

//        for(CuisineCategory dt: mydataList){
//
//            //Log.e("data:", String.valueOf(dt.getTitle()));
//        }
    }

    public CuisineAdapter(Context context, CuisineListToggleListener listToggleListener) {
        this.context = context;
        this.listToggleListener = listToggleListener;

        this.dataList = new ArrayList<>();

        this.dataList.add(new CuisineCategory("Popular"));
        this.dataList.add(new CuisineCategory("Main course"));
        this.dataList.add(new CuisineCategory("Lunch"));
        this.dataList.add(new CuisineCategory("Breakfast"));
        this.dataList.add(new CuisineCategory("Dinner"));
        this.dataList.add(new CuisineCategory("Appetizer"));

    }




    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return new MyViewHolder(LayoutInflater.from(context).inflate(R.layout.item_cuisine_cat, parent, false));
    }

    @Override
    public void onBindViewHolder(MyViewHolder holder, int position) {
        holder.setData(dataList.get(position));
    }

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

    public class MyViewHolder extends RecyclerView.ViewHolder {
        private LinearLayout rootLayout;
        private TextView cuisineCategory;
        private ImageView cuisineItemToggle;
        private RecyclerView cuisineItemList;

        public MyViewHolder(View itemView) {
            super(itemView);
            cuisineItemList = itemView.findViewById(R.id.cuisineItemList);
            cuisineCategory = itemView.findViewById(R.id.cuisineCategory);
            cuisineItemToggle = itemView.findViewById(R.id.cuisineItemToggle);
            rootLayout = itemView.findViewById(R.id.rootLayout);

            itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    int pos = getAdapterPosition();
                    CuisineCategory category = dataList.get(pos);
                    category.setSelected(!category.isSelected());
                    Collections.swap(dataList, 0, pos);
                    notifyItemMoved(category.isSelected() ? pos : 0, category.isSelected() ? 0 : pos);
                    listToggleListener.OnListExpanded(category.isSelected());
                    notifyItemChanged(0);
                }
            });
        }

        public void setData(CuisineCategory category) {
            cuisineCategory.setText(category.getTitle());
            cuisineItemToggle.setImageDrawable(ContextCompat.getDrawable(context, category.isSelected() ? R.drawable.ic_keyboard_arrow_up_accent_24dp : R.drawable.ic_keyboard_arrow_down_accent_24dp));

            cuisineItemList.setLayoutManager(new LinearLayoutManager(context));
            cuisineItemList.setAdapter(new RestaurantMenuAdapter(context));
            cuisineItemList.setVisibility(category.isSelected() ? View.VISIBLE : View.GONE);

            ViewGroup.LayoutParams layoutParams = rootLayout.getLayoutParams();
            layoutParams.height = category.isSelected() ? ViewGroup.LayoutParams.MATCH_PARENT : ViewGroup.LayoutParams.WRAP_CONTENT;
            rootLayout.setLayoutParams(layoutParams);
        }
    }

    public interface CuisineListToggleListener {
        void OnListExpanded(boolean selected);
    }
}


在我的第二个适配器dataList数组中,我想动态地加载它,因此通过改型,我使用以下代码将一些数据提取到ArrayList中

cuisineAdapter = new CuisineAdapter(RestaurantDetailActivity.this,dataList);


并像这样设置适配器

private void setupCuisineRecycler() {
        linearLayoutManager = new MyLinearLayoutManager(getContext());
        cuisineRecycler.setLayoutManager(linearLayoutManager);

        cuisineRecycler.setAdapter(new CuisineAdapter(getContext(), new CuisineAdapter.CuisineListToggleListener() {
            @Override
            public void OnListExpanded(final boolean selected) {
                if (selected) {
                    cuisineRecycler.scrollToPosition(0);
                }
                new Handler().postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        linearLayoutManager.setScrollEnabled(!selected);
                    }
                }, 500);
            }
        }));
    }


现在我想将数据绑定到数据列表。

最佳答案

你做不到

this.dataList = mydataList;<- this is not working


在您的第二个构造函数中,mydataList不存在。

您可以执行以下操作:

  public class CuisineAdapter{

    private ArrayList<CuisineCategory> mydataList;

    public CuisineAdapter(Context context, ArrayList<CuisineCategory> mydataList) {
            this.context = context;
            this.mydataList = mydataList;
    }

    public CuisineAdapter(Context context,
                          ArrayList<CuisineCategory> mydataList,
                          CuisineListToggleListener listToggleListener) {
            this(context, mydataList);
            this.listToggleListener = listToggleListener;
    }

    public CuisineAdapter(Context context,
                          CuisineListToggleListener listToggleListener) {
            this.context = context;
            this.listToggleListener = listToggleListener;
    }

    public ArrayList<CuisineCategory> getMydataList(){
       return mydataList;
    }

    public void setMydataList(ArrayList<CuisineCategory> mydataList){
       this.mydataList= mydataList;
    }

  }


然后将其用于:

CuisineAdapter adapter = new CuisineAdapter(context, mydataList);


要么:

CuisineAdapter adapter = new CuisineAdapter(context, listToggleListener);
adapter.setMydataList(mydataList);

08-17 15:02