带适配器的列表视图中的数组对象列表

时间:2014-04-04 06:44:21

标签: android listview object android-listview android-arrayadapter

我有一个名为约会的类,该类的每个对象都有一个Calendar和一个String

我想在Listview中显示对象的文本部分我该怎么做?

// 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 个答案:

答案 0 :(得分:1)

ArrayAdapter使用对象的toString()创建文本表示形式,因此最简单的方法是以下列方式覆盖toString()

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

顺便说一下,尝试坚持Java命名约定:类名应以大写字母开头,变量名应以小写字母开头。