这个自定义数组适配器有什么问题?

时间:2013-08-30 15:30:57

标签: java android listview

我正在尝试使用Appointment个对象的集合填充listview'Diary'。打开活动时,我收到运行时异常:java.util.NoSuchElementException。我一直绞尽脑汁想弄清楚这可能会发生什么,但由于我的机器人经验相当有限,我完全被难倒......

我的活动代码在这里:

    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);      
    }

我的自定义适配器代码(DiaryAdapter)如下:

public class DiaryAdapter extends ArrayAdapter {

    ArrayList<Appointment> appointments;

    public DiaryAdapter(Context context, int resource, ArrayList<Appointment> appts) {
        super(context, resource, appts);
        this.appointments = appts;
    }  

    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.appointment_info, 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.startDateTime.toString("hh:mm"));
        viewHolder.v2.setText(temp.subject);
        viewHolder.v3.setText(Db.Functions.getLocation(temp.locationId.toString()));
        viewHolder.v4.setText(Db.Functions.getCustomerName(temp.customerId.toString()));
        return convertView;
    }

    @Override
    public Appointment getItem(int position) {
        return appointments.get(position);
    }
}  

列表项的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>

最后这是我的logcat异常:

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

所以我只想弄清楚为什么我会得到这个例外。我已经按照一些关于如何为列表视图创建自定义数据适配器的教程,据我所知,已经正确完成了。我一定错过了什么,但我不确定是什么。

很抱歉发布这样的问题,但我是Android新手,我完全被难倒了。非常感谢任何帮助!

1 个答案:

答案 0 :(得分:2)

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

您多次致电iter.next(),但只检查iter.hasNext()一次。

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

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

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