Question

I have a class called appointments each object of that class has a Calendar and a String

i want to display the text portion of the Object in a Listview how do i do this?

// Code for my class trying to display the appointments
public class Display extends Activity {
ArrayList<appointment> listoffappointments = new ArrayList<appointment>();
adapter = new ArrayAdapter(instance,R.layout.mylist, listoffappointments) ; 
listView.setAdapter(adapter) ;
}
//Code for the appointment class

import java.util.Calendar;

public class appointment  implements Comparable<appointment> {

Calendar time;
String Text;
public appointment(Calendar tempc, String temptext) {
    Text = temptext;
    time = tempc;
}

public void settext(String text)
{
    Text = text;
}
public String gettext()
{
    return Text;
}
public Long gettimeofappt()
{
    return time.getTimeInMillis();

}

@Override
public int compareTo(appointment o) {
     return Long.valueOf((Long)time.getTimeInMillis()).compareTo(Long.valueOf(o.gettimeofappt()));
}


}
Was it helpful?

Solution

ArrayAdapter uses your object's toString() to create a text representation of it, so the easiest way for you to go is to override the toString() in the following way:

@Override
public String toString() {
    return Text;
}

By the way, try to stick to the Java naming conventions: class names should start with uppercase letters and variable names should start with lowercase letters.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top