簡體   English   中英

警報對話框中的GridView未填充

[英]GridView inside Alert Dialog is not being populated

我一直在嘗試使用網格視圖在我的代碼中彈出一個對話框,但是該對話框只是顯示為空白視圖。 我可以看到該視圖已創建但未填充。 這是我的代碼:

  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文件,這些圈子文件可以在我的應用程序的其他位置使用而不會出現問題。

builder.setView(gridView);設置視圖時,需要將其充氣builder.setView(gridView); 像這樣做:

LayoutInflater layoutInflater = getLayoutInflater();

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

這將起作用。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM