views:

193

answers:

1

Hello,

I have just this piece of code which is quite simple. I have a list and in the onCreate method I added some objects to this list to show them on the screen. I have a broadcast receiver which has to enable/disable some elements of the list when there aren't internet connection.

The broadcast receiver works good if the connection is lost when the application is already in the screen of this activity. The problem is when there is no connection before entering this activity. In this case after calling oncreate() method in the onresume() the receiver is registered, but when I call getListView() inside the receiver it hasn't got any child (though I've added to the adaptor in the oncreate method and I haven't loaded then using any thread at all).

Can anyone tell me why this is happening?

public class MyActivity extends ListActivity {
    private List<MyClass> myObjects;

    private final BroadcastReceiver receiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {

            //check if internet connection is available
            boolean networkAvailable = ... ;
            if (!networkAvailable) {
                //No Internet connection: disabled non-cached objects
                List<MyClass> cachedObjects = getCachedObjects();
                for(int i = 0; i<myObjects.size(); i++){
                    MyClass myObject = myObjects.get(i);
                    if (!cachedSurveys.contains(myObject)) {
                        ListView listView = getListView();
                        //The problem is here: listView is empty when there was no connection
                        //before creating the activity so the broadcast receiver was called in a sticky way                     
                        View child = listView.getChildAt(i);
                        child.setEnabled(false);

                    }
                }
            } else {
                // Internet connection: enable all myObjects
                 int size = getListView().getChildCount();
                 for (int i = 0; i < size; i++) {
                    View child = getListView().getChildAt(i);
                    child.setEnabled(true);
                }
            }
        }
     }; 

     @Override
     public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        myObjects = getMyObjects();

        setListAdapter(new ArrayAdapter<MyClass>(this, android.R.layout.simple_list_item_1, myObjects));
        getListView().setTextFilterEnabled(true);   
}

    @Override
    protected void onResume() {
        super.onResume();
        IntentFilter intentFilter = new IntentFilter("android.net.conn.CONNECTIVITY_CHANGE");
        registerReceiver(receiver, intentFilter);
    }

    @Override
    protected void onPause() {
        super.onPause();
        unregisterReceiver(receiver);
    }
}

Thanks

+3  A: 

Hi Javi,

First, I think we should go through some facts:

  • The activity will start drawing its content after the onResume() method is called but not "right after"
  • After you call ListView.setListAdapter(...) the ListView only store the adapter object, at that time, there is no child view.
  • A child view will be available after the ListView draw that child on screen (after Adapter.getView() is called) and the children view list will increase one by one each time the ListView add one more child view until ListView draws them all on screen.

And for your code, the reason why you can not get the children view list at the first time activity starts is "the ListView has not draw anything on screen". Because the onReceive() method of the receiver is triggered before the ListView draws.

You can check that by yourself by overriding the adapter and show log to see the order those methods are called. Here is my result:

07-30 23:06:03.231: DEBUG/MyActivity(386): onResume 1280505963237   
07-30 23:06:03.281: DEBUG/MyActivity(386): onReceive 1280505963281
07-30 23:06:03.361: DEBUG/MyActivity(386): getView 0 1280505963371
07-30 23:06:03.381: DEBUG/MyActivity(386): getView 1 1280505963386

Hope my answer can help you!

Bino
Thank you very much.
Javi