domingo, 27 de enero de 2013

Awesome explanation of how Android ListView works

Ever run into the problem of adding checkboxes to a ListView and trying to check/uncheck the checkbox assuming that its state was stored in the checkbox itself?

It turns out that Android maintains a pool of views (in the "recycler") and reuses those views when we scroll the list until new items are seen. These new items are actually the old ones with their values set to those of the new ones, so never save state in the views held by a ListView! Save them in the adapter instead, for example.

Assume you have this code in an Adapter (and assume that creating checkbox like this works...):

public View getView(int position, View convertView, ViewGroup parent) {
    CheckBox checkBox;

    if  (convertView == null) {
       checkBox = new CheckBox(mContext);

       checkBox.setOnClickListener(new OnClickListener() {
          void onClick(View v) {
              CheckBox checkBox = (CheckBox) v;

              checkBox.toggle(); // NOT GOING TO WORK AS EXPECTED!
          }
       }
    } else {
       checkBox = (CheckBox) convertView;
    }

          

    return checkBox;
}

There's an awesome explanation here:
http://android.amberfog.com/?p=296

viernes, 25 de enero de 2013

Windows always on top (Windows)

I just found this awesome utility called Filebox Extender that allows you to make windows stay on top when they dont' have the focus.

On top of that, it's open source!