Android 如何从数组更改为一个数组列表
我有这个数组,我把它作为一个参数发送给另一个活动, 如何使用一个数组列表添加此项??? 我试图使用数组列表类型模块,但我不能添加所有我不想添加的项目Android 如何从数组更改为一个数组列表,android,arrays,Android,Arrays,我有这个数组,我把它作为一个参数发送给另一个活动, 如何使用一个数组列表添加此项??? 我试图使用数组列表类型模块,但我不能添加所有我不想添加的项目 title = new String[]{"ahmad", "ali", "omar"}; desc = new String[]{"China", "India", "United States"}; background = new int[]{R.drawable.apple_ex,R.mipmap.ic_launch
title = new String[]{"ahmad", "ali", "omar"};
desc = new String[]{"China", "India", "United States"};
background = new int[]{R.drawable.apple_ex,R.mipmap.ic_launcher,R.drawable.apple_ex};
profile = new int[]{R.drawable.apple_ex,R.mipmap.ic_launcher,R.drawable.apple_ex};
创建一个模型类,如下所示:-
public class Model {
private String title;
private String desc;
private int background;
private int profile;
public Model(String title, String desc, int background, int profile) {
this.title = title;
this.desc = desc;
this.background = background;
this.profile = profile;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getDesc() {
return desc;
}
public void setDesc(String desc) {
this.desc = desc;
}
public int getBackground() {
return background;
}
public void setBackground(int background) {
this.background = background;
}
public int getProfile() {
return profile;
}
public void setProfile(int profile) {
this.profile = profile;
}
}
ArrayList<Model> modelList = new ArrayList<>();
Model model1 = new Model("ahmad", "China", R.drawable.apple_ex, R.drawable.apple_ex);
Model model2 = new Model("ali", "India", R.mipmap.ic_launcher, R.drawable.apple_ex);
Model model3 = new Model("omar", "United States", R.mipmap.ic_launcher, R.drawable.apple_ex);
modelList.add(model1);
modelList.add(model2);
modelList.add(model3);
然后,当您必须在Arraylist中添加项时,只需创建一个包含适当项的模型,并按如下方式添加:-
public class Model {
private String title;
private String desc;
private int background;
private int profile;
public Model(String title, String desc, int background, int profile) {
this.title = title;
this.desc = desc;
this.background = background;
this.profile = profile;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getDesc() {
return desc;
}
public void setDesc(String desc) {
this.desc = desc;
}
public int getBackground() {
return background;
}
public void setBackground(int background) {
this.background = background;
}
public int getProfile() {
return profile;
}
public void setProfile(int profile) {
this.profile = profile;
}
}
ArrayList<Model> modelList = new ArrayList<>();
Model model1 = new Model("ahmad", "China", R.drawable.apple_ex, R.drawable.apple_ex);
Model model2 = new Model("ali", "India", R.mipmap.ic_launcher, R.drawable.apple_ex);
Model model3 = new Model("omar", "United States", R.mipmap.ic_launcher, R.drawable.apple_ex);
modelList.add(model1);
modelList.add(model2);
modelList.add(model3);
你可以上课
public class Container{
public String[] title;
public String[] desc;
public int[] background ;
public int[] profile;
}
设置属性并通过序列化为字符串将其传递给其他活动。是否希望标题、描述、背景、配置文件合并到一个列表中?是!将数据存储在列表中并在其他活动中使用为什么不使用Hashmap并放置这些数据。如果在Arraylist中循环,则不会得到类强制转换异常。通过使用Title、Desc作为键并将这些数组放入值中,可以将它们放置为HashMap。