无法在AlertDialog中创建RecyclerView,Alert Dialog中的内容为空

时间:2018-10-03 08:48:41

标签: java android

我正在尝试在警报对话框内容中显示两个RecyclerViews。请帮助我做错了什么事

未调用onCreateViewHolder函数

请注意,RecyclerViews位于xml的ConstraintLayout中。如果那样导致他们无法显示,应该怎么做?

public class MainActivity extends AppCompatActivity {
private View inflatedView;
private RecyclerView fontList,sizeList;
private String[] fonts = {"normal","sans","serif","monospace"};
private String[] sizes = {"14","16","18","20","22","24"};

@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    AlertDialog.Builder builder = new AlertDialog.Builder(this);

    inflatedView = getLayoutInflater().inflate(R.layout.design_style,null);

    builder.setView(inflatedView).setPositiveButton("OK", new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {
            Toast.makeText(MainActivity.this,"OK Pressed",Toast.LENGTH_SHORT).show();
        }
    }).setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {
            Toast.makeText(MainActivity.this,"Cancel",Toast.LENGTH_SHORT).show();
        }
    });

    AlertDialog dialog = builder.create();
    dialog.show();

    fontList = inflatedView.findViewById(R.id.fontlist);
    sizeList = inflatedView.findViewById(R.id.sizelist);

    fontList.setLayoutManager(new LinearLayoutManager(this));
    fontList.setAdapter(new MyAdapter(Arrays.asList(fonts)));

    sizeList.setLayoutManager(new LinearLayoutManager(this));
    sizeList.setAdapter(new MyAdapter(Arrays.asList(sizes)));

}

private class MyAdapter extends RecyclerView.Adapter<MyAdapter.MyViewHolder>{

    private List<String> itemList;

    public MyAdapter(List<String> itemList) {
        this.itemList = itemList;
        Log.d("lol","MyAdapter");
    }

    @NonNull
    @Override
    public MyAdapter.MyViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.design_fontcell,viewGroup);
        Log.d("lol","onCreateViewHolder");
        return new MyViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull MyAdapter.MyViewHolder myViewHolder, int i) {
        myViewHolder.mtextView.setText(itemList.get(i));
        Log.d("lol",itemList.get(i));
    }

    @Override
    public int getItemCount() {
        Log.d("lol","getItemCount: "+itemList.size());
        return itemList.size();
    }

    class MyViewHolder extends RecyclerView.ViewHolder{
        TextView mtextView;
        MyViewHolder(View cellView){
            super(cellView);
            mtextView = cellView.findViewById(R.id.textView_font);
        }
    }
}
}

1 个答案:

答案 0 :(得分:0)

像这样更改代码

x