我從共享偏好得到的數據:把一個ArrayList <String>到一個ArrayList <自定義對象>
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"
我splitt它轉換成String數組:
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的值放入自定義對象數組中? 我有一個對象類與構造函數,getters和setters。
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");
請幫助我,謝謝您的期待!
thx很多,工作正常! Thx爲所有的快速答案! :) – David