我有一個Appointment
對象,其中包含幾個字段;主題,開始時間,結束時間等等使用陣列適配器在列表視圖中將對象的多個值分配給單個項目
我試圖讓這些對象的列表出現在列表視圖中。我有一個列表項目佈局xml文件,其中包含4個TextView
對象;開始時間,結束時間,主題和人名。佈局如下:
<?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個不同的TextView,所有這些都在單列表視圖項目中。我怎樣才能做到這一點?