Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

android custom listview very slow when scrolling

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;
  }

 }
like image 994
Rahadyanteja Avatar asked Dec 22 '10 07:12

Rahadyanteja


2 Answers

  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.

like image 103
Dmitry Ryadnenko Avatar answered Oct 12 '22 23:10

Dmitry Ryadnenko


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...

like image 39
st0le Avatar answered Oct 13 '22 01:10

st0le