-1

I'm trying to do ListView. This is my code:

List<String> list = new ArrayList<>();
String[] add1 = {"FirstName1 ","LastName1", "12-12.1993"};
String[] add2 = {"FirstName2 ","LastName2", "20-12.1993"};
// Then adds these tables to the list;
list.add(add1[0] + add1[1] + add1[2]);
list.add(add2[0] + add2[1] + add2[2]);
//I create a ListView adapter
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_2, list);

Now, My ListView looks like this: "FirstName1 LastName1 12-12.1993" etc.

I would like to looks like this: "FirstName1 LastName1".

I could not deleted the date, because it need to be sent to SecondActivity.

Calvin
  • 3,302
  • 2
  • 32
  • 41
  • You can hide extra column like this: http://stackoverflow.com/questions/14711065/hide-column-in-listview-android – Jehy Jun 01 '16 at 10:08
  • Yes, @Selvin. Very clear! ;) What I did understand: `"... List ... List?"` –  Jun 01 '16 at 10:12
  • @Selvin Jestem początkującym programistą. Rozumiem, że lepszym podejściem byłoby zrobić osobą klasę która odpowiadałaby za tą listę tak? – Kamil Szczęsny Jun 01 '16 at 10:25
  • Yeap ... or just [a SimpleAdapter with HashMap List](http://stackoverflow.com/questions/6305899/custom-listview-android/6306901#6306901) ... for example if you remove `"Icon"` from first array and `R.id.imageView1` from second then image would not be mapped(Adapter will show default icon) `new String[] {"Icon","Chance","TeamID"}, new int[] { R.id.imageView1, R.id.textView1,R.id.textView2})` but still it will be in the HashMap ready to use later – Selvin Jun 01 '16 at 10:27
  • Ok, I understand. Thank you very much!! – Kamil Szczęsny Jun 01 '16 at 10:31

1 Answers1

2

Make new Class that hold a Person

Person.java

public class Person {
    private String name;
    private String surname;
    private String date;


    public Person(String name, String surname, String date) {
        this.name = name;
        this.surname = surname;
        this.date = date;
    }


   public String getName() {
        return name;
    }

    public String getSurname() {
        return surname;
    }

    public String getDate() {
        return date;
    }

    @Override
    public String toString() {
        return "Person{" +
                "name='" + name + '\'' +
                ", surname='" + surname + '\'' +
                ", date='" + date + '\'' +
                '}';
    }
}

Then on your main activity create an ArrayList. this Array holds a list of Persons

   ArrayList<Person> list =new ArrayList<>();
   list.add(new Person("FirstName1 ","LastName1", "12-12.1993"));
   list.add(new Person("FirstName2 ","LastName2", "20-12.1993"));

if you want to see the items in ArrayList you can type this:

list.get(0).toString();

or

Log.e("ArrayList","ArrayList Position 0 :"+list.get(0).toString());

or Get the name of user at position 0

Log.e("ArrayList","Name Position 0 :"+list.get(0).getName());

Finally you must Create a Custom Adapter that you pass him the Custom Array.

CustomAdapter adapter= new CustomAdapter(this,list);

just look this tutorial it will help : Tutorial

Cliff
  • 682
  • 8
  • 30