Boris Pawlowski
Boris Pawlowski

Reputation: 1771

notifyDataSetChanged() without freezing mainthread

I have listview and custom adapter that uses my own objects to draw the listitem.

From the other head I have a service that is gathering information realtime and every 0.1s my activity calls for the service information and than redraw listview by calling myCustomAdapter.notifyDataSetChanged() method.

This is bad for me because the objects are really large and my UI thread freezes for some time probably less than 0.01s but still feels bad user experience.

The resource I'm updating is one circle that is drawn by custom drawing class in canvas. Does any of you know how to handle this problem ? Is there some way to update data and redraw listview without stopping my UI thread?

Upvotes: 0

Views: 1375

Answers (1)

José Barbosa
José Barbosa

Reputation: 903

You should try to reuse your convertView, like this:

public View getView (int position, View convertView, ViewGroup parent){
    //inflate the view if it is null
    if( convertView == null ){
        convertView = inflater.inflate(R.layout.my_list_item, parent, false);
    }
    //make the changes on your  convertView that are changed from row to row,
    //such as a text in a TextView
    return convertView;
}

Upvotes: 3

Related Questions