0

我有一个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 全部在一个列表视图项中。我怎样才能做到这一点?

4

3 回答 3

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 对象项中设置约会值

于 2013-08-30T14:04:07.830 回答
1

您将需要覆盖 ArrayAdapter 中的 getView 方法,以手动将每一行膨胀到您想要的方式。默认情况下,ArrayAdapter 只是在您的对象上调用 toString() 来显示。

与此类似:自定义 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;
}
于 2013-08-30T13:53:17.453 回答
0

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

于 2013-08-30T14:08:23.097 回答