警报对话框中的GridView未填充

时间:2017-05-13 13:25:01

标签: java android gridview

我一直在尝试使用网格视图在我的代码中弹出一个对话框,但对话框只是显示为空白视图。我可以看到视图正在创建但未填充。这是我的代码:

  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();
}

这是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;
}
}

我的可绘制资源是我的应用中其他位置使用的圆圈的xml文件,没有问题。

1 个答案:

答案 0 :(得分:0)

builder.setView(gridView);中设置视图时,需要对视图进行充气 这样做:

LayoutInflater layoutInflater = getLayoutInflater();

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

这将有效。