Android ListView Slow when displaying profile Avatar with tweet

I am currently pulling 15 tweets into an ArrayList using a handler, and then passing the finished ArrayList to the updateUI method. So far, it worked perfectly, but when my ListView is full, the scroll experience is very volatile. When I remove the image link from my TwitterAdapter, the ListView has a fast / smooth user interface. I suspect my adapter is trying to reload every graphic when I scroll, but I can’t get the debugger to connect to this method to prove it.

Here is the key code inside the Activity:

@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setTheme(R.style.Theme_BambooZen);
        this.setContentView(R.layout.twitter_layout);

        myListView = (ListView) findViewById(R.id.TwitterListView);         

        /*
         * Remove any existing callbacks to the handler before adding the new handler, 
         * to make absolutely sure we don't get more callback events than we want.
         */
        mHandler.removeCallbacks(mUpdateAdapterTask);
        mHandler.postDelayed(mUpdateAdapterTask, 1000);  //Fire the Handler once, the handler will manage self-calls        

    }   

    private Runnable mUpdateAdapterTask = new Runnable() {
           public void run() {

               try{ 

                 //Get Twitter Tweets on separate thread (Requires Network)                 
                 ArrayList<Tweet> tweets = getTweets("Formula1", 1);                
                 updateUI(tweets);

                 //Refresh every 45 seconds = 45000
                 mHandler.postDelayed(this, 45000); 

               }
               catch (Exception e)
               {
                   e.toString();
               }
           }
    };

    /*
     *  Rebuilds the Tweets on UIThread
     */
    public void updateUI(ArrayList<Tweet> tweets){
        adapter = new TwitterAdapter(this, R.layout.twitter_listitem, tweets);
        myListView.setAdapter(adapter);
    }

Item Adapter:

public class TwitterItemAdapter extends ArrayAdapter<Tweet> {

    public TwitterItemAdapter(Context context, int textViewResourceId) {
        super(context, textViewResourceId);
        // TODO Auto-generated constructor stub
    }

    public static Bitmap getBitmap(String bitmapUrl) {
        try {
            URL url = new URL(bitmapUrl);
            return BitmapFactory.decodeStream(url.openConnection() .getInputStream()); 
        }
        catch(Exception ex) {return null;}
    }
}

Twitter adapter:

public class TwitterAdapter extends ArrayAdapter<Tweet>{

    private ArrayList<Tweet> tweets;

    public TwitterAdapter(Context context, int textViewResourceId, ArrayList<Tweet> tweets) {
        super(context, textViewResourceId, tweets);
        this.tweets = tweets;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {


        View v = convertView;
        if (v == null) {
            LayoutInflater vi = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            v = vi.inflate(R.layout.twitter_listitem, null);
        }

        Tweet tweet = tweets.get(position);
        if (tweet != null) {
            TextView username = (TextView) v.findViewById(R.id.username);
            TextView message = (TextView) v.findViewById(R.id.message);
            ImageView image = (ImageView) v.findViewById(R.id.avatar);

            if (username != null) {
                username.setText(tweet.username);
            }

            if(message != null) {
                message.setText(tweet.message);
            }

            if(image != null) {
                image.setImageBitmap(TwitterItemAdapter.getBitmap(tweet.image_url));                
            }
        }
        return v;
    }   
}
+3
source share
1 answer

, ,

.

getView() TwitterAdapter . getBitmap() ( TwitterItemAdapter), .

, , , , SDK .

0

All Articles