2016-05-25 7 views
2

私はアイコンのリスト、つまりドロワーブルを使ってダイアログを作成しました。これらのアイコンを表示するためにグリッドビューを作成しました。intを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); 

ようにしようとしたが、これは何のリソース見つかった例外をスローしません。

ではなく(それはない)のResourceIDとしてpositionを使用するのではなく、むしろpositionsmThumbIds配列のアイテムを取得。..

+0

を使用することができます 'IMAGEID = position'あなたは' parent.getItem(posiiton)を使用し、 'して、描画可能 – Blackbelt

+0

' IMAGEID = mThumbIds [位置] 'でなければなりません。 。ありがとう.. :-) @ishmaelMakitla – Raghunandan

答えて

7

、ありがとうございました。したがって、あなたのコードは次のようになります:

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

} 

私はこれが役に立ちそうです。

+0

としてそれを得たことを設定することができます –

0

imageId = position; 

が廃止され、この

imageId = mThumbIds[position]; 
0

方法getDrawable(INT)ようにする必要があり、このライン。 今、あなたは簡単に

icon.setImageResource(imageId);