0

I made a custom adapter class using baseAdapter and it seems to be working just fine. BUT when I do the onItemClickMethod it returned a StackOverFlowError. Here is my adapter class :

public class ShowroomListAdapter extends BaseAdapter
{
    Context context;
    private LayoutInflater inflater;
    private int[]icons=
        {
            R.drawable.icon_service_center, 
            R.drawable.icon_car_showroom,
            R.drawable.icon_office
        };
    private List<Showroom>showrooms = null;

    public ShowroomListAdapter(Context paramContext, List<Showroom>paramList)
    {
        this.context = paramContext;
        this.showrooms = paramList;
        this.inflater = LayoutInflater.from(paramContext);
    }

    @Override
    public int getCount() 
    {
        return this.showrooms.size();
    }

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

    @Override
    public long getItemId(int paramInt) 
    {
        return paramInt;
    }

    @SuppressWarnings("unused")
    @Override
    public View getView(int paramInt, View paramView, ViewGroup paramViewGroup) 
    {
        Showroom localshowroom = (Showroom)this.showrooms.get(paramInt);
        ViewHolder localViewHolder;
        if(paramView ==null)
        {
            paramView = this.inflater.inflate(R.layout.showroom_list, null);//get the list layout with inflater
            localViewHolder = new ViewHolder();
            localViewHolder.company = ((TextView)paramView.findViewById(R.id.car_company));
            localViewHolder.addr1 =((TextView)paramView.findViewById(R.id.car_addr1));
            localViewHolder.addr2 =((TextView)paramView.findViewById(R.id.car_addr2));
            localViewHolder.addr3=((TextView)paramView.findViewById(R.id.car_addr3));
            localViewHolder.distance =((TextView)paramView.findViewById(R.id.car_distance));
            localViewHolder.carLogo =((ImageView)paramView.findViewById(R.id.car_logo));
            localViewHolder.carLogo.setImageResource(R.drawable.icon_bmw);
            paramView.setTag(localViewHolder);//connecting the view holder to the View, thus enabling the paramView to return the object from local view holder
        }

            localViewHolder = (ViewHolder)paramView.getTag();

            localViewHolder.company.setText(localshowroom.name);
            localViewHolder.addr1.setText(localshowroom.address1);
            localViewHolder.addr2.setText(localshowroom.address2);
            localViewHolder.addr3.setText(localshowroom.postcode +" "+ localshowroom.state);
            localViewHolder.distance.setText(localshowroom.distance);
            localViewHolder.carLogo.setImageResource(R.drawable.icon_toyota);

            return paramView;   

    }

    public boolean isEmpty()
    {
        return (this.showrooms==null) || (this.showrooms.size()==0);
    }
    private static class ViewHolder
    {
        TextView addr1;
        TextView addr2;
        TextView addr3;
        ImageView carLogo;
        TextView company;
        TextView distance;

        ViewHolder()
        {

        }
    }
}

And this is the main class :

public class NearbyShowroomActivity extends ListActivity
{
    private StringBuilder builder;
    private Showroom show ;
    private List<Showroom>showrooms = new ArrayList<Showroom>();

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.nearby_showroom);

        showrooms.add(new Showroom("Toyota", "MTR Automobile Sdn Bhd", "Lot 312, Jalan Sungai Besi,", "Kuala Lumpur", "", "57100", "Kuala Lumpur W.P", "03 9222 3312", 3.044626D, 101.723348D, "12.91KM"));
        showrooms.add(new Showroom("Toyota", "Laser Motor Sdn Bhd", "Lot 61, Jalan Maarof,", "Bangsar", "", "59000", "Kuala Lumpur W.P", "03 2287 3272", 3.127044D, 101.676785D, "7.61KM"));
        showrooms.add(new Showroom("Toyota", "Dynamic View Sdn Bhd", "No.12 & 12A, Jalan 22/70A Desa Sri Hartamas ,", "Desa Sri Hartamas", "", "50480", "Kuala Lumpur W.P", "03 6201 6118", 3.162246D, 101.65208699999999D, "9.39KM"));
        showrooms.add(new Showroom("Toyota", "Motozoom Sdn Bhd", "No 2, Jalan Wangsa Niaga 1,", "Off Jalan 2/27 A", "Wangsa Maju,", "53300", "Kuala Lumpur W.P", "03 4143 3088", 3.19781D, 101.728776D, "4.20KM"));
        showrooms.add(new Showroom("Toyota", "Wheelcorp Sdn Bhd", "Lot 214, Batu 3, Jalan Klang Lama,", "", "", "58000", "Kuala Lumpur W.P", "03 7982 3636", 3.104109D, 101.677488D, "9.07KM"));
        showrooms.add(new Showroom("Toyota", "Roda Express Auto Sdn Bhd", "Lot 9165, Batu 4, Jalan Genting Klang ,", "Setapak", "", "53300", "Kuala Lumpur W.P", "03 4025 1845", 3.204079D, 101.72284999999999D, "5.04KM"));

        getListView().setAdapter(new ShowroomListAdapter(this, this.showrooms));
    }
    @Override
    protected void onListItemClick(ListView paramListView, View paramView, int paramInt, long paramLong)
    {
        show = (Showroom)getListView().getAdapter().getItem(paramInt);
        builder = new StringBuilder("showroom = ");


         startActivity(new Intent("android.intent.action.VIEW", Uri.parse((new StringBuilder("geo:0,0?q=")).append(show.longitude).append(",").append(show.latitude).append("(").append(show.name).append(")").toString())));
//      
//      }

    }
}

the stacktrace is :

07-09 12:10:36.719: E/AndroidRuntime(31304): FATAL EXCEPTION: main
07-09 12:10:36.719: E/AndroidRuntime(31304): java.lang.StackOverflowError
07-09 12:10:36.719: E/AndroidRuntime(31304):    at com.jatismobile.oac.adapter.ShowroomListAdapter.getItem(ShowroomListAdapter.java:45)
07-09 12:10:36.719: E/AndroidRuntime(31304):    at com.jatismobile.oac.adapter.ShowroomListAdapter.getItem(ShowroomListAdapter.java:45)
07-09 12:10:36.719: E/AndroidRuntime(31304):    at com.jatismobile.oac.adapter.ShowroomListAdapter.getItem(ShowroomListAdapter.java:45)

Is there something wrong with the adapter? Any answer will be appreciated and upvoted. thanks

Karate_Dog
  • 1,265
  • 5
  • 20
  • 37

2 Answers2

2

You must change the Adapter's getItem method. I think it should return something like this, in your case.

@Override
public Object getItem(int paramInt) 
{
    return this.showrooms.get(paramInt);
}
Naresh
  • 3,174
  • 1
  • 17
  • 22
1

Your getItem calls this.getItem, so this function will enter into a never ending recursive loop

Pethical
  • 1,472
  • 11
  • 18