使用数组适配器在listview中将对象的多个值分配给单个项目

时间:2013-08-30 13:39:32

标签: java android listview android-listview jodatime

我有一个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似乎只能将一个字段调整到一个视图 - 但我需要在Appointment对象)到4个不同的TextView >单个列表视图项。我怎么能这样做?

3 个答案:

答案 0 :(得分:2)

您可以向ArrayAdapter提供数组列表对象{{1} -

appointments

然后覆盖getView()并为List项目膨胀视图,然后从指定位置的ArrayList对象项设置约会值

答案 1 :(得分: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 :(得分:0)

考虑使用SimpleCursor Adapter而不是ArrayAdapter, 您可以在其中传递自定义视图并将数据绑定到该视图。 或者通过扩展ArrayAdapter

来制作自定义ArrayAdapter