Recyclerview未在AlertDialog中设置

时间:2018-07-05 15:06:44

标签: android android-recyclerview android-alertdialog recycler-adapter

我在自定义警报对话框视图中设置了回收站,但无法在警报对话框内部设置回收器视图。

Button button = (Button) findViewById(R.id.button);
    button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            AlertDialog.Builder alertDialog = new AlertDialog.Builder(context);
            View convertView = LayoutInflater.from(context).inflate(R.layout.recycle_data, null);
            alertDialog.setView(convertView);
            alertDialog.setTitle(context.getResources().getString(R.string.app_name));
            RecyclerView rv = (RecyclerView) convertView.findViewById(R.id.recyclerView);
            rv.setLayoutManager(new LinearLayoutManager(context));
            rv.setAdapter(new CustomSpinner_Adapter(context, mModels));
            AlertDialog dialog= alertDialog.create();
            dialog.show();
        }
    });

我正在传递从android中的string_array转换而来的ArrayList

String[] foodAllergy = getResources().getStringArray(R.array.food_allergy);
    for (int i = 0; i < foodAllergy.length; i++) {
        mCustomSpinnerModel = new CustomSpinnerModel();
        mCustomSpinnerModel.setTitle(foodAllergy[i]);
        mModels.add(mCustomSpinnerModel);
    }

这是用于设置视图的CustomAdapter,事情是我在设置自定义适配器视图之前设置了警报对话框的视图

    public class CustomSpinner_Adapter extends RecyclerView.Adapter<CustomSpinner_Adapter.ViewHolder> implements
        CompoundButton.OnCheckedChangeListener {

    private Context mContext;
    private List<CustomSpinnerModel> mCustomModel;

    public CustomSpinner_Adapter(Context context, ArrayList<CustomSpinnerModel> models) {
        mContext = context;
        mCustomModel = models;
    }

    @NonNull
    @Override
    public CustomSpinner_Adapter.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(mContext).inflate(R.layout.item_selection_check, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull CustomSpinner_Adapter.ViewHolder holder, int position) {
        holder.textCheckTitle.setText(mCustomModel.get(position).getTitle());
    }

    @Override
    public int getItemCount() {

        return mCustomModel.size();

    }

    @Override
    public void onCheckedChanged(CompoundButton compoundButton, boolean b) {

    }

    public class ViewHolder extends RecyclerView.ViewHolder {

        private TextView textCheckTitle;
        private CheckBox checkAllergy;

        public ViewHolder(View itemView) {
            super(itemView);

            textCheckTitle = (TextView) itemView.findViewById(R.id.check_text_label);
            checkAllergy = (CheckBox) itemView.findViewById(R.id.checkbox_selection);
        }
    }
}

我不明白为什么自定义适配器视图之前会显示警报对话框

1 个答案:

答案 0 :(得分:0)

错误不是在代码中,而是在xml中,因为如果我们使用包含recyclerview的约束布局对Alertdialog进行充气,则需要将recyclerview的高度设置为 wrap_content