1

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};
3
  • So you want title,desc,background,profile to merge in one single list? Commented Oct 7, 2016 at 8:05
  • yes ! stored data in list and use it in another activity Commented Oct 7, 2016 at 8:07
  • Why not use Hashmap and place these. You will not get classCast exceptions if looping in a Arraylist. You can place them as HashMap by using Title, Desc as Keys and placing these Arrays in Values. Commented Oct 7, 2016 at 8:21

2 Answers 2

1

You can have a class

public class Container{

  public String[]  title;
  public String[] desc;
  public int[] background ;
  public  int[] profile;

}

Set the properties and pass it along to other activity by serializing as string.

Sign up to request clarification or add additional context in comments.

Comments

0

Create a Model Class as follows:-

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

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

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.