【问题标题】:Adding arraylist using a class to an array adapter使用类将arraylist添加到数组适配器
【发布时间】: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


【解决方案1】:
import java.util.ArrayList;
import java.util.List;

class Person {
    private String firstName;

    //Might be more fields

    public String getFirstName() {
        return firstName;
    }

    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }

    public Person(String firstName) {
        super();
        this.firstName = firstName;
    }

}

public class SO1 {

    public static void main(String[] args) {

        List<Person> personList = new ArrayList<Person>();
        //Either you can directly add first name in adaptor
        for (int i = 0; i < 10; i++) {
            personList.add(new Person("Name "+i));
        }

        //If you have list than you have to iterate it and add first name
        List<String> adaptorList = new ArrayList<String>();
        personList.forEach( p -> adaptorList.add(p.getFirstName()));

        System.out.println(adaptorList);
    }

}

【讨论】:

  • 这太完美了。这就是我需要得到的字符串。谢谢。
【解决方案2】:

为名字写一个getter。成员变量是私有的,以便使用所谓的 getter 和 setter 方法访问它们。因此,您需要在您的 PersonClass 中使用 getFirstName() 方法。

public String getFirstName()
{
return firstName;
}

在你的主要

List<String> nameList = new ArrayList<>();

nameList.add(person1.getFirstName());

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-02-15
    • 1970-01-01
    • 2020-11-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多