我已经创建了一个带有图标列表的对话框,即可绘制的drawable。 我创建了一个网格视图来显示这些图标。
现在,我想获取所选的图标并将其设置为活动中的图像视图。
那么,如何将其转换为drawable,以便我可以在活动的图像视图上设置所选的图标?
对话框代码:
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]
。 - Blackbeltparent.getItem(position)
,然后将其设置为可绘制对象。 - Raghunandan