【发布时间】:2017-03-11 02:12:51
【问题描述】:
我无法弄清楚如何从使用类的数组列表中添加我的信息,以及如何将信息添加到列表视图中的数组适配器。我正在尝试从这个数组中添加名字。
我怎么能做到这一点?
我有这个单独的类(不确定 toString 是否正确):
public class PersonClass {
final private String firstName;
final private String lastName;
final private String birthday;
final private int personPic;
private PersonClass(String fName, String lName, String bDay, int pic) {
firstName = fName;
lastName = lName;
birthday = bDay;
personPic = pic;
}
public String toString() {
return firstName+" "+lastName+" "+birthday+" "+personPic;
}
public static void main(String args[]){
// Information for each person using the PersonClass
PersonClass person1 = new PersonClass("first name", "last name", "01/01/2000", R.drawable.pic1);
// ..... up to 10...
// ArrayList
final ArrayList<PersonClass> personList = new ArrayList<>();
// Add person data to array list
personList.add(person1);
personList.add(person2);
personList.add(person3);
personList.add(person4);
personList.add(person5);
personList.add(person6);
personList.add(person7);
personList.add(person8);
personList.add(person9);
personList.add(person10);
}
}
这是我的阵列适配器。
final ArrayAdapter<String> arrayAd = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1,???);
我不能只将 arraylist 名称添加到适配器,因为该数组有一个类向它添加信息。但我只想在列表视图中显示数组的名字。
【问题讨论】:
-
你能在这里解释一下你想要达到的目标吗?
-
我正在向数组列表中添加一个具有一些信息的人。从这个列表中,我需要获取名字并将其添加到列表视图中。我有适配器的代码:ArrayAdapter
arrayAd = new ArrayAdapter (this, android.R.layout.simple_list_item_1, ??? ); ...但就我而言,我不知道现在该怎么办。 -
我已经发布了我的答案让我知道你是否也在寻找相同的答案?
标签: java class arraylist android-arrayadapter