guys I got a problem with my listview. It runs so slow when scrolling can anybody help me. My listview is custom layout using relative with 3 textview and 1 imageview each row with all content is retrieve from the web. I use a custom adapter and a view holder.
below is my codes on how I use the adapter.
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;
}
}
public View getView(int positiion, View convertView, ViewGroup parent){
...
Drawable draw = LoadImageFromWebOperation(image.get(positiion));
...
}
This is the problem. You should never ever do networking in UI thread. Implement some async task for the job. Also, you can use traceview http://developer.android.com/guide/developing/tools/traceview.html tool to determine performance bottlenecks.
This is your problem, right here..
Drawable draw = LoadImageFromWebOperation(image.get(positiion));
Instead of making repeated URL Requests in your getView()
, Make all your requests at once and store the Drawables
in an array, and then use the array inside the getView
. This will make it considerably faster...
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With