Hans Vn
Hans Vn

Reputation: 844

Adapter.getItem(position) crashes application

I'm writing an Android Application with a custom Adapter for a list view.

The adapter is a variable in the mainActivity class and contains a list of custom classes. I need to access a class in this list from an other activity, but the application seems to crash when it reaches getItem(position) in the adapter.

Basically this is what the application looks like:

in the MainActivity class there is a basic custom adapter:

public class MainActivity extends Activity {
    public static MyAdapter myAdapter;
    ...
}

The adapter only has the basic functions like getItem and has a list of custom (basic) classes

public class MyAdapter extends BaseAdapter {
    private ArrayList<MyClass> tours = new ArrayList<MyClass>();

    @Override
    public Object getItem(int arg0) {
        return getItem(arg0);
    }
    ...
}

When the other activity is opened, an index is passed to it to access a certain Object from the list.

The only problem is that when I try to access a class from this list, the application crashes... I used following code to access the class from the list in the adapter:

MyClass myClass = (MyClass)MainActivity.myAdapter.getItem(index);

Can someone tell me what I'm doing wrong or why the app crashes?

Upvotes: 0

Views: 502

Answers (1)

Sam
Sam

Reputation: 86948

This is recursive logic:

@Override
public Object getItem(int arg0) {
    return getItem(arg0);
}

There's no way for this method to complete, it simply calls itself again and again until the app throws some type of overflow exception. It should be:

@Override
public MyClass getItem(int arg0) {
    return tours.get(arg0);
}

Notice how this method returns data from your List.

Upvotes: 5

Related Questions