Search code examples
androidlistviewbroadcastreceiverwifi

Android: display BroadcastReceiver in ListView


I created a broadcast receiver to follow my Wi-Fi, I'm getting normaly information in LogCat, but I couldn't put the data in a ListView, like I wan't. I put a ListView example to test it in the onReceive method but I didn't work, and I'm having this error:

The constructor ArrayAdapter(Wifi.Receiver, int, int, String[]) is undefined

This is my code:

public class Wifi extends Activity implements OnClickListener{
/** Called when the activity is first created. */
WifiManager wifi;
Button      enab;
String resultsString ; 
String[] myStringArray;

public class Receiver extends BroadcastReceiver{
    @Override
    public void onReceive(Context context, Intent intent) {

    }
ListView listView ;
            // Get ListView object from xml
            listView = (ListView) findViewById(R.id.listView1);

            // Defined Array values to show in ListView
        String[] values = new String[] { "Android List View", 
                                         "Adapter implementation",
                                         "Simple List View In Android",
                                         "Create List View Android", 
                                         "Android Example", 
                                         "List View Source Code", 
                                         "List View Array Adapter", 

                                        };
//getting this error : The constructor ArrayAdapter<String>(Wifi.Receiver, int, int, //String[]) is undefined           
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1, android.R.id.text1, values);


        listView.setAdapter(adapter);

}
}

PS: when I put the code of the ListView in the onCreate method it worked normaly, but I want it in the onReceive method.


Solution

  • In your case this does not refer to Activity Context.

    Use

    ArrayAdapter<String> adapter = new ArrayAdapter<String>(Wifi.this,android.R.layout.simple_list_item_1, android.R.id.text1, values);
    

    Look at the constructor

    public ArrayAdapter (Context context, int resource, int textViewResourceId, T[] objects)
    
    Added in API level 1 Constructor
    
    Parameters 
    
    context             The current context. 
    resourc             The resource ID for a layout file containing a layout to use when instantiating views. 
    textViewResourceId  The id of the TextView within the layout resource to be populated 
    objects             The objects to represent in the ListView.
    

    So the first param is a valid context

    java.lang.Object
       ↳    android.content.Context // see context
           ↳    android.content.ContextWrapper
               ↳    android.view.ContextThemeWrapper
                   ↳    android.app.Activity // see activity
    

    And your class extends Activity

    public class Wifi extends Activity 
    

    Hence use Wifi.this as a valid context.