简体   繁体   English

警报对话框中的GridView未填充

[英]GridView inside Alert Dialog is not being populated

I have been trying to get a dialog to popup in my code with a Grid View, but the dialog is just showing up as a blank view. 我一直在尝试使用网格视图在我的代码中弹出一个对话框,但是该对话框只是显示为空白视图。 I can see the view is being created but not populated. 我可以看到该视图已创建但未填充。 Here is my code: 这是我的代码:

  public void chooseIcon(){

    AlertDialog.Builder builder = new AlertDialog.Builder(this);
    GridView gridView = new GridView(this);
    gridView.setNumColumns(3);
    gridView.setLayoutParams(new GridView.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
    gridView.setBackgroundColor(Color.WHITE);
    gridView.setColumnWidth(GridView.AUTO_FIT);
    gridView.setVerticalSpacing(5);
    gridView.setHorizontalSpacing(5);
    gridView.setStretchMode(GridView.STRETCH_COLUMN_WIDTH);
    IconImageAdapter adapter = new IconImageAdapter(this);
    gridView.setAdapter(adapter);
    builder.setTitle("Pick an icon");
    builder.setView(gridView);
    builder.show();
}

And here is the IconImageAdapter class: 这是IconImageAdapter类:

public class IconImageAdapter extends BaseAdapter{
.....
public View getView(int position, View convertView, ViewGroup parent) {
    ImageView imageView;
    if (convertView == null) {
        imageView = new ImageView(mContext);
        imageView.setLayoutParams(new GridView.LayoutParams(width/3, height/3));
        imageView.setScaleType(ImageView.ScaleType.FIT_XY);
        imageView.setPadding(0, 0, 0, 0);
    } else {
        imageView = (ImageView) convertView;
    }

    switch(position){
        case 0:{
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                imageView.setImageDrawable(mContext.getResources().getDrawable(R.drawable.black_circle, mContext.getTheme()));
            } else {
                imageView.setImageDrawable(mContext.getResources().getDrawable(R.drawable.black_circle));
            }
            break;
        }
        case 1:{
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                imageView.setImageDrawable(mContext.getResources().getDrawable(R.drawable.black_circle, mContext.getTheme()));
            } else {
                imageView.setImageDrawable(mContext.getResources().getDrawable(R.drawable.black_circle));
            };
            break;
        }
        case 2:{
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                imageView.setImageDrawable(mContext.getResources().getDrawable(R.drawable.black_circle, mContext.getTheme()));
            } else {
                imageView.setImageDrawable(mContext.getResources().getDrawable(R.drawable.black_circle));
            }
            break;
        }
        case 3:{
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                imageView.setImageDrawable(mContext.getResources().getDrawable(R.drawable.black_circle, mContext.getTheme()));
            } else {
                imageView.setImageDrawable(mContext.getResources().getDrawable(R.drawable.black_circle));
            }
            break;
        }
        case 4:{
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                imageView.setImageDrawable(mContext.getResources().getDrawable(R.drawable.black_circle, mContext.getTheme()));
            } else {
                imageView.setImageDrawable(mContext.getResources().getDrawable(R.drawable.black_circle));
            }
            break;
        }
        case 5:{
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                imageView.setImageDrawable(mContext.getResources().getDrawable(R.drawable.black_circle, mContext.getTheme()));
            } else {
                imageView.setImageDrawable(mContext.getResources().getDrawable(R.drawable.black_circle));
            }
            break;
        }
    }


    return imageView;
}
}

My drawable resources are xml files of circles that are used in other places in my app without issue. 我的可绘制资源是圈子的xml文件,这些圈子文件可以在我的应用程序的其他位置使用而不会出现问题。

You need to inflate the view when you set it in the builder.setView(gridView); builder.setView(gridView);设置视图时,需要将其充气builder.setView(gridView); Do it like this: 像这样做:

LayoutInflater layoutInflater = getLayoutInflater();

builder.setView(layoutInflater.inflate(gridView.getId(), null));

this will work. 这将起作用。

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

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