0

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


}

1 Answer 1

1

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.

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

3 Comments

wow thank you so much never knew arrayadaptor uses tostring and yes sir will fix that thank you !
also one more thing Egor im calling collections.Sort on the arraylist called listoffappointments although it doesint seem to rearrange my array elements is the comapre to i have listed in the appointment class above correct? @Egor
@lilz4life, It looks correct, however using Long.valueOf() is probably redundant, as you can just write this.gettimeofappt().compareTo(that.gettimeofappt())

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.