通过java编程通过loop-Android动态分配值
以下是定制的圆形图像列表。需要通过循环动态分配一个值通过java编程通过loop-Android动态分配值,java,android,loops,variables,dynamic,Java,Android,Loops,Variables,Dynamic,以下是定制的圆形图像列表。需要通过循环动态分配一个值 RoundedImageView img23 = (RoundedImageView) findViewById(R.id.avatar23); img23.setImageResource(R.mipmap.avatars_male_28); RoundedImageView img24 = (RoundedImageView) findViewById(R.id.a
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);
}
RoundedImageView img23=(RoundedImageView)findViewById(R.id.avatar23);
img23.setImageResource(R.mipmap.avatars\u male\u 28);
RoundedImageView img24=(RoundedImageView)findViewById(R.id.avatar24);
img24.setImageResource(R.mipmap.avatars\u male\u 29);
RoundedImageView img25=(RoundedImageView)findViewById(R.id.avatar25);
img25.setImageResource(R.mipmap.avatars\u male\u 30);
对于(i=1;i>25;i++){
j=(字符串)i;
//需要一个动态设置img**2**(任意数字)的循环
img.setImageResource(R.mipmap.avatars\u male\u 30);
}
而不是创建
RoundedImageView img1、RoundedImageView img2、RoundedImageView img3
只需创建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));
ArrayList list=new ArrayList();
添加((RoundedImageView)findViewById(R.id.avatar1));
添加((RoundedImageView)findViewById(R.id.avatar2));
添加((RoundedImageView)findViewById(R.id.avatar3));
。
.
.而不是创建
RoundedImageView img1、RoundedImageView img2、RoundedImageView img3
只需创建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));
ArrayList list=new ArrayList();
添加((RoundedImageView)findViewById(R.id.avatar1));
添加((RoundedImageView)findViewById(R.id.avatar2));
添加((RoundedImageView)findViewById(R.id.avatar3));
。
.
.在这里,您可以创建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));
}
//保存图像视图时,可以在其中创建ImageViewModel类型类
类ImageViewModel{
公共图像视图getImage(){
返回图像;
}
public void setImage(图像视图图像){
这个图像=图像;
}
图像视图图像;
}
ArrayList images=新的ArrayList();
ArrayList drawables=新的ArrayList();
添加(R.mipmap.ic_启动器);
添加(R.mipmap.ic_启动器);
添加(R.mipmap.ic_启动器);
//在模型类中设置imageview并将其添加到模型类类型的arraylist中
ImageViewModel模型=新的ImageViewModel();
model.setImage(图像视图);
图片。添加(模型);
//然后您可以将其设置为
对于(inti=0;i,您可以通过创建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));
}
//保存图像视图时,可以在其中创建ImageViewModel类型类
类ImageViewModel{
公共图像视图getImage(){
返回图像;
}
public void setImage(图像视图图像){
这个图像=图像;
}
图像视图图像;
}
ArrayList images=新的ArrayList();
ArrayList drawables=新的ArrayList();
添加(R.mipmap.ic_启动器);
添加(R.mipmap.ic_启动器);
添加(R.mipmap.ic_启动器);
//在模型类中设置imageview并将其添加到模型类类型的arraylist中
ImageViewModel模型=新的ImageViewModel();
model.setImage(图像视图);
图片。添加(模型);
//然后您可以将其设置为
对于(int i=0;i要删除所有样板代码,请使用butterknife library要删除所有样板代码,请使用butterknife library将图像添加到列表或数组?创建一个列表列表
,然后迭代列表。简单、快速:)将图像添加到列表或数组?创建一个列表list
,然后迭代列表。简单、快速:)