将ArrayList <string>放入ArrayList <custom object =“”>

时间:2017-10-05 10:18:59

标签: java android arrays object

我从共享首选项中获取数据:

SharedPreferences sharedPref = ImageListViewActivity.this.getSharedPreferences("settings",Context.MODE_PRIVATE;
String MyString1 = sharedPref.getString("MyPackage.NameOfSharedPref",null);
String MyString2 = sharedPref.getString("MyPackage.NameOfSharedPref",null);
String MyString3 = sharedPref.getString("MyPackage.NameOfSharedPref",null);

看起来像:

MyString1 = "Ben, David, Tom, Jessica"
MyString2 = "25, 27, 21, 22"
MyString3 = "male, male, male, female"

我把它分成了String Arrays:

String[] splitt1 = MyString1.split(",");
String[] splitt2 = MyString2.split(",");
String[] splitt3 = MyString3.split(",");

现在我将它放入ArrayList:

ArrayList<String> arrayList1 = new ArrayList<String>();
for (int i = 0; i < MyString1.length; i++) {
  arrayList1 .add(MyString1[i]);
}

ArrayList<String> arrayList2 = new ArrayList<String>();
for (int i = 0; i < MyString2.length; i++) {
  arrayList2 .add(MyString2[i]);
}

ArrayList<String> arrayList3 = new ArrayList<String>();
for (int i = 0; i < MyString3.length; i++) {
  arrayList3 .add(MyString3[i]);
}

如何将ArrayList的值放入自定义对象数组中? 我有一个带有构造函数,getter和setter的对象类。

public class Student {
  private String Name;
  private String Age;
  private String Sex;

  public Student(String name, String age, String sex) {
    this.Name = name;
    this.Age = age;
    this.Sex = sex;   
  }

  public String getName() {
    return Name;
  }

  public void setName(String artikelnummer) {
    Name = name;
  }

  public String getAge() {
    return Age;
  }

  public void setAge(String artikelnummer) {
    Age = age;
  }

  public String getSex() {
    return sex;
  }

  public void setSex(String artikelnummer) {
    Sex = sex;
  }

现在我想填充我的学生对象数组,我试过这种方式,但这是我填充的字符串数组,它不起作用:

ArrayList<Students> peopleList = new ArrayList<>();

for (int i = 0; i < splitt1.length; i++) {
   peoplelist.add(splitt1[i]);
}

我想对ArrayLists进行排序,并将它们写入ObjectArrayList,如下所示:

Student Stu1 = new Student("Ben","25","male");
Student Stu2 = new Student("David","27","male");
Student Stu3 = new Student("Tom","21","male");
Student Stu4 = new Student("Jessica","22","female");

请帮帮我,谢谢你的期待!

4 个答案:

答案 0 :(得分:1)

您必须创建一个Student对象,例如

for (int i = 0; i < splitt1.length; i++) {
     String name = splitt1[i].trim(); remove whitespaces
     String age = splitt2[i].trim(); remove whitespaces
     String gender = splitt3[i].trim(); remove whitespaces
     peoplelist.add(new Student(name, age, gender));
}

这将解决您现在的问题。您必须记住,ArrayList的长度必须完全相同,否则您将获得IndexOutOfBondException

答案 1 :(得分:1)

试试此代码

 ArrayList<Student> peopleList = new ArrayList<>();

   for (int i = 0; i < splitt1.length; i++) {
         Student Student = new Student(splitt1[i],splitt2[i],splitt[i]);
        peoplelist.add(Student );
     }

将模型类名称从学生更改为学生

答案 2 :(得分:1)

因为您的字符串大小与:

相同
MyString1 = "Ben, David, Tom, Jessica"
MyString2 = "25, 27, 21, 22"
MyString3 = "male, male, male, female"

您可以从中获得:

String[] splitt1 = MyString1.split(",");
String[] splitt2 = MyString2.split(",");
String[] splitt3 = MyString3.split(",");

您可以使用splitted String的长度作为for循环计数。

您需要从Students pojo 创建学生列表(应该Student来描述单个对象)。从你的pojo中,你有一个构造函数:

public Student(String name, String age, String sex) {
  ...
}

因此,您可以使用它来创建Student对象。因此,您可以使用以下代码:

List<Students> students = new ArrayList<>();
// using splitt1.length - 1 because index is starting from zero for list.
for (int i = 0; i < splitt1.length - 1; i++) {
  // use .trim() for removing extra whitespace.
  Students student = new Student(splitt1[i].trim(), splitt2[i].trim(), splitt3[i].trim());
  students.add(student);
}

旁注:

相反,对字符串使用单个首选项条目,您可以使用Gson将对象保存到SharedPreference中,在https://stackoverflow.com/a/38089938/4758255更多地阅读

答案 3 :(得分:0)

使用此

ArrayList<String> arrayList1 = new ArrayList<String>();
    for (int i = 0; i < splitt1.length; i++) {
        arrayList1 .add(splitt1[i]);
    }

ArrayList<String> arrayList2 = new ArrayList<String>();
    for (int i = 0; i < splitt2.length; i++) {
        arrayList2 .add(splitt2[i]);
    }

ArrayList<String> arrayList3 = new ArrayList<String>();
    for (int i = 0; i < splitt3.length; i++) {
        arrayList3 .add(splitt3[i]);
    }