简体   繁体   中英

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:

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.

You need to inflate the view when you set it in the builder.setView(gridView); Do it like this:

LayoutInflater layoutInflater = getLayoutInflater();

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

this will work.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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