簡體   English   中英

放置一個 ArrayList<String> 進入一個 ArrayList<Custom Object>

[英]Put an ArrayList<String> into an ArrayList<Custom 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[] 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");

請幫幫我,謝謝您的期待!

您必須創建一個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

試試這個代碼

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

將模型類名稱從學生更改為學生

因為你有相同大小的字符串:

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(",");

您可以使用拆分字符串的長度作為 for 循環計數。

您需要從您的Students pojo創建 Student 列表(應該是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

用這個

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