【问题标题】:How to convert int to drawable?如何将int转换为drawable?
【发布时间】:2016-05-25 14:04:01
【问题描述】:

我创建了一个带有图标列表的对话框,即可绘制对象。我创建了一个网格视图来显示这些图标。

现在我想获取选定的图标并将其设置为我的活动中的图像视图。

那么如何将它转换为可绘制的,以便我可以在我的活动的图像视图上设置选定的图标?

对话框代码:

 private void showAlertDialog() {

        GridView gridView = new GridView(this);

        gridView.setGravity(Gravity.CENTER);
        gridView.setPadding(0,20,0,20);


        int[] mThumbIds = {
                R.drawable.roundicons05,R.drawable.roundicons08,R.drawable.roundicons02,R.drawable.roundicons03,R.drawable.roundicons04,
                R.drawable.roundicons16,R.drawable.roundicons37,R.drawable.roundicons06,R.drawable.roundicons07,R.drawable.roundicons05,
                R.drawable.roundicons09,R.drawable.roundicons10,R.drawable.roundicons11,R.drawable.roundicons12,R.drawable.roundicons13,
                R.drawable.roundicons14,R.drawable.roundicons15,R.drawable.roundicons16,R.drawable.roundicons17,R.drawable.roundicons18,
                R.drawable.roundicons19,R.drawable.roundicons20,R.drawable.roundicons22,
        };

        gridView.setAdapter(new ImageAdapter(CheckListActivity.this,mThumbIds));
        gridView.setNumColumns(4);
        gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                // do something here

             //   icon.setImageDrawable(id);
                imageId = position;
                Drawable drawable = getResources().getDrawable(imageId);
                icon.setImageDrawable(drawable);

            }
        });

        final MaterialDialog dialog = new MaterialDialog.Builder(CheckListActivity.this)
                .customView(gridView, false)
                .title("Select Icon")
                .negativeText("CANCEL")
                .canceledOnTouchOutside(true)
                .build();

        dialog.show();


    }

我试过这样

  imageId = position;
  Drawable drawable = getResources().getDrawable(imageId);
  icon.setImageDrawable(drawable);

但这不会引发资源发现异常。

谢谢你..

【问题讨论】:

  • imageId = position 应该是imageId = mThumbIds [position]
  • 您可以使用parent.getItem(posiiton),然后将其设置为可绘制对象

标签: android int drawable android-gridview


【解决方案1】:

而不是使用position 作为ResourceID(它不是),而是在positions 处获取mThumbIds 数组的一项。所以你的代码应该是这样的:

public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
    // do something here
    imageId = mThumbIds[position];
    Drawable drawable = getResources().getDrawable(imageId);
    icon.setImageDrawable(drawable);

 }

我希望这会有所帮助。

【讨论】:

  • 知道了.. 谢谢.. :-) @ishmaelMakitla
【解决方案2】:

方法 getDrawable (int) 已弃用。 现在您可以轻松使用

    icon.setImageResource(imageId);

【讨论】:

    【解决方案3】:

    在科特林中:

    ContextCompat.getDrawable(context, resourceId)
    

    【讨论】:

      【解决方案4】:

      这一行

      imageId = position;
      

      应该是这样的

      imageId = mThumbIds[position];
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-04-01
        • 1970-01-01
        • 1970-01-01
        • 2011-03-03
        • 2014-05-22
        • 1970-01-01
        相关资源
        最近更新 更多