What’s wrong with this custom array adapter? Code Answer

Hello Developer, Hope you guys are doing great. Today at Tutorial Guruji Official website, we are sharing the answer of What’s wrong with this custom array adapter? without wasting too much if your time.

The question is published on by Tutorial Guruji team.

I am trying to populate a listview ‘Diary’ with a collection of Appointment objects. When opening up the activity, I get a runtime exception: java.util.NoSuchElementException. I’ve been racking my brains trying to figure out how this might be happening, but as my android experience is fairly limited, I am completely stumped…

My activity code is here:

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

My custom adapter code (DiaryAdapter) is as follows:

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

The XML layout for the list item:

<?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>

And finally here is my logcat exception:

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

So I’m just trying to figure out why I’m getting this exception. I’ve followed a few tutorials on how to make a custom data adapter for a listview, and to my knowledge, have done it correctly. I must be missing something, but I’m not sure what.

Sorry to post a question like this but I am new to android and am completely stumped. Any help is much appreciated!

Answer

I think one possible place for issue would be, following code:

You are calling iter.next() multiple times, but checking iter.hasNext() only once.

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

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

             ............................
We are here to answer your question about What’s wrong with this custom array adapter? - If you find the proper solution, please don't forgot to share this with your team members.

Related Posts

Tutorial Guruji