我有一个Appointment
对象,其中包含几个字段;主题、开始时间、结束时间等
我试图让这些对象的列表出现在列表视图中。我有一个包含 4 个TextView
对象的列表项布局 xml 文件;开始时间、结束时间、主题和人名。布局如下:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<TextView
android:id="@+id/lblTime"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignBaseline="@+id/lblSubject"
android:layout_alignBottom="@+id/lblSubject"
android:paddingTop="16dp"
android:paddingLeft="16dp"
android:text="Time"
android:textSize="16dp"
android:textColor="#FFFFFF" />
<TextView
android:id="@+id/lblSubject"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:text="Subject"
android:textSize="20dp"
android:paddingTop="16dp"
android:paddingRight="16dp"
android:textColor="#FFFFFF" />
<TextView
android:id="@+id/lblLocation"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/lblCustomer"
android:layout_alignBottom="@+id/lblCustomer"
android:layout_alignParentLeft="true"
android:text="Location"
android:paddingBottom="15dp"
android:paddingLeft="16dp"
android:textColor="#FFFFFF"
android:textSize="16dp" />
<TextView
android:id="@+id/lblCustomer"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_below="@id/lblSubject"
android:text="Customer"
android:paddingBottom="15dp"
android:paddingRight="16dp"
android:textSize="16dp"
android:textColor="#FFFFFF" />
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:layout_alignParentRight="true"
android:layout_below="@+id/lblLocation"
android:background="#333333" />
</RelativeLayout>
我想做的是让这个约会列表(包含在 中ArrayList<Appointment>
)显示在我的列表视图中。
这是我的约会对象的简化版本:
public class Appointment {
///////////////////////////
// If isFreeTime is true, this object becomes a dummy appointment.
// It then gets used as a 'free time' entry on the diary screen.
boolean isFreeTime = false;
///////////////////////////
UUID appointmentId;
UUID appointmentTypeId;
UUID customerId;
DateTime startDateTime;
DateTime endDateTime;
String subject;
// lots of other variables here
////////////////////////////////////
// Constructors
public Appointment(){
if (this.appointmentId == null)
this.appointmentId = UUID.randomUUID();
}
public Appointment(UUID id){
if (id == null)
this.appointmentId = UUID.randomUUID();
else this.appointmentId = id;
}
public Appointment(String id){
if (id == null)
this.appointmentId = UUID.randomUUID();
else this.appointmentId = UUID.fromString(id);
}
///////////////////////////////////
public UUID getID(){
return this.appointmentId;
}
public void setID(UUID appointmentId){
this.appointmentId = appointmentId;
}
public boolean save()
{
// Saves this object to db
return true;
}
public boolean hasFreeTimeBefore(Appointment appt2)
{
if (this.endDateTime.toDate().before(appt2.startDateTime.toDate()))
return true;
else return false;
}
public ContentValues getValues()
{
// Puts all local variables into a ContentValues object for db storage.
return values;
}
@Override
public String toString() {
return this.subject;
}
}
到目前为止我的代码:
SimpleCursorAdapter cAdapter;
DiaryAdapter dAdapter;
DateTime currentDate;
ListView diary;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Show the Up button in the action bar.
setupActionBar();
setContentView(R.layout.activity_diary);
diary = (ListView) findViewById(R.id.lstAppts);
currentDate = new DateTime();
populateDiaryNew(currentDate);
}
private void populateDiaryNew(DateTime dt) {
currentDate = dt;
ArrayList<Appointment> appointments = new ArrayList<Appointment>();
Cursor cursor = Db.Functions.getAppointmentList(dt);
if (cursor == null)
return;
cursor.moveToFirst();
while (!cursor.isAfterLast()) {
Appointment appt = Db.Functions.getAppointment(cursor.getString(cursor.getColumnIndex("_id")));
appointments.add(appt);
cursor.moveToNext();
}
cursor.close();
ListIterator<Appointment> iter = appointments.listIterator();
DateTime lastEndTime = new DateTime();
int count = 0;
while (iter.hasNext()){
lastEndTime = iter.next().endDateTime;
if (count > 0)
{
if (iter.next().startDateTime.isAfter(lastEndTime))
{
Appointment freeAppt = new Appointment();
freeAppt.isFreeTime = true;
freeAppt.subject = "Free slot";
freeAppt.startDateTime = lastEndTime;
freeAppt.endDateTime = iter.next().startDateTime;
appointments.add(freeAppt);
}
}
count++;
}
ArrayAdapter<Appointment> arrayAdapter = new ArrayAdapter<Appointment>(this, R.layout.appointment_info, R.id.lblSubject, appointments);
diary.setAdapter(arrayAdapter);
}
问题是ArrayAdapter
唯一似乎将一个字段调整为一个视图 - 但我需要将 4 个不同的字段(来自一个Appointment
对象)调整为 4 个不同的 TextViews 全部在一个列表视图项中。我怎样才能做到这一点?