2013年7月21日 星期日

Render UI out side of UI thread

Rendering is not available for threads other than the main thread, or UI thread. Put the following run thread creation method in a background function within an activity achieves the goal:

/* 3) Run a UI thread to catch the UI update*/
            runOnUiThread(new Runnable() {
                public void run() {
                    if (rendernow == 1) {
                        selectMenu = (GridView)findViewById(R.id.grid_view);
                        selectMenu.setAdapter(adapter);
                        selectMenu.setOnItemClickListener(new OnItemClickListener() {
                            @Override
                            public void onItemClick(AdapterView<?> parent, View v,
                                    int position, long id) {

                                    /* Handling click event for GridView */
                            }
                        });
                        rendernow = 0;
                    }
                }
             });


Reference links
http://stackoverflow.com/questions/5161951/android-only-the-original-thread-that-created-a-view-hierarchy-can-touch-its-vi

沒有留言:

張貼留言