我使用 baseAdapter 制作了一个自定义适配器类,它似乎工作得很好。但是当我执行 onItemClickMethod 时,它返回了 StackOverFlowError。这是我的适配器类:
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()
{
}
}
}
这是主要课程:
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())));
//
// }
}
}
堆栈跟踪是:
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)
适配器有问题吗?任何答案都将受到赞赏和赞成。谢谢