如何从数组更改为一个数组列表

如何从数组更改为一个数组列表

本文介绍了如何从数组更改为一个数组列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个数组,我将其作为参数发送给另一个活动,
如何使用一个数组列表添加此项?
我试图使用数组列表类型模块,但我不能添加所有项目,我不会!

i have this arrays and i send it as a parameter to another activity ,how i can use one array list to add this item ???i tried to use array list type module but i cant add all item i wont !

    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中添加项目时,只需创建具有适当项目的模型,并按如下所示添加它:-

Then when you have to add items in Arraylist just create a model with appropriate items and add it as follows:-

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

现在,您的modelList将具有所需的所有值。然后,您可以使用它通过forloop

Now your modelList will have all the values that you want. Then you can use it to get values using forloop

for (Model model : modelList) {
  model.getTitle();
  model.getDesc();
  model.getBackground();
  model.getProfile();
}

这篇关于如何从数组更改为一个数组列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-13 17:38