伙计我的listview出了问题。当滚动任何人帮助我时,它运行得如此之慢。 我的listview是自定义布局,使用相对于3 textview和1 imageview每行,所有内容都从Web检索。 我使用自定义适配器和视图持有者。
下面是关于我如何使用适配器的代码。
public class MessageList extends ListActivity {
@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.listarticle);
loadFeed(link);
setListAdapter(new IconAdapter(this));
}
/* This method load xml file and parse it into message object*/
private void loadFeed(String link){
try{
BaseFeedParser parser = new BaseFeedParser(link);
messages = parser.parse();
titles = new ArrayList<String>(messages.size());
image = new ArrayList<String>(messages.size());
date_post = new ArrayList<String>(messages.size());
descs = new ArrayList<String>(messages.size());
for (Message msg : messages){
titles.add(msg.getTitle());
image.add(msg.getImageLink().toString());
date_post.add(msg.getDate());
descs.add(msg.getDescription());
}
} catch (Throwable t){
Log.e("AndroidNews",t.getMessage(),t);
}
}
/*this is my custom baseadapter */
class IconAdapter extends BaseAdapter{
private LayoutInflater mInflater;
public IconAdapter(Context cxt){
mInflater = LayoutInflater.from(cxt);
}
public class ViewHolder{
private TextView title;
private TextView date;
private TextView desc;
private ImageView thumb;
}
public View getView(int positiion, View convertView, ViewGroup parent){
ViewHolder holder;
if (convertView==null){
convertView = mInflater.inflate(R.layout.row, null);
holder = new ViewHolder();
holder.title = (TextView) convertView.findViewById(R.id.title);
holder.date = (TextView) convertView.findViewById(R.id.date);
holder.desc = (TextView) convertView.findViewById(R.id.deskripsi);
holder.thumb = (ImageView) convertView.findViewById(R.id.thumbnail);
convertView.setTag(holder);
}else{
holder = (ViewHolder) convertView.getTag();
}
holder.title.setText(titles.get(positiion));
holder.date.setText(date_post.get(positiion));
holder.desc.setText(descs.get(positiion).substring(0, 55)+"...");
Drawable draw = LoadImageFromWebOperation(image.get(positiion));
holder.thumb.setImageDrawable(draw);
return convertView;
}
/* this method take image from url that retrieve from xml*/
public Drawable LoadImageFromWebOperation(String url){
try{
InputStream is = (InputStream) new URL(url).getContent();
Drawable d = Drawable.createFromStream(is, "src name");
return d;
}catch (Exception e){
Log.d("image", url, e);
return null;
}
}
@Override
public int getCount() {
return messages.size();
}
@Override
public Object getItem(int position) {
return messages.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
}
答案 0 :(得分:12)
public View getView(int positiion, View convertView, ViewGroup parent){
...
Drawable draw = LoadImageFromWebOperation(image.get(positiion));
...
}
这就是问题所在。你永远不应该在UI线程中进行网络连接。为作业实现一些异步任务。 此外,您还可以使用traceview http://developer.android.com/guide/developing/tools/traceview.html工具来确定性能瓶颈。
答案 1 :(得分:4)
这是你的问题,就在这里..
Drawable draw = LoadImageFromWebOperation(image.get(positiion));
不是在getView()
中重复提出网址请求,而是立即发出所有请求并将Drawables
存储在数组中,然后使用getView
中的数组。这将使它更快......
答案 2 :(得分:1)
答案 3 :(得分:0)
Please use View Holder pattern in getview.
ViewHolder holder = null;
String fileName = arrfiles.get(position);
if (convertView == null) {
holder = new ViewHolder();
LayoutInflater inflator = ((Activity) context).getLayoutInflater();
convertView = inflator.inflate(R.layout.unsubmited_row, parent,
false);
convertView.setClickable(false);
}
else
{
holder = (ViewHolder) convertView.getTag();
}
holder.txtFileName = (TextView) convertView
.findViewById(R.id.txtFileName);
holder.txtisActive = (TextView) convertView
.findViewById(R.id.txtIsActive);
holder.txtFileName.setText(fileName);
convertView.setTag(holder);