2016-08-25 9 views
0

カスタムメイドの丸められた画像のリストです。ループを介して値を動的に割り当てる必要があります。代わりに ​​を作成するループを介して値を動的に割り当てる - Android - プログラミングによるJavaプログラミング

  RoundedImageView img23 = (RoundedImageView) findViewById(R.id.avatar23); 
      img23.setImageResource(R.mipmap.avatars_male_28); 

      RoundedImageView img24 = (RoundedImageView) findViewById(R.id.avatar24); 
      img24.setImageResource(R.mipmap.avatars_male_29); 

      RoundedImageView img25 = (RoundedImageView) findViewById(R.id.avatar25); 
      img25.setImageResource(R.mipmap.avatars_male_30); 

      for (i = 1;i>25; i++){ 
       j=(String) i; 

       //need a loop that dynamically sets img**2** (any number) 
       img<j>.setImageResource(R.mipmap.avatars_male_30); 
      } 
+4

イメージをリストまたは配列に追加しますか? – mayha

+0

List リストを作成し、リストを繰り返します。簡単に、速く:) –

答えて

0

ありますが、ImageViewのタイプのモデルを作ることによって行い、その後、モデルからImageViewのを抽出し、このようにImageViewのにあなたのドローアブルを使用することができます...

//You can make ImageViewModel type class there you save your image view 
    class ImageViewModel { 

     public ImageView getImage() { 
      return image; 
     } 

     public void setImage(ImageView image) { 
      this.image = image; 
     } 

     ImageView image; 

    } 
    ArrayList<ImageViewModel> images = new ArrayList<>(); 
    ArrayList<Integer> drawables = new ArrayList<>(); 
    drawables.add(R.mipmap.ic_launcher); 
    drawables.add(R.mipmap.ic_launcher); 
    drawables.add(R.mipmap.ic_launcher); 



    //set you imageview in model class and add into the arraylist of modelclass type 
    ImageViewModel model = new ImageViewModel(); 
    model.setImage(imageviews); 
    images.add(model); 




    // Then you can set this as 
    for(int i = 0; i<drawables.size(); i++){ 

     model.getImage().setImageResource(drawables.get(i)); 
    } 

したがって、あなたが動的にImageViewのに

をあなたのイメージを設定することができますこれは、すべてこの定型コード使用butterknifeライブラリを削除するにも

+0

これもpngのmipmapフォルダにありますか? – Prateekro

2

...

だけRoundImageViewのArrayListを作成します。

ArrayList<RoundedImageView> list = new ArrayList<RoundedImageView>(); 
list.add((RoundedImageView) findViewById(R.id.avatar1)); 
list.add((RoundedImageView) findViewById(R.id.avatar2)); 
list.add((RoundedImageView) findViewById(R.id.avatar3)); 

を。 。 。

関連する問題