Public classViewholder {//I added a generic return type to reduce the casting noise in client code@SuppressWarnings ("Unchecked") Public Static<textendsView> T Get (view view,intID) {Sparsearray<View> Viewholder = (sparsearray<view>) View.gettag (); if(Viewholder = =NULL) {Viewholder=NewSparsearray<view>(); View.settag (Viewholder); } View Childview=viewholder.get (ID); if(Childview = =NULL) {Childview=View.findviewbyid (ID); Viewholder.put (ID, Childview); } return(T) Childview; }}
Use the following methods:
@Override PublicView GetView (intposition, View Convertview, ViewGroup parent) { if(Convertview = =NULL) {Convertview=Layoutinflater.from (context). Inflate (R.layout.banana_phone, parent,false); } ImageView Bananaview=Viewholder.get (Convertview, R.id.banana); TextView Phoneview=Viewholder.get (Convertview, R.id.phone); Bananaphone Bananaphone=GetItem (position); Phoneview.settext (Bananaphone.getphone ()); Bananaview.setimageresource (Bananaphone.getbanana ()); returnConvertview;}
You can add this class to a common class library once and for all.