I have an Activity which has a ListView as its content View. I need to
call Activity.setContentView after a short asynchronous delay from
when Activity.onCreate is called (because there is a delay loading a
network resource needed to fully construct the ListView's
ArrayAdapter). In this case, it seems that the ListView won't enter
trackball mode or receive focus, unless I first touch the screen.

I have tried to distill this to a minimal example pasted below. There
is a comment at the top of the class explaining how the two variables
control the examples behavior.

Any ideas on how I can re-enable trackball mode, without requiring the
user to first touch the screen?


package foobar.androidtest;

import android.app.*;
import android.os.*;
import android.widget.*;

//
// With the settings SYNCHRONOUS=false and SLEEP=3000, then
// when the ListView is displayed, the first item won't be
// highlighted, and the trackball doesn't work.
//
// However, if you either change SYNCHRONOUS=true or SLEEP=0
// then when the ListView is displayed, the first item is
// highlighted, and the trackball works just fine.
//
public class AndroidTest extends Activity {
    private static final boolean SYNCHRONOUS = false;
    private static final int SLEEP = 3000;

    public void onCreate(Bundle bundle) {
        super.onCreate(bundle);
        if(SYNCHRONOUS) {
            doSetContentView();
        }
        else {
            final Handler mainThreadHandler = new Handler();
            Runnable backgroundThreadRunnable = new Runnable() {
                public void run() {
                    optionalSleep();
                    Runnable mainThreadRunnable = new Runnable() {
                        public void run() {
                            doSetContentView();
                        }
                    };
                    mainThreadHandler.post(mainThreadRunnable);
                }
            };
            Thread backgroundThread = new Thread
(backgroundThreadRunnable);
            backgroundThread.start();
        }
    }

    private void doSetContentView() {
        String[] strings = new String[] {
            "SYNCHRONOUS: " + SYNCHRONOUS,
            "SLEEP: " + SLEEP,
            "third item",
            "fourth item",
            "etc..."
        };
        ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(
            this, android.R.layout.simple_list_item_1, strings);
        ListView listView = new ListView(this);
        listView.setAdapter(arrayAdapter);
        setContentView(listView);
    }

    private void optionalSleep() {
        if(SLEEP > 0) {
            try {
                Thread.sleep(SLEEP);
            }
            catch(InterruptedException interruptedException) {
                throw new RuntimeException(interruptedException);
            }
        }
    }
}

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to