0

I'm having trouble figuring out how to add my information from an arraylist that is using a class, and adding the information to an array adapter in a list view. I'm trying to add the first name from this array.

How am I able to do that?

I have this separate class (not sure if the toString is correct):

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

  }

}

This is my array adapter.

final ArrayAdapter<String> arrayAd = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1,???);

I can't just add the arraylist name to the adapter because the array has a class adding the information to it. But I just want to show the first name of the array on the list view.

3
  • Can you explain more what you want to achieve here? Commented Mar 11, 2017 at 2:29
  • I'm adding a person with some information to an array list. From this list I need to get the first name and add this to a list view. I have the code for the adapter:ArrayAdapter<String> arrayAd = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, ??? ); ... but this is as far as I go, I'm not sure what to do now. Commented Mar 11, 2017 at 2:34
  • I have posted my answer let me know if you looking for the same? Commented Mar 11, 2017 at 2:48

2 Answers 2

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

}
Sign up to request clarification or add additional context in comments.

1 Comment

This was perfect. That's what I needed to get the strings. Thank you.
1

Write a getter for the name. Member variables are private in order to access them with so called getter and setter Methods. Therefore you'd want the getFirstName() Method in your PersonClass.

public String getFirstName()
{
return firstName;
}

In your main

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

nameList.add(person1.getFirstName());

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.