简体   繁体   English

放置一个 ArrayList<String> 进入一个 ArrayList<Custom Object>

[英]Put an ArrayList<String> into an ArrayList<Custom Object>

I get data from shared preferences:我从共享首选项中获取数据:

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

It looks like:看起来像:

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

I splitt it into String Arrays:我将其拆分为字符串数组:

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

Now I put it into an ArrayList:现在我把它放到一个 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]);
}

How can I put the values of the ArrayList into a custom Object Array?如何将 ArrayList 的值放入自定义对象数组? I have one object class with constructor, getters and setters.我有一个带有构造函数、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;
  }

Now I want to fill my Student Object Array, I tried this way, but this is my String Arrays that I fill and it doesn't work:现在我想填充我的学生对象数组,我试过这种方式,但这是我填充的字符串数组,它不起作用:

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

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

I want to sort my ArrayLists and write them into an ObjectArrayList like this:我想对我的 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");

Please Help me, thank you in anticipation!请帮帮我,谢谢您的期待!

You have to create a Student object eg您必须创建一个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));
}

This will solve your issue as of now.到目前为止,这将解决您的问题。 You have to keep in mind that the length of your ArrayList has to be the exactly the same otherwise you will get IndexOutOfBondException .你必须记住你的ArrayList的长度必须完全相同,否则你会得到IndexOutOfBondException

Try this code试试这个代码

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

change the model class name from Students to Student将模型类名称从学生更改为学生

Because you have the same size of the string from:因为你有相同大小的字符串:

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

Which you can get from:您可以从中获得:

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

You can use the length of the splitted String as the for loop count.您可以使用拆分字符串的长度作为 for 循环计数。

You need to create the Student list from your Students pojo (it should be Student to describe a single object).您需要从您的Students pojo创建 Student 列表(应该是Student来描述单个对象)。 From your pojo, you have a constructor:从你的 pojo 中,你有一个构造函数:

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

So, you can use it to create the Student object.因此,您可以使用它来创建 Student 对象。 Therefore, you can use the following code:因此,您可以使用以下代码:

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

Side note:边注:

Instead using single preference entry for the string, you can use Gson to save the object to the SharedPreference read more at https://stackoverflow.com/a/38089938/4758255您可以使用 Gson 将对象保存到 SharedPreference,而不是使用字符串的单个首选项条目,阅读更多内容,访问https://stackoverflow.com/a/38089938/4758255

use this用这个

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM