2013-08-30 36 views
0

我有一個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,所有這些都在單列表視圖項目中。我怎樣才能做到這一點?

回答

1

您將需要重寫ArrayAdapter中的getView方法,以手動將每行充氣到您想要的方式。默認情況下,ArrayAdapter只需要在對象上調用toString()來顯示。

與此類似:Custom ArrayAdapter

代碼示例:

static class ViewHolder { 
    TextView v1; 
    TextView v2; 
    TextView v3; 
    TextView v4; 
} 

@Override 
public View getView(int position, View convertView, ViewGroup parent) { 
    final Appointment temp = getItem(position); 
    ViewHolder viewHolder; 

    if (convertView == null) { //inflate convertView and populate viewHolder 
     viewHolder = new ViewHolder(); 
     convertView = LayoutInflater.from(getContext()).inflate(R.layout.yourLayout, parent, false); 
     viewHolder.v1= (TextView) convertView.findViewById(R.id.lblTime); 
     viewHolder.v2= (TextView) convertView.findViewById(R.id.lblSubject); 
     viewHolder.v3= (TextView)convertView.findViewById(R.id.lblLocation); 
     viewHolder.v4= (TextView) convertView.findViewById(R.id.lblCustomer); 
     convertView.setTag(viewHolder); //set the tag 
    } 
    else { 
     viewHolder = (ViewHolder) convertView.getTag(); //re-use the ViewHolder to  save calls to findViewById 
    } 
    viewHolder.v1.setText(temp.getText1()); 
    viewHolder.v2.setText(temp.getText2()); 
    viewHolder.v3.setText(temp.getText3()); 
    viewHolder.v4.setText(temp.getText4()); 
    return convertView; 
} 
2

可以提供數組列表對象appointments到ArrayAdapter -

arrayAdapter = new ArrayAdapter<ArrayList>(this, android.R.layout.simple_list_item_1, appointments); 
listView.setAdapter(arrayAdapter); 


public class CustomAdapter extends ArrayAdapter<Appointment>{ 

ArrayList<Contact> items; 

LayoutInflater mInflater ; 

Context context; 

int layoutResourceId; 

public CustomAdapter(Context context, int layoutResourceId, ArrayList<Appointment> items) { 
    super(context, layoutResourceId, items); 

    this.layoutResourceId=layoutResourceId; 

    this.items = items; 

    this.context=context; 
}....... 

然後覆蓋getView()和膨脹意見的清單項目,然後從指定位置的ArrayList對象項目設置約會值

0

考慮使用SimpleCursor Adapter而不是ArrayAdapter, ,您可以在其中傳遞自定義視圖並將數據綁定到該數據。 或通過擴展ArrayAdapter來自定義ArrayAdapter

相關問題