2016-10-07 4 views
1

私はこのアレイを持っており、別のアクティビティへのパラメータとして送信します どのようにこのアイテムを追加するために1つのアレイリストを使用できますか? 私は配列リストタイプのモジュールを使用しようとしましたが、私はすべての項目を追加できません!次のようにArrayListの中にアイテムを追加する必要がある場合アレイから1つのアレイリストへの変更方法

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

それからちょうど適切な項目でモデルを作成し、それを追加します。 - - は:

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

title、desc、background、profileを1つのリストにマージしますか? –

+0

はい!リストに格納されたデータを別のアクティビティで使用する –

+0

なぜHashmapを使用せず、これらを配置してください。 Arraylistでループする場合、classCast例外は発生しません。 Title、Desc as Keysを使ってHashMapとして配置し、これらの配列を値に配置することができます。 –

答えて

0

次のようにモデルクラスの作成

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に必要なすべての値が設定されます。そして、あなたはあなたがクラス

public class Container{ 

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

} 

を設定に特性を有することができる

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

大歓迎!ハッピーに助けてくれる:) @moayedalayaseh –

1

forloop使用して値を取得するためにそれを使用して文字列としてシリアル化することにより、他の活動に沿って、それを渡すことができます。

関連する問題