Java 什么';这个自定义阵列适配器有什么问题?

Java 什么';这个自定义阵列适配器有什么问题?,java,android,listview,Java,Android,Listview,我试图用约会对象的集合填充listview“日记”。打开活动时,我得到一个运行时异常:java.util.NoSuchElementException。我一直绞尽脑汁想弄清楚这是怎么发生的,但由于我的android经验相当有限,我完全被难倒了 我的活动代码如下: DiaryAdapter dAdapter; DateTime currentDate; ListView diary; @Override protected void onCreate(Bu

我试图用
约会
对象的集合填充listview“日记”。打开活动时,我得到一个运行时异常:
java.util.NoSuchElementException
。我一直绞尽脑汁想弄清楚这是怎么发生的,但由于我的android经验相当有限,我完全被难倒了

我的活动代码如下:

    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++;
        }
        DiaryAdapter adapter = new DiaryAdapter(this, R.layout.appointment_info, appointments);
        //ArrayAdapter<Appointment> arrayAdapter = new ArrayAdapter<Appointment>(this, R.layout.appointment_info, R.id.lblSubject, appointments);
        diary.setAdapter(adapter);      
    }
所以我只是想弄明白为什么会出现这种异常。我已经学习了一些关于如何为listview制作自定义数据适配器的教程,据我所知,我已经正确地完成了。我肯定错过了什么,但我不知道是什么


很抱歉发布这样的问题,但我是android新手,完全被难住了。非常感谢您的帮助

我认为一个可能的问题是,以下代码:

您多次调用
iter.next()
,但只检查了一次
iter.hasNext()

while (iter.hasNext()){
            lastEndTime = iter.next().endDateTime;

            if (count > 0)
            {
                if (iter.next().startDateTime.isAfter(lastEndTime))

             ............................

@Yume117这其实不是一个很长的问题。这些数据块主要是为了在需要时提供参考。您是否尝试使用BaseAdapter而不是arrayadapter?有时出于某些原因,它工作得更好。哎呀,我如何在迭代中引用当前对象?@Teifi您可以将
iter.next()
存储在变量中,例如
Appointment element=iter.next()
,然后使用
element
,而不是连续调用
iter.next()
。天哪,它工作了!谢谢Nambari和Josh M。我的逻辑仍然存在问题(“免费”约会没有出现在列表中),但至少列表视图正在填充,我不再有任何例外:)@Teifi:祝你好运!乔希。
<?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>
08-30 16:13:29.268: E/AndroidRuntime(9917): FATAL EXCEPTION: main
08-30 16:13:29.268: E/AndroidRuntime(9917): java.lang.RuntimeException: Unable to start 

activity ComponentInfo{com.example.myapp/com.example.myapp.DiaryActivity}: 

java.util.NoSuchElementException
08-30 16:13:29.268: E/AndroidRuntime(9917):     at 

android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2110)
08-30 16:13:29.268: E/AndroidRuntime(9917):     at 

android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2135)
08-30 16:13:29.268: E/AndroidRuntime(9917):     at android.app.ActivityThread.access$700

(ActivityThread.java:140)
08-30 16:13:29.268: E/AndroidRuntime(9917):     at android.app.ActivityThread

$H.handleMessage(ActivityThread.java:1237)
08-30 16:13:29.268: E/AndroidRuntime(9917):     at android.os.Handler.dispatchMessage

(Handler.java:99)
08-30 16:13:29.268: E/AndroidRuntime(9917):     at android.os.Looper.loop(Looper.java:137)
08-30 16:13:29.268: E/AndroidRuntime(9917):     at android.app.ActivityThread.main

(ActivityThread.java:4921)
08-30 16:13:29.268: E/AndroidRuntime(9917):     at java.lang.reflect.Method.invokeNative

(Native Method)
08-30 16:13:29.268: E/AndroidRuntime(9917):     at java.lang.reflect.Method.invoke

(Method.java:511)
08-30 16:13:29.268: E/AndroidRuntime(9917):     at com.android.internal.os.ZygoteInit

$MethodAndArgsCaller.run(ZygoteInit.java:1027)
08-30 16:13:29.268: E/AndroidRuntime(9917):     at com.android.internal.os.ZygoteInit.main

(ZygoteInit.java:794)
08-30 16:13:29.268: E/AndroidRuntime(9917):     at dalvik.system.NativeStart.main(Native 

Method)
08-30 16:13:29.268: E/AndroidRuntime(9917): Caused by: java.util.NoSuchElementException
08-30 16:13:29.268: E/AndroidRuntime(9917):     at java.util.AbstractList

$SimpleListIterator.next(AbstractList.java:59)
08-30 16:13:29.268: E/AndroidRuntime(9917):     at 

com.example.myapp.DiaryActivity.populateDiaryNew(DiaryActivity.java:71)
08-30 16:13:29.268: E/AndroidRuntime(9917):     at 

com.example.myapp.DiaryActivity.onCreate(DiaryActivity.java:46)
08-30 16:13:29.268: E/AndroidRuntime(9917):     at android.app.Activity.performCreate

(Activity.java:5206)
08-30 16:13:29.268: E/AndroidRuntime(9917):     at 

android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1094)
08-30 16:13:29.268: E/AndroidRuntime(9917):     at 

android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2074)
08-30 16:13:29.268: E/AndroidRuntime(9917):     ... 11 more
while (iter.hasNext()){
            lastEndTime = iter.next().endDateTime;

            if (count > 0)
            {
                if (iter.next().startDateTime.isAfter(lastEndTime))

             ............................