
问题是我的程序大量调用getVIEw()方法,并且我实现了Holder范例,但它仍然非常慢,也许有人可以启发我?
这是我的Adapter类:
import java.util.ArrayList;import androID.content.Context;import androID.location.Location;import androID.vIEw.LayoutInflater;import androID.vIEw.VIEw;import androID.vIEw.VIEwGroup;import androID.Widget.ArrayAdapter;import androID.Widget.TextVIEw; public class IconListVIEwAdapterPharmacy extends ArrayAdapter<PharmacyListItem> { private ArrayList<PharmacyListItem> items; private Context cont; private Location l; public IconListVIEwAdapterPharmacy(Context cont,int textVIEwResourceID,ArrayList<PharmacyListItem> items,Location l) { super(cont,textVIEwResourceID,items); this.cont = cont; this.items = items; this.l = l; } @OverrIDe public VIEw getVIEw(int position,VIEw convertVIEw,VIEwGroup parent) { VIEw v = convertVIEw; PharmacyListItem o = items.get(position); VIEwHolder holder = new VIEwHolder(); if (v == null) { LayoutInflater vi = (LayoutInflater)cont.getSystemService(Context.LAYOUT_INFLATER_SERVICE); v = vi.inflate(R.layout.icon_row_pharmacy,null); holder.tt = (TextVIEw) v.findVIEwByID(R.ID.pharmacy_row_toptext); holder.address = (TextVIEw) v.findVIEwByID(R.ID.pharmacy_row_address); holder.distance = (TextVIEw) v.findVIEwByID(R.ID.pharmacy_row_distance); holder.telephone = (TextVIEw) v.findVIEwByID(R.ID.pharmacy_row_telephone); // associate the holder with the vIEw for later lookup v.setTag(holder); } else { holder = (VIEwHolder)convertVIEw.getTag(); } holder.tt.setText(o.getname()); String add = "" + o.getAddress(); holder.address.setText("" + add.substring(0,add.lastIndexOf(","))); holder.telephone.setText("" + o.getTelephone()); String distancia = "" + o.getdistance(); if(l != null) holder.distance.setText("distancia: " + distancia); return v; } static class VIEwHolder { TextVIEw tt; TextVIEw address; TextVIEw distance; TextVIEw telephone; } } PharmacyListItem类:
import androID.location.Location; public class PharmacyListItem { private String name;private double lat;private double lng;private String telephone;private double distance;public PharmacyListItem(Location l,String name,double lat,double lng,String telephone){ this.name = name; this.lng = lng; this.lat = lat; this.telephone = telephone; Location l2 = new Location(l); l2.setLatitude(lat); l2.setLongitude(lng); distance = l.distanceto(l2);}public String getname(){ return name;}public String getAddress(){ return Geolocator.getAddress(lat,lng);}public String getTelephone(){ return telephone;}public String getdistance(){ return (""+distance).substring(0,(""+distance).indexOf(".")) + "m.";}public double getdist(){ return distance;} }
任何想法将不胜感激!
谢谢你们!
编辑我粘贴我的.xml文件:
<?xml version="1.0" enCoding="utf-8"?><linearLayoutxmlns:androID="http://schemas.androID.com/apk/res/androID"androID:layout_wIDth="fill_parent"androID:layout_height="?androID:attr/ListPreferredItemHeight"androID:padding="6dip"androID:layout_marginRight="4dip"androID:background="@drawable/bg_detalle_1"> <ImageVIEw androID:ID="@+ID/arrow2" androID:layout_wIDth="wrap_content" androID:layout_centerVertical="true" androID:layout_height="wrap_content" androID:layout_alignParentRight="true" androID:layout_alignParentBottom="true" androID:singleline="true" androID:ellipsize="marquee" androID:layout_marginRight="4dip"/> <linearLayout androID:padding="10dip" androID:layout_wIDth="match_parent" androID:layout_height="wrap_content" androID:ID="@+ID/linearLayout3" androID:orIEntation="vertical"> <linearLayout androID:layout_height="wrap_content" androID:ID="@+ID/linearLayout1" androID:layout_wIDth="match_parent"> <TextVIEw androID:gravity="center" androID:textcolor="#525252" androID:layout_height="fill_parent" androID:singleline="true" androID:ellipsize="marquee" androID:text="" androID:textStyle="bold" androID:ID="@+ID/pharmacy_row_toptext" androID:layout_wIDth="wrap_content" androID:layout_marginRight="6dip"></TextVIEw> </linearLayout> <linearLayout androID:layout_height="wrap_content" androID:ID="@+ID/linearLayout2" androID:layout_wIDth="match_parent"> <TextVIEw androID:layout_height="fill_parent" androID:text="TextVIEw" androID:ID="@+ID/pharmacy_row_address" androID:layout_wIDth="wrap_content" androID:layout_marginRight="6dip"></TextVIEw> </linearLayout> <linearLayout androID:layout_height="wrap_content" androID:layout_wIDth="match_parent" androID:ID="@+ID/linearLayout5"> <TextVIEw androID:gravity="center" androID:layout_height="fill_parent" androID:text="TextVIEw" androID:singleline="true" androID:ellipsize="marquee" androID:layout_wIDth="wrap_content" androID:ID="@+ID/pharmacy_row_telephone"></TextVIEw> </linearLayout> <linearLayout androID:layout_gravity="right" androID:layout_height="wrap_content" androID:ID="@+ID/linearLayout5" androID:layout_wIDth="match_parent"> <TextVIEw androID:layout_height="fill_parent" androID:text="TextVIEw" androID:singleline="true" androID:ellipsize="marquee" androID:layout_wIDth="wrap_content" androID:ID="@+ID/pharmacy_row_distance"></TextVIEw> </linearLayout> </linearLayout> </linearLayout>解决方法 而不是这个
public IconListVIEwAdapterPharmacy(Context cont,Location l){ super(cont,items); this.cont = cont; this.items = items; this.l = l;} 做这个
public IconListVIEwAdapterPharmacy(Context cont,items); this.l = l;}
然后在getVIEw访问项中使用getItem(position)而不是items.get(position)并获取上下文调用getContext().
不知道这是否会改善性能.但尝试没有害处.这也是正确的方法.
编辑:
如果您有要下载和显示的配置文件图像,可以像以后一样获取地址并更新UI.
String add = "" + o.getAddress();if (TextUtils.IsEmpty(add)) { // download the geolocation. pass the position to the downloader thread which will update the item at position with address,after fetching is complete. new GetAddresstask(position).execute(o);} 这将释放UI线程专注于显示ListvIEw.
class GetAddresstask extends Asynctask<.......> { int mposition = -1; public voID GetAddresstask(int position) { mposition = position; } String doInBackground(parameters) { return Geolocation.getaddress(parameter[0]); } voID onPostExecute(String result) { // this runs on UI thread. // use mposition here. // update addapter item with this address thats in 'result' variable. // and call adapter.notifyDatasetInvalIDated() } 总结 以上是内存溢出为你收集整理的android – ListView使我的应用程序运行速度很慢,虽然我创建了Holder Class全部内容,希望文章能够帮你解决android – ListView使我的应用程序运行速度很慢,虽然我创建了Holder Class所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)